#!/bin/bash

set -eu

# cd to location of script
cd "$(dirname "$(realpath "$0")")"

# Builds the docker images.
# Usage: ./build {COMPONENTS}, or just ./build to build all.
# The resulting images are named wubloader-COMPONENT.
# Pass PUSH=true to also push the resulting images, or PUSH=latest to push them as :latest tag

# The different images we can build
COMPONENTS=(
	backfiller
	buscribe
	buscribe_api
	chat_archiver
	cutter
	downloader
	graphs
	nginx
	playlist_manager
	postgres
	restreamer
	segment_coverage
	sheetsync
	thrimshim
)

# Define push if not already defined
PUSH=${PUSH:-}

# Base is the repository namespace information, not including the wubloader-component part.
BASE="ghcr.io/dbvideostriketeam"

# cache name is the name of the repository we store shared layer cache in
CACHE_NAME="$BASE/wubloader-cache"

# The docker image tag, derived from the git commit + whether working tree is clean
TAG=$(./get-build-tag)

if [ "$#" -gt 0 ]; then
	COMPONENTS=("$@")
fi

# If CACHE is set, image layers will be pushed to the registry during the build,
# and re-use any layers from this cache instead of rebuilding them.
# You can set CACHE=readonly to only use the cache and not write to it (eg. due to permissions)
# Setting CACHE requires buildah be installed and configured, as this is used for the build instead of docker.
case "${CACHE:-}" in
	"")
		CACHE_ARGS=()
		BUILD_CMD="docker"
		;;
	readonly)
		CACHE_ARGS+=("--cache-from" "$CACHE_NAME")
		BUILD_CMD="buildah"
		;;
	*)
		CACHE_ARGS+=("--layers" "--cache-to" "$CACHE_NAME" "--cache-from" "$CACHE_NAME")
		BUILD_CMD="buildah"
		;;
esac

# check for MULTIPLATFORM flag. error out if we're not using buildah for now. docker buildx should be able to do it too, but needs testing
if [ -n "$MULTIPLATFORM" ]; then
	if [ "$BUILD_CMD" == "docker" ]; then
		echo "Multiplatform builds are not yet supported with docker! Use buildah instead."
		exit 1
	fi
fi
		
for component in "${COMPONENTS[@]}"; do
	echo "Building image for $component"
	latest="$BASE/wubloader-$component:latest"
	specific="$BASE/wubloader-$component:$TAG"
	if [ -n "$MULTIPLATFORM" ]; then
		echo "Creating multi-arch manifests"
		"$BUILD_CMD" manifest create "$specific"
		"$BUILD_CMD" manifest create "$latest"

		# buscribe doesn't (yet?) build under arm64 due to pulling in libc-bin, skip arm64 for it
		if [ "$component" != "buscribe" ]; then
			"$BUILD_CMD" build \
				-f "$component/Dockerfile" \
				--manifest "$specific" \
				--manifest "$latest" \
				--jobs 2 \
				--platform=linux/amd64,linux/arm64 \
				"${CACHE_ARGS[@]}" \
				.
		else
			"$BUILD_CMD" build \
				-f "$component/Dockerfile" \
				--manifest "$specific" \
				--manifest "$latest" \
				--platform=linux/amd64 \
				"${CACHE_ARGS[@]}" \
				.
		fi
	else
		"$BUILD_CMD" build \
			-f "$component/Dockerfile" \
			-t "$latest" \
			-t "$specific" \
			--manifest "wubloader-$component" \
			"${CACHE_ARGS[@]}" \
			.
	fi
	echo "Built image wubloader-$component:$TAG"
	if [ -n "$PUSH" ]; then
		echo "Pushing tag $specific"
		"$BUILD_CMD" push "$specific"
	fi
	if [ "$PUSH" == "latest" ]; then
		echo "Pushing tag $latest"
		"$BUILD_CMD" push "$latest"
	fi
done