#!/usr/bin/env bash
set -Eeuo pipefail

APP_DIR="${1:-/opt/containers/recipe-app}"
COMPOSE_FILES=(-f compose.yml -f compose.flutter.yml)
SERVICE="recipe-flutter"

log() { echo "[flutter-rebuild] $*"; }
fail() { echo "[flutter-rebuild][ERROR] $*" >&2; exit 1; }

cd "$APP_DIR" || fail "Kunde inte byta till katalog: $APP_DIR"

if ! docker compose version >/dev/null 2>&1; then
  fail "docker compose saknas eller fungerar inte"
fi

if [ ! -f "compose.yml" ] || [ ! -f "compose.flutter.yml" ]; then
  fail "compose.yml eller compose.flutter.yml saknas i $APP_DIR"
fi

log "Bygger om $SERVICE"
docker compose "${COMPOSE_FILES[@]}" build "$SERVICE"

log "Startar om $SERVICE"
docker compose "${COMPOSE_FILES[@]}" up -d "$SERVICE"

log "Status"
docker compose "${COMPOSE_FILES[@]}" ps "$SERVICE"

log "Klart"
