#!/bin/bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"

if [ -f "$PROJECT_ROOT/.env" ]; then
    set -a
    source "$PROJECT_ROOT/.env"
    set +a
fi

REGISTRY="${REGISTRY:?REGISTRY is required}"
IMAGE_TAG="${IMAGE_TAG:?IMAGE_TAG is required}"
LOADER_URL="${LOADER_URL:-http://loader:8080}"
ANNOTATIONS_URL="${ANNOTATIONS_URL:-http://annotations:8080}"

usage() {
    echo "Usage: $0 [--help]"
    echo "Start Azaion.Detections service container."
    exit 0
}

for arg in "$@"; do
    case "$arg" in
        --help) usage ;;
    esac
done

IMAGE="${REGISTRY}/azaion/detections-cpu:${IMAGE_TAG}"
CONTAINER_NAME="azaion-detections"

echo "Starting $CONTAINER_NAME from $IMAGE ..."

docker run -d \
    --name "$CONTAINER_NAME" \
    --restart unless-stopped \
    -p 8080:8080 \
    -e LOADER_URL="$LOADER_URL" \
    -e ANNOTATIONS_URL="$ANNOTATIONS_URL" \
    -v "$(pwd)/classes.json:/app/classes.json:ro" \
    --shm-size=512m \
    --memory=4g \
    "$IMAGE"

echo "Container $CONTAINER_NAME started."
