#!/bin/sh

if ! which podman > /dev/null 2>&1; then
	echo "podman is not in your path"
	exit 1
fi

CONTAINER_DIR="$(dirname "$(realpath "${0}")")"
CONTAINER_TAG="dbt2-appimage"

# Use the return code from `podman inspect` to determine if the container image
# needs to be created.
if ! podman inspect $CONTAINER_TAG > /dev/null 2>&1; then
	"${CONTAINER_DIR}/build-appimage-container" || exit 1
fi

podman run \
		--rm \
		--user "$(id -u):$(id -g)" \
		--userns=keep-id \
		-v "${CONTAINER_DIR}/..:/src/dbt2:rw,Z" \
		--env PKG_CONFIG_PATH="/usr/lib/pkgconfig" \
		-w /src/dbt2 \
		$CONTAINER_TAG \
		make appimage
