#!/usr/bin/env bash
# First-time install on a server. Usage:  bash deploy/install.sh /var/www/dms
# Needs: PHP 8.2+ (bcmath gd mbstring openssl pdo curl fileinfo zip xml), Composer 2, Node 20+, npm, rsync, and internet access.
set -euo pipefail

TARGET="${1:?Usage: bash deploy/install.sh /var/www/dms}"
SRC="$(cd "$(dirname "$0")/.." && pwd)"
say() { printf '\n\033[1m== %s\033[0m\n' "$*"; }
need() { command -v "$1" >/dev/null 2>&1 || { echo "Missing program: $1"; exit 1; }; }

say "Checking the server"
for p in php composer node npm rsync; do need "$p"; done
php -r 'exit(version_compare(PHP_VERSION, "8.2.0", ">=") ? 0 : 1);' || { echo "PHP 8.2 or newer is required (found $(php -r 'echo PHP_VERSION;'))"; exit 1; }
for e in bcmath gd mbstring openssl pdo curl fileinfo zip xml; do php -m | grep -qix "$e" || { echo "PHP extension missing: $e"; exit 1; }; done
[ -f "$TARGET/artisan" ] && { echo "$TARGET already holds an install. Use deploy/update.sh to upgrade it."; exit 1; }

say "Creating the Laravel 12 application in $TARGET"
mkdir -p "$TARGET"
composer create-project "laravel/laravel:^12.0" "$TARGET" --prefer-dist --no-interaction
cd "$TARGET"

say "Adding the dealership system"
rm -f database/migrations/0001_01_01_000000_create_users_table.php resources/views/welcome.blade.php   # replaced by our users, roles and sessions tables
rsync -a "$SRC/app/" app/ && rsync -a "$SRC/bootstrap/" bootstrap/ && rsync -a "$SRC/config/" config/ && rsync -a "$SRC/database/" database/
rsync -a "$SRC/public/" public/ && rsync -a "$SRC/resources/" resources/ && rsync -a "$SRC/routes/" routes/ && rsync -a "$SRC/tests/" tests/
mkdir -p docs && rsync -a "$SRC/docs/" docs/ && rsync -a "$SRC/deploy/" deploy/

say "Installing libraries (QR codes, barcodes, PDF, Excel)"
composer require bacon/bacon-qr-code picqer/php-barcode-generator barryvdh/laravel-dompdf phpoffice/phpspreadsheet --no-interaction
composer install --no-dev --optimize-autoloader --no-interaction

say "Building the website styles and scripts"
npm install alpinejs
npm install
npm run build

say "Preparing settings"
if [ ! -f .env ]; then cp deploy/.env.production.example .env; fi
php artisan key:generate --force
mkdir -p storage/app/private && chmod -R ug+rwX storage bootstrap/cache

cat <<DONE

Installed. Now:
  1. Edit $TARGET/.env : database, APP_URL (https), DMS_ADMIN_EMAIL, DMS_ADMIN_PASSWORD, SITE_*, MPESA_*, ETIMS_*
  2. Create the database and user, then:     cd $TARGET && php artisan migrate --force --seed
  3. Change the sample branches (HQ, NBO, MSA) to your real ones under Branches, and sign in as the admin you set in .env
  4. Web server:      deploy/nginx.conf        (point the root at $TARGET/public, add HTTPS)
  5. Queue worker:    deploy/supervisor-dms-worker.conf
  6. Scheduler cron:  deploy/crontab.txt
  7. Optimise:        php artisan config:cache && php artisan route:cache && php artisan view:cache && php artisan event:cache
  8. Check:           php artisan dms:preflight      (and again with --live before real customers)
Full guide: $TARGET/docs/DEPLOYMENT.md
DONE
