#!/bin/bash
# Full deployment: Docker image → Portainer stack → RPM build/publish/install
set -e

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

# Load .env
if [ -f .env ]; then
  set -a; source .env; set +a
fi

echo "========================================"
echo "  mcpctl Full Deploy"
echo "========================================"

echo ""
echo ">>> Step 1/3: Build & push mcpd Docker image"
echo ""
bash scripts/build-mcpd.sh "$@"

echo ""
echo ">>> Step 2/3: Deploy stack to production"
echo ""
bash deploy.sh

echo ""
echo ">>> Step 3/3: Build, publish & install RPM"
echo ""
bash scripts/release.sh

echo ""
echo ">>> Post-deploy: Restart mcplocal"
echo ""
systemctl --user restart mcplocal
sleep 2

echo ""
echo ">>> Post-deploy: Smoke tests"
echo ""
export PATH="$HOME/.npm-global/bin:$PATH"
if pnpm test:smoke; then
  echo "  Smoke tests passed!"
else
  echo "  WARNING: Smoke tests failed! Verify mcplocal + mcpd are healthy."
fi

echo ""
echo "========================================"
echo "  Full deploy complete!"
echo "========================================"
