#!/bin/sh
set -e

if [ "$APP_SERVER" != "local" ] && [ "$APP_ENV" != "test" ]; then
    # Load Azure and .env vars
    . /usr/local/bin/azure_env.sh
fi

echo ">>> docker-entrypoint started with args: $@"

# Start SSH in the background
if [ "$APP_ENV" != "test" ] && [ -x /usr/sbin/sshd ]; then
    echo "Starting SSH"
    /usr/sbin/sshd
fi

# If first argument starts with `-` (like a flag), prepend apache2-foreground
if [ "${1#-}" != "$1" ]; then
    set -- apache2-foreground "$@"
fi

# Run setup logic for default commands or when no args are passed
if [ -z "$1" ] || [ "$1" = 'apache2-foreground' ] || [ "$1" = 'php' ] || [ "$1" = 'bin/console' ]; then
    eval "export DATABASE_URL=\"$DATABASE_URL\""
    echo ">>> Running Symfony setup for environment: $APP_ENV - MYSQL_USER: $MYSQL_USER"

    if [ "$APP_SERVER" = "local" ]; then
        echo "Composer install..."
        composer install
    fi

    if printenv DATABASE_URL >/dev/null 2>&1; then
        echo "Waiting for DB to be ready... - MYSQL_USER: $MYSQL_USER"
        ATTEMPTS_LEFT_TO_REACH_DATABASE=60
        until [ $ATTEMPTS_LEFT_TO_REACH_DATABASE -eq 0 ] || DATABASE_ERROR=$(bin/console dbal:run-sql "SELECT 1" 2>&1); do
            sleep 1
            ATTEMPTS_LEFT_TO_REACH_DATABASE=$((ATTEMPTS_LEFT_TO_REACH_DATABASE - 1))
            echo "Still waiting for DB... ($ATTEMPTS_LEFT_TO_REACH_DATABASE attempts left)"
        done

        if [ $ATTEMPTS_LEFT_TO_REACH_DATABASE -eq 0 ]; then
            echo "The database is not up or not reachable:"
            echo "$DATABASE_ERROR"
            exit 1
        else
            echo "The db is now ready and reachable"
        fi

        # Doctrine migrations
        if [ "$(find ./migrations -iname '*.php' -print -quit)" ]; then
            echo "Execute migrations"
            bin/console doctrine:migrations:migrate --no-interaction
        fi

        echo "Setting up messenger transports"
        php bin/console messenger:setup-transports

        echo "Generate JWT keypair"
        bin/console lexik:jwt:generate-keypair --overwrite

        if [ "$APP_ENV" != "test" ]; then
            echo "Stopping messenger workers so Supervisor can restart them with fresh code"
            php bin/console messenger:stop-workers || true
        fi

        # Test environment bootstraps
        if [ "$APP_ENV" = "test" ]; then
            echo "Generate fixtures"
            composer generate-fixtures --no-interaction
        fi
    fi

    echo "Setting permissions for var/"
    setfacl -R -m u:www-data:rwX -m u:"$(whoami)":rwX var 2>/dev/null || true
    setfacl -dR -m u:www-data:rwX -m u:"$(whoami)":rwX var 2>/dev/null || true

    if [ "$APP_ENV" != "test" ] && [ -x /usr/sbin/cron ]; then
        echo "Starting Cron"
        /usr/sbin/cron
    fi

    if [ "$APP_ENV" != "test" ] && [ -x /usr/bin/supervisord ]; then
        echo "Starting Supervisor"
        /usr/bin/supervisord -c /etc/supervisor/supervisord.conf
    fi
fi

echo ">>> Starting main process: $@"
exec apache2-foreground "$@"
