#!/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
	bus_analyzer
	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
		
for component in "${COMPONENTS[@]}"; do
	echo "Building image for $component"
	latest="$BASE/wubloader-$component:latest"
	specific="$BASE/wubloader-$component:$TAG"
	"$BUILD_CMD" build \
		-f "$component/Dockerfile" \
		-t "$latest" \
		-t "$specific" \
		"${CACHE_ARGS[@]}" \
		.
	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