#!/bin/bash
set -euo pipefail

cd "$(dirname "$0")"

# Bust the Docker build cache by injecting a timestamp
TIMESTAMP=$(date +%s)
sed -i -E "s/ARG CACHE_BUSTER=.*/ARG CACHE_BUSTER=${TIMESTAMP}/" Dockerfile

echo "=== Stopping existing container ==="
docker compose down 2>/dev/null || true

echo "=== Building (no cache) ==="
docker compose build --no-cache

echo "=== Starting ==="
docker compose up -d

# Restore Dockerfile so git diff stays clean
sed -i -E "s/ARG CACHE_BUSTER=.*/ARG CACHE_BUSTER=\${TIMESTAMP}/" Dockerfile

echo "=== Done. Container: $(docker compose ps -q) ==="
