#!/usr/bin/env bash
# Script to build and tag the yt-dlp Docker image.

set -e

SCRIPT_DIR=$(dirname "$(realpath "$0")")
PROJECT_ROOT=$(realpath "$SCRIPT_DIR/..")
DOCKERFILE_DIR="$PROJECT_ROOT/ytops_client/youtube-dl"
IMAGE_NAME=${1:-"ytops/yt-dlp"}

# The default version is 'latest'. If a release version file exists, use that for tagging.
VERSION="latest"
VERSION_FILE="$DOCKERFILE_DIR/release-versions/latest.txt"

if [ -f "$VERSION_FILE" ]; then
    VERSION=$(cat "$VERSION_FILE")
    echo "Found version: $VERSION from $VERSION_FILE"
fi

echo "Building Docker image: $IMAGE_NAME:$VERSION"
echo "Dockerfile location: $DOCKERFILE_DIR"

docker build -t "$IMAGE_NAME:$VERSION" "$DOCKERFILE_DIR"

if [ "$VERSION" != "latest" ]; then
    echo "Also tagging as: $IMAGE_NAME:latest"
    docker tag "$IMAGE_NAME:$VERSION" "$IMAGE_NAME:latest"
fi

echo "Build complete."
echo "Image tags created:"
echo "  - $IMAGE_NAME:$VERSION"
if [ "$VERSION" != "latest" ]; then
    echo "  - $IMAGE_NAME:latest"
fi
