#!/usr/bin/env bash
#
# Windows (Git Bash / MSYS) friendly Sail entrypoint.
# On macOS / Linux / WSL, this delegates to vendor/bin/sail.
#

set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$ROOT"

UNAMEOUT="$(uname -s)"

case "${UNAMEOUT}" in
    Linux*|Darwin*)
        exec "$ROOT/vendor/bin/sail" "$@"
        ;;
esac

# --- Git Bash / MSYS / Cygwin path ---
# docker compose reads `.env` for compose variable substitution.

export APP_SERVICE="${APP_SERVICE:-laravel.test}"
# Windows bind mounts are often only writable as root inside the container.
export APP_USER="${APP_USER:-root}"
export NPM_USER="${NPM_USER:-root}"
export WWWUSER="${WWWUSER:-0}"
export WWWGROUP="${WWWGROUP:-0}"
export SAIL_DOCKER_BINARY="${SAIL_DOCKER_BINARY:-docker}"

if ${SAIL_DOCKER_BINARY} compose version >/dev/null 2>&1; then
    COMPOSE=(${SAIL_DOCKER_BINARY} compose)
else
    COMPOSE=(${SAIL_DOCKER_BINARY}-compose)
fi

if ! ${SAIL_DOCKER_BINARY} info >/dev/null 2>&1; then
    echo "Docker is not running." >&2
    exit 1
fi

is_running() {
    local ids
    ids="$("${COMPOSE[@]}" ps -q 2>/dev/null || true)"
    [ -n "$ids" ]
}

require_running() {
    if ! is_running; then
        echo "Sail is not running." >&2
        echo "Start it with: ./sail up -d" >&2
        exit 1
    fi
}

if [ $# -eq 0 ] || [ "$1" = "help" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
    cat <<'EOF'
Laravel Sail (Windows Git Bash wrapper)

Usage:
  ./sail up -d                 Start containers
  ./sail stop                  Stop containers
  ./sail down                  Stop and remove containers
  ./sail ps                    Show container status
  ./sail build [--no-cache]    Build images
  ./sail logs [-f]             View logs
  ./sail artisan ...           Run Artisan
  ./sail composer ...          Run Composer
  ./sail npm ...               Run npm
  ./sail test ...              Run PHPUnit via Artisan
  ./sail shell                 Bash into the app container
  ./sail root-shell            Root bash into the app container
  ./sail tinker                Open Tinker
  ./sail mysql                 MySQL CLI
  ./sail redis                 Redis CLI

Other arguments are passed to docker compose.
EOF
    exit 0
fi

cmd="$1"
shift || true

case "$cmd" in
    up|down|stop|restart|ps|build|pull|images|port|top|kill|rm|config|events|version)
        exec "${COMPOSE[@]}" "$cmd" "$@"
        ;;
    logs)
        exec "${COMPOSE[@]}" logs "$@"
        ;;
    artisan|art)
        require_running
        exec "${COMPOSE[@]}" exec -u "$APP_USER" "$APP_SERVICE" php artisan "$@"
        ;;
    php)
        require_running
        exec "${COMPOSE[@]}" exec -u "$APP_USER" "$APP_SERVICE" php "$@"
        ;;
    composer)
        require_running
        exec "${COMPOSE[@]}" exec -u "$APP_USER" "$APP_SERVICE" composer "$@"
        ;;
    npm)
        require_running
        exec "${COMPOSE[@]}" exec -u "$NPM_USER" "$APP_SERVICE" npm "$@"
        ;;
    npx)
        require_running
        exec "${COMPOSE[@]}" exec -u "$NPM_USER" "$APP_SERVICE" npx "$@"
        ;;
    node)
        require_running
        exec "${COMPOSE[@]}" exec -u "$NPM_USER" "$APP_SERVICE" node "$@"
        ;;
    test)
        require_running
        exec "${COMPOSE[@]}" exec -u "$APP_USER" "$APP_SERVICE" php artisan test "$@"
        ;;
    pint)
        require_running
        exec "${COMPOSE[@]}" exec -u "$APP_USER" "$APP_SERVICE" vendor/bin/pint "$@"
        ;;
    shell|bash)
        require_running
        exec "${COMPOSE[@]}" exec -u "$APP_USER" "$APP_SERVICE" bash
        ;;
    root-shell|root-bash)
        require_running
        exec "${COMPOSE[@]}" exec -u root "$APP_SERVICE" bash
        ;;
    tinker)
        require_running
        exec "${COMPOSE[@]}" exec -u "$APP_USER" "$APP_SERVICE" php artisan tinker
        ;;
    mysql)
        require_running
        exec "${COMPOSE[@]}" exec mysql bash -c "MYSQL_PWD=\${MYSQL_PASSWORD} mysql -u \${MYSQL_USER} \${MYSQL_DATABASE}"
        ;;
    redis)
        require_running
        exec "${COMPOSE[@]}" exec redis redis-cli
        ;;
    bin)
        require_running
        exec "${COMPOSE[@]}" exec -u "$APP_USER" "$APP_SERVICE" ./vendor/bin/"$@"
        ;;
    run)
        require_running
        exec "${COMPOSE[@]}" exec -u "$APP_USER" "$APP_SERVICE" "$@"
        ;;
    *)
        exec "${COMPOSE[@]}" "$cmd" "$@"
        ;;
esac
