#!/bin/bash
set -e
cd /var/www/html
if [ -z "$APP_KEY" ] || [ "$APP_KEY" = "base64:" ]; then
export APP_KEY="base64:$(openssl rand -base64 32)"
fi
php artisan package:discover --ansi --quiet 2>/dev/null || true
php artisan config:clear --quiet 2>/dev/null || true
php artisan route:clear --quiet 2>/dev/null || true
php artisan view:clear --quiet 2>/dev/null || true
mkdir -p storage/framework/{sessions,views,cache} storage/logs bootstrap/cache database
chown -R www-data:www-data storage bootstrap/cache 2>/dev/null || true
chmod -R 775 storage bootstrap/cache 2>/dev/null || true
# Legacy SQLite (one-time import source); app data lives in PostgreSQL
touch database/database.sqlite 2>/dev/null || true
chown www-data:www-data database/database.sqlite 2>/dev/null || true
chmod 664 database/database.sqlite 2>/dev/null || true
if [ ! -f storage/oauth-private.key ]; then
php artisan passport:keys --force --quiet 2>/dev/null || true
fi
php artisan app:ensure-database --quiet 2>/dev/null || true
php artisan migrate --force --quiet 2>/dev/null || true
php artisan app:import-sqlite --quiet 2>/dev/null || true
php artisan app:sync-env-settings --quiet 2>/dev/null || true
php artisan migrate --database=audit --path=database/migrations/audit --force --quiet 2>/dev/null || true
php artisan config:cache --quiet
php artisan route:cache --quiet
php artisan view:cache --quiet
exec /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf