#!/usr/bin/env bash
rm -f /var/lock/laravel_horizon_started

# If horizon is present, publish the frontend assets, if HORIZON_UI is set to "on"
if [[ ${HORIZON_ENABLE,,} == "on" ]]; then
	if [[ -f "/app/config/horizon.php" ]]; then
		if [[ ${MIGRATE_ENABLE} == "on" ]]; then
			echo "[HORIZON] Waiting until Migration Complete."
			until [[ -f /var/lock/laravel_migration_complete ]]; do
				sleep 1
			done
			echo "[HORIZON] Migration is complete, running Horizon."
		fi
		if [[ ${HORIZON_UI,,} == "on" ]]; then
			echo "[HORIZON] Publishing horizon frontend assets"
			php /app/artisan horizon:publish
		fi
		echo "[HORIZON] Running laravel horizon runner"
		cpulimit -l 30 -- php /app/artisan horizon
		touch /var/lock/laravel_horizon_started
	else
		echo "[HORIZON] Horizon is not present."
	fi
else
	echo "[HORIZON] Not enabled. To enable this feature, set HORIZON_ENABLE = on."
fi

sleep infinity