#!/usr/bin/env bash

set -euo pipefail

project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
archive_name=Noko-Resort-cPanel.zip
archive_path="$project_dir/$archive_name"

if [[ -e "$archive_path" || -L "$archive_path" ]]; then
    printf 'Move the existing %s before rebuilding.\n' "$archive_name" >&2
    exit 1
fi

stage="$(mktemp -d "${TMPDIR:-/tmp}/noko-cpanel.XXXXXX")"
trap 'rm -rf -- "$stage"' EXIT

account_home="$stage/account-home"
backend="$account_home/noko-resort/backend"
frontend="$account_home/noko-resort/frontend"
webroot="$account_home/public_html"

mkdir -p "$backend" "$frontend" "$webroot" \
    "$backend/bootstrap/cache" "$backend/storage/app/private" "$backend/storage/app/public" \
    "$backend/storage/framework/cache/data" "$backend/storage/framework/sessions" \
    "$backend/storage/framework/views" "$backend/storage/logs"

rsync -rtp --exclude='.*' --exclude='*.sqlite*' --exclude='/bootstrap/cache/*' \
    "$project_dir/backend/app" "$project_dir/backend/bootstrap" "$project_dir/backend/config" \
    "$project_dir/backend/database" "$project_dir/backend/resources" "$project_dir/backend/routes" \
    "$backend/"
cp "$project_dir/backend/artisan" "$project_dir/backend/composer.json" "$project_dir/backend/composer.lock" "$backend/"
mkdir -p "$backend/public"
cp "$project_dir/backend/public/index.php" "$backend/public/index.php"

rsync -rtp --exclude='.*' --exclude='vite.config.js' --exclude='node_modules/' \
    --include='*/' --include='*.html' --include='*.css' --include='*.js' \
    --include='*.jpg' --include='*.jpeg' --include='*.png' --include='*.webp' --include='*.svg' \
    --include='*.mp4' --include='*.mp3' --include='*.wav' \
    --include='*.ttf' --include='*.woff' --include='*.woff2' --exclude='*' \
    "$project_dir/frontend/client" "$project_dir/frontend/admin" \
    "$project_dir/frontend/assets" "$frontend/"
mkdir -p "$frontend/assets/admin-uploads"

sed -e 's/^APP_ENV=local$/APP_ENV=production/' \
    -e 's/^APP_DEBUG=true$/APP_DEBUG=false/' \
    -e 's|http://127\.0\.0\.1:8000|https://your-domain.example|g' \
    -e 's/^LOG_LEVEL=debug$/LOG_LEVEL=warning/' \
    -e 's/^SESSION_DRIVER=database$/SESSION_DRIVER=file/' \
    -e 's/^QUEUE_CONNECTION=database$/QUEUE_CONNECTION=sync/' \
    -e 's/^CACHE_STORE=database$/CACHE_STORE=file/' \
    -e 's/^ADMIN_SESSION_COOKIE_SECURE=false$/ADMIN_SESSION_COOKIE_SECURE=true/' \
    "$project_dir/backend/.env.example" > "$backend/.env.example"

cp "$project_dir/cpanel-index.php" "$webroot/index.php"
cp "$project_dir/backend/public/.htaccess" "$project_dir/backend/public/favicon.ico" \
    "$project_dir/backend/public/robots.txt" "$webroot/"

composer --working-dir="$backend" install --no-dev --prefer-dist --optimize-autoloader \
    --no-interaction --no-progress --no-scripts

(
    cd "$account_home"
    zip -qr "$stage/$archive_name" noko-resort public_html
)
zip -T "$stage/$archive_name"
mv -- "$stage/$archive_name" "$archive_path"
printf 'Created %s\n' "$archive_path"
