#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")"
command -v composer >/dev/null || { echo 'Composer 2 is required in PATH.'; exit 1; }
php -r 'exit(version_compare(PHP_VERSION, "8.2", ">=") && extension_loaded("pdo_sqlite") ? 0 : 1);' || { echo 'Enable PHP 8.2+ and pdo_sqlite in CLI and web PHP.'; exit 1; }
umask 077
if [[ ! -f .env ]]; then cp .env.example .env; fi
mkdir -p storage/framework/{sessions,views,cache/data} storage/logs bootstrap/cache
touch database/database.sqlite
composer install --no-dev --optimize-autoloader --no-interaction
php artisan config:clear
if ! grep -Eq '^APP_KEY=base64:.+' .env; then
  php artisan key:generate --force
fi
php artisan migrate --force
php artisan panel:admin
