#!/usr/bin/env bash
###############################################################################
# pod-start.sh — Build image, create pod, start containers
#
# Usage:   ./pod-start.sh
#          ./pod-start.sh --server-ip 192.168.1.50
#          ./pod-start.sh --hls-size 4g --dl-size 4g
#
# Requires: Podman installed and running
###############################################################################

set -euo pipefail

SERVER_IP="127.0.0.1"
HLS_SIZE="2g"
DL_SIZE="2g"
IMAGE_NAME="o11v4"
POD_NAME="o11v4-pod"

while [[ $# -gt 0 ]]; do
    case "$1" in
        --server-ip)  SERVER_IP="$2";  shift 2 ;;
        --hls-size)   HLS_SIZE="$2";   shift 2 ;;
        --dl-size)    DL_SIZE="$2";    shift 2 ;;
        --image)      IMAGE_NAME="$2"; shift 2 ;;
        --pod)        POD_NAME="$2";   shift 2 ;;
        *) echo "Unknown option: $1"; exit 1 ;;
    esac
done

echo ""
echo "========================================"
echo "  o11v4 Podman Pod Setup"
echo "========================================"
echo ""

# ── Clean up any existing pod ────────────────────────────────────────────────
if podman pod exists "$POD_NAME" 2>/dev/null; then
    echo "Removing existing pod '$POD_NAME'..."
    podman pod stop "$POD_NAME" 2>/dev/null || true
    podman pod rm -f "$POD_NAME" 2>/dev/null || true
fi

# ── Build the image ─────────────────────────────────────────────────────────
echo ""
echo "[1/4] Building image '$IMAGE_NAME'..."
podman build -t "$IMAGE_NAME" .
echo "      Image built successfully."

# ── Create the pod ──────────────────────────────────────────────────────────
echo ""
echo "[2/4] Creating pod '$POD_NAME'..."
podman pod create \
    --name "$POD_NAME" \
    --add-host "lic.cryptolive.one:$SERVER_IP" \
    --add-host "lic.bitmaster.cc:$SERVER_IP" \
    -p 8484:8484 \
    -p 8080:80 \
    -p 8443:443 \
    -p 5454:5454
echo "      Pod created."

# ── Start the license server container ──────────────────────────────────────
echo ""
echo "[3/4] Starting license server container..."
podman run -d \
    --pod "$POD_NAME" \
    --name licserver \
    -e "SERVER_IP=$SERVER_IP" \
    "$IMAGE_NAME" \
    /entrypoint-licserver.sh
echo "      License server started."

# ── Start the o11v4 app container ───────────────────────────────────────────
echo ""
echo "[4/4] Starting o11v4 app container..."
podman run -d \
    --pod "$POD_NAME" \
    --name o11v4-app \
    -e "SERVER_IP=$SERVER_IP" \
    --tmpfs "/home/o11/hls:size=$HLS_SIZE,mode=1777" \
    --tmpfs "/home/o11/dl:size=$DL_SIZE,mode=1777" \
    "$IMAGE_NAME" \
    /entrypoint-app.sh
echo "      o11v4 app started."

# ── Done ────────────────────────────────────────────────────────────────────
echo ""
echo "========================================"
echo "  All running!"
echo "========================================"
echo ""
echo "  App:    http://localhost:8484"
echo "  Login:  admin / admin"
echo ""
echo "  Useful commands:"
echo "    podman pod ps                          # pod status"
echo "    podman ps --pod                        # all containers"
echo "    podman logs -f licserver               # license server logs"
echo "    podman logs -f o11v4-app               # app logs"
echo "    podman exec -it o11v4-app bash         # shell into app"
echo "    ./pod-stop.sh                          # stop everything"
echo ""
