#!/usr/bin/env bash
rm -f /var/lock/laravel_ready
echo "[LARAVEL-FIXER] Fixing laravel application permissions"
mkdir -p /app/storage /app/bootstrap/cache
find /app/storage -type d -exec chmod 777 {} \;
find /app/bootstrap/cache -type d -exec chmod 777 {} \;
find /app/storage -type f -not -name ".gitignore" -exec chmod 666 {} \;
find /app/bootstrap/cache -type f -exec chmod 666 {} \;

touch /app/storage/logs/laravel.log
chmod 777 -R /app/storage
chmod +x /app/artisan

php /app/artisan package:discover
if [[ ${REGENERATE_KEYS,,} == "on" ]]; then
	php /app/artisan key:generate
	php /app/artisan passport:keys --force
fi

echo "[LARAVEL-FIXER] Waiting for mysql..."
/usr/bin/wait-for-mysql
echo "[LARAVEL-FIXER] Mysql Ready, Laravel Ready."

php /app/artisan wipe

touch /var/lock/laravel_ready

# Output the laravel log to the docker terminal.
tail -n0 -f /app/storage/logs/laravel-*.log /app/storage/logs/laravel.log &

# Sleep forever (and sleep again incase the sleep process is killed)
while true; do
	sleep infinity
done