#!/usr/bin/env bash
###############################################################################
# pod-stop.sh — Stop and remove the o11v4 pod and all its containers
#
# Usage:   ./pod-stop.sh
#          ./pod-stop.sh --pod my-pod
#          ./pod-stop.sh --remove-image   # also delete the built image
###############################################################################

set -euo pipefail

POD_NAME="o11v4-pod"
IMAGE_NAME="o11v4"
REMOVE_IMAGE=false

while [[ $# -gt 0 ]]; do
    case "$1" in
        --pod)           POD_NAME="$2";   shift 2 ;;
        --image)         IMAGE_NAME="$2"; shift 2 ;;
        --remove-image)  REMOVE_IMAGE=true; shift ;;
        *) echo "Unknown option: $1"; exit 1 ;;
    esac
done

echo ""
echo "Stopping pod '$POD_NAME'..."

# ── Stop and remove the pod (takes all containers with it) ──────────────────
podman pod stop "$POD_NAME" 2>/dev/null || true
podman pod rm -f "$POD_NAME" 2>/dev/null || true

echo "Pod stopped and removed."

# ── Optionally remove the image ─────────────────────────────────────────────
if [ "$REMOVE_IMAGE" = true ]; then
    echo "Removing image '$IMAGE_NAME'..."
    podman rmi "$IMAGE_NAME" 2>/dev/null || true
    echo "Image removed."
fi

echo ""
echo "Done. To start again:  ./pod-start.sh"
echo ""
