#!/bin/bash
usage() {
	cat <<EOF
Usage:
	docker_build.sh <target> <registry> [<args>]
	docker_build.sh -h

	Build and push a docker image to a arm64v8 registry

	Targets: 
		base     The pacakges aimed at running on the raspberry pi
EOF
}


TARGET="$1"
REGISTRY="$2"
ARGS="$2"

if [[ $TARGET == "-h" || $TARGET == "--help" ]]; then
	usage
	exit 0
fi

if [[ $# -lt 2 ]]; then
	echo "Error: Expected at least 2 arguments."
	usage
	exit 1
fi

trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT

case $TARGET in
	"base") 
		ssh -N \
			-o StrictHostKeyChecking=no \
			-o UserKnownHostsFile=/dev/null \
			-L 0.0.0.0:5000:localhost:5000 \
			$REGISTRY &

		echo "Building target: base"

		sudo docker buildx build \
			--platform linux/arm64 \
			-t localhost:5000/toid:latest \
			-f Dockerfile \
			--output type=registry \
			.
		;;
	*)
		echo "Target not defined"
		exit 1
		;;
esac
