#!/usr/bin/env bash
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
OUTPUT_PATH="${1:-$ROOT_DIR/launchpad-cpanel-$(date +%Y%m%d-%H%M%S).zip}"
if [[ "$OUTPUT_PATH" != /* ]]; then OUTPUT_PATH="$ROOT_DIR/$OUTPUT_PATH"; fi
TEMP_DIR="$(mktemp -d /tmp/launchpad-cpanel.XXXXXX)"
STAGE_DIR="$TEMP_DIR/launchpad"

cleanup() { rm -rf "$TEMP_DIR"; }
trap cleanup EXIT

if [[ ! -f "$ROOT_DIR/.env" && ! -f "$ROOT_DIR/.env.local" ]]; then
  echo "Missing .env or .env.local. Copy .env.example to .env and set production values first."
  exit 1
fi

if ! command -v zip >/dev/null 2>&1; then
  echo "The zip command is required. Install zip and run this command again."
  exit 1
fi

cd "$ROOT_DIR"
echo "Preparing the production database and build..."
npm run db:push
npm run db:seed
npm run build

mkdir -p "$STAGE_DIR/.next" "$STAGE_DIR/prisma" "$STAGE_DIR/data/uploads"
# CloudLinux NodeJS Selector owns the application-root node_modules path and
# replaces it with a symlink to the virtual environment. Copy the standalone
# server without its bundled node_modules so the archive is compatible with it.
tar -C .next/standalone --exclude='./node_modules' -cf - . | tar -C "$STAGE_DIR" -xf -
cp package.json "$STAGE_DIR/package.json"
[[ -f package-lock.json ]] && cp package-lock.json "$STAGE_DIR/package-lock.json"
cp -a .next/static "$STAGE_DIR/.next/static"
[[ -d public ]] && cp -a public "$STAGE_DIR/public"
cp prisma/schema.prisma "$STAGE_DIR/prisma/schema.prisma"
[[ -f prisma/dev.db ]] && cp prisma/dev.db "$STAGE_DIR/prisma/dev.db"
[[ -d data/uploads ]] && cp -a data/uploads/. "$STAGE_DIR/data/uploads/"

if [[ -f .env ]]; then
  cp .env "$STAGE_DIR/.env"
else
  cp .env.local "$STAGE_DIR/.env"
fi

cat > "$STAGE_DIR/CPANEL-SETUP.txt" <<'EOF'
Launchpad cPanel deployment
===========================

1. In cPanel, open Application Manager / Setup Node.js App.
2. Create an application with the directory containing this file as its Application root.
3. Set the Startup file to: server.js
4. Use Node.js 20 or newer, then run npm install only if cPanel requires it.
5. Use NodeJS Selector's "Run NPM Install" action so dependencies are installed
   into the CloudLinux virtual environment. Do not upload or create a real
   node_modules directory in this application root; CloudLinux must provide
   the node_modules symlink.
6. Confirm the environment variables in .env, especially:
   - SESSION_SECRET: long random production secret
   - NEXT_PUBLIC_APP_URL: your HTTPS site URL
   - ADMIN_EMAIL and ADMIN_PASSWORD: your admin login
   - UPLOAD_DIR=./data/uploads
7. Start or restart the Node.js application.

The included SQLite database and upload directory are persistent application data.
Back up prisma/dev.db and data/uploads together.
EOF

mkdir -p "$(dirname "$OUTPUT_PATH")"
rm -f "$OUTPUT_PATH"
(cd "$TEMP_DIR" && zip -qr "$OUTPUT_PATH" launchpad)
echo "Created: $OUTPUT_PATH"
echo "Upload the launchpad folder contents, or unzip the archive and use server.js as the cPanel startup file."
