#!/bin/sh
# Builds deploy/cleanmate-laravel-cpanel.zip: the CleanMate server (Laravel,
# PHP 8.3 + MySQL) with its packages, the admin dashboard, the ordering /
# driver / card pages and a ready .env to fill in. Upload and extract it in
# the cPanel home folder (see laravel_server/DEPLOY_CPANEL.md).
#
#   sh laravel_server/tools/build_cpanel.sh             (builds the dashboard too)
#   sh laravel_server/tools/build_cpanel.sh --skip-web  (uses admin_web/build/web as it is)
set -e
cd "$(dirname "$0")/../.."
ROOT=$(pwd)
OUT=deploy/laravel-cpanel/build
APP="$OUT/cleanmatepro"
rm -rf "$OUT" && mkdir -p "$APP"

# The release number shown by /api/health comes from schema.json: all must agree.
RELEASE=$(grep '^version:' admin_web/pubspec.yaml | cut -d' ' -f2 | cut -d+ -f1)
SCHEMA=$(python3 -c "import json;print(json.load(open('shared/schema.json'))['version'])")
[ "$RELEASE" = "$SCHEMA" ] || { echo "Version mismatch: admin_web $RELEASE, schema.json $SCHEMA (run: cd shared && dart run tool/export_schema.dart)"; exit 1; }

if [ "$1" != "--skip-web" ]; then
  (cd admin_web && flutter build web --release --no-web-resources-cdn --no-tree-shake-icons)
  # The icon font gets a new address each release so browsers never reuse an older copy.
  V=$(grep '^version:' admin_web/pubspec.yaml | cut -d' ' -f2 | tr '+' '-')
  sed -i '' "s#\"fonts/MaterialIcons-Regular.otf\"#\"fonts/MaterialIcons-Regular.otf?v=$V\"#" admin_web/build/web/assets/FontManifest.json
fi
[ -f admin_web/build/web/index.html ] || { echo "admin_web/build/web is missing: run without --skip-web"; exit 1; }

# The Laravel app without local files (.env, logs, caches, data).
rsync -a --exclude .env --exclude 'storage/logs/*' --exclude 'storage/framework/cache/*' --exclude 'storage/framework/sessions/*' \
  --exclude 'storage/framework/views/*' --exclude 'storage/app/*' --exclude vendor --exclude node_modules --exclude tests --exclude .phpunit.cache \
  laravel_server/ "$APP/"
mkdir -p "$APP/storage/app" "$APP/storage/logs" "$APP/storage/framework/cache" "$APP/storage/framework/sessions" "$APP/storage/framework/views"

# Packages for PHP 8.3, without the development tools.
(cd "$APP" && composer install --no-dev --optimize-autoloader --no-interaction --no-progress --quiet)

# What the server reads at run time: the table definitions, the website
# content and the pages and fonts it serves.
mkdir -p "$APP/resources/dinepad/assets"
cp shared/schema.json "$APP/resources/dinepad/schema.json"
python3 laravel_server/tools/export_website.py >/dev/null && cp laravel_server/resources/dinepad/website.json "$APP/resources/dinepad/website.json"
for d in fonts order driver laundry card products wallet tutorials site; do cp -R "server/assets/$d" "$APP/resources/dinepad/assets/$d"; done

# The dashboard next to index.php (Apache serves its files directly).
cp -R admin_web/build/web/. "$APP/public/"
# Only the CanvasKit engine is used (a JavaScript build): no debug symbols or
# WebAssembly-build engines in the package.
find "$APP/public" -name '*.symbols' -delete
rm -rf "$APP/public"/canvaskit/skwasm* "$APP/public"/canvaskit/wimp.* "$APP/public"/canvaskit/webparagraph
rm -f "$APP"/public/*.apk "$APP"/public/*.exe "$APP"/public/*.zip # app downloads are uploaded separately

# A .env to fill in (database name, user and password), with its own key.
KEY="base64:$(head -c 32 /dev/urandom | base64)"
sed "s#^APP_KEY=.*#APP_KEY=$KEY#" laravel_server/.env.cpanel > "$APP/.env"

chmod -R u+rwX,go+rX "$APP"
chmod -R 775 "$APP/storage" "$APP/bootstrap/cache"
rm -f deploy/cleanmate-laravel-cpanel.zip
(cd "$OUT" && zip -qr "$ROOT/deploy/cleanmate-laravel-cpanel.zip" cleanmatepro)
# The empty database for cPanel → phpMyAdmin → Import (DEPLOY_CPANEL.md step 3b).
# After a table change: sh laravel_server/tools/make_sql.sh (needs a local MySQL / MariaDB).
cp laravel_server/database/sql/cleanmate.sql deploy/cleanmate-database.sql
ls -lh deploy/cleanmate-laravel-cpanel.zip deploy/cleanmate-database.sql
