#!/usr/bin/env sh
set -eu
cd /var/www/html
php artisan config:clear >/dev/null 2>&1 || true
if [ "${DB_CONNECTION:-}" = "mysql" ] && [ "${WAIT_FOR_DB:-true}" = "true" ]; then
echo "Waiting for MySQL at ${DB_HOST:-mysql}:${DB_PORT:-3306}..."
i=0
while [ "$i" -lt 60 ]; do
if php -r 'try { new PDO("mysql:host=".getenv("DB_HOST").";port=".getenv("DB_PORT").";dbname=".getenv("DB_DATABASE"), getenv("DB_USERNAME"), getenv("DB_PASSWORD"), [PDO::ATTR_TIMEOUT => 3]); exit(0);} catch (Throwable $e) { exit(1);}'; then
echo "MySQL is reachable."
break
fi
i=$((i + 1))
sleep 2
done
fi
if [ "${RUN_MIGRATIONS:-true}" = "true" ]; then
php artisan migrate --force
fi
php artisan storage:link >/dev/null 2>&1 || true
exec "$@"