#!/bin/sh
# Deploy NFC-Admin on cPanel subdomain path from zip.
# Run as root (or account owner) on the server.
#
# Expected layout:
#   /home/erupaiin/public_html/nfc.admin.erupai.com/          ← Apache docroot
#   /home/erupaiin/public_html/nfc.admin.erupai.com/code/NFC-Admin.zip
#
# Modes:
#   sh deploy-from-zip.sh static   — unzip, build, copy dist → docroot (Apache serves site)
#   sh deploy-from-zip.sh podman   — unzip, build container, run on :8210 (proxy subdomain)
set -eu

if grep -q "$(printf '\r')" "$0" 2>/dev/null; then
  sed -i 's/\r$//' "$0"
  exec sh "$0" "$@"
fi

MODE="${1:-static}"
USER_HOME="${NFC_CPANEL_HOME:-/home/erupaiin}"
SITE_ROOT="${NFC_ADMIN_SITE:-$USER_HOME/public_html/nfc.admin.erupai.com}"
CODE_DIR="$SITE_ROOT/code"
ZIP="$CODE_DIR/NFC-Admin.zip"
APP_DIR="$CODE_DIR/NFC-Admin"
API_URL="${VITE_API_BASE_URL:-http://66.116.206.165:8201/api/v1}"

echo "SITE_ROOT=$SITE_ROOT"
echo "ZIP=$ZIP"
echo "MODE=$MODE"
echo "VITE_API_BASE_URL=$API_URL"

test -f "$ZIP" || { echo "ERROR: missing $ZIP — upload NFC-Admin.zip first"; exit 1; }

echo "==> Unzip"
rm -rf "$APP_DIR"
mkdir -p "$CODE_DIR"
unzip -qo "$ZIP" -d "$CODE_DIR"

# Zip may extract as NFC-Admin/ or flat files
if [ ! -d "$APP_DIR" ]; then
  # find package.json under code/
  FOUND="$(find "$CODE_DIR" -maxdepth 3 -name package.json -print | head -n1 || true)"
  if [ -n "$FOUND" ]; then
    APP_DIR="$(dirname "$FOUND")"
  fi
fi
test -f "$APP_DIR/package.json" || { echo "ERROR: package.json not found under $CODE_DIR"; exit 1; }
echo "APP_DIR=$APP_DIR"

cd "$APP_DIR"

if [ "$MODE" = "podman" ]; then
  echo "==> Podman build + run (8210)"
  export VITE_API_BASE_URL="$API_URL"
  podman-compose -f compose.prod.yaml -f compose.prod.cpanel.yaml down 2>/dev/null || true
  podman rm -f eyerexus-admin 2>/dev/null || true
  podman-compose -f compose.prod.yaml -f compose.prod.cpanel.yaml up -d --build
  sleep 5
  ss -tlnp | grep 8210 || { podman logs eyerexus-admin --tail 40; exit 1; }
  echo "DONE — Podman Admin on :8210 (point subdomain ProxyPass to 127.0.0.1:8210)"
  echo "Site: https://nfc.admin.erupai.com"
  exit 0
fi

echo "==> Static build (Node) → Apache docroot"
if command -v npm >/dev/null 2>&1; then
  npm ci --registry=https://registry.npmjs.org/ || npm install --registry=https://registry.npmjs.org/
  VITE_API_BASE_URL="$API_URL" npm run build
else
  echo "npm not found — building with podman then copying dist"
  export VITE_API_BASE_URL="$API_URL"
  podman build -t eyerexus-admin-build \
    --build-arg "VITE_API_BASE_URL=$API_URL" \
    -f Containerfile .
  CID="$(podman create eyerexus-admin-build)"
  mkdir -p "$APP_DIR/dist"
  podman cp "$CID:/usr/share/nginx/html/." "$APP_DIR/dist/"
  podman rm "$CID" >/dev/null
fi

echo "==> Publish dist → $SITE_ROOT"
# Keep code/ and any hidden cPanel files; replace site assets
find "$SITE_ROOT" -mindepth 1 -maxdepth 1 ! -name code ! -name .well-known -exec rm -rf {} +
cp -a "$APP_DIR/dist/." "$SITE_ROOT/"
# Ensure SPA rewrite rules
if [ -f "$APP_DIR/public/.htaccess" ]; then
  cp -f "$APP_DIR/public/.htaccess" "$SITE_ROOT/.htaccess"
elif [ -f "$APP_DIR/dist/.htaccess" ]; then
  cp -f "$APP_DIR/dist/.htaccess" "$SITE_ROOT/.htaccess"
fi

echo "DONE — open https://nfc.admin.erupai.com"
echo "API: $API_URL"
