#!/bin/sh
set -e

echo "Waiting for database connection..."
until php -r "try { new PDO('mysql:host=${DB_HOST};port=${DB_PORT}', '${DB_USERNAME}', '${DB_PASSWORD}'); echo 'ok'; } catch(PDOException \$e) { echo \$e->getMessage(); exit(1); }" 2>/dev/null | grep -q 'ok'; do
    sleep 3
done
echo "Database connected."

php artisan migrate --force
php artisan storage:link --force || true

if [ "${APP_ENV}" = "production" ]; then
    php artisan config:cache
    php artisan view:cache
fi

exec php-fpm
