#!/usr/bin/env bash

UNAMEOUT="$(uname -s)"

WHITE='\033[1;37m'
NC='\033[0m'

# Verify operating system is supported
case "${UNAMEOUT}" in
    Linux*)  MACHINE=linux;;
    Darwin*) MACHINE=mac;;
    *)       MACHINE="UNKNOWN";;
esac

if [ "$MACHINE" == "UNKNOWN" ]; then
    echo -e "${WHITE}Unsupported operating system [$(uname -s)]. Evo Salo supports macOS, Linux, and Windows (WSL2).${NC}" >&2
    exit 1
fi

# Define environment variables
export APP_PORT=${APP_PORT:-80}
export APP_SERVICE=${APP_SERVICE:-"evo.test"}
export DB_PORT=${DB_PORT:-3306}
export WWWUSER=${WWWUSER:-$UID}
export WWWGROUP=${WWWGROUP:-$(id -g)}

export SALO_SHARE_DASHBOARD=${SALO_SHARE_DASHBOARD:-4040}
export SALO_SHARE_SERVER_HOST=${SALO_SHARE_SERVER_HOST:-"evo-salo.site"}
export SALO_SHARE_SERVER_PORT=${SALO_SHARE_SERVER_PORT:-8080}

# Ensure Docker is running
if ! docker info > /dev/null 2>&1; then
    echo -e "${WHITE}Docker is not running.${NC}" >&2
    exit 1
fi

# Check if Salo is currently up
PSRESULT="$(docker compose ps -q)"
if docker compose ps | grep "$APP_SERVICE" | grep 'Exit'; then
    echo -e "${WHITE}Shutting down old Salo processes...${NC}" >&2
    docker compose down > /dev/null 2>&1
    EXEC="no"
elif [ -n "$PSRESULT" ]; then
    EXEC="yes"
else
    EXEC="no"
fi

# Function: Salo is not running
function salo_is_not_running {
    echo -e "${WHITE}Salo is not running.${NC}" >&2
    echo ""
    echo -e "${WHITE}You may start Salo using the following commands:${NC} './vendor/bin/salo up' or './vendor/bin/salo up -d'" >&2
    exit 1
}

# Source the ".env" file if it exists
if [ -f ./.env ]; then
    source ./.env
fi

# Process commands
if [ $# -gt 0 ]; then
    case "$1" in
        php|bin|composer|artisan|art|test|dusk|dusk:fails|tinker|node|npm|npx|yarn)
            CMD=$1
            shift
            if [ "$EXEC" == "yes" ]; then
                docker compose exec -u salo "$APP_SERVICE" "$CMD" "$@"
            else
                salo_is_not_running
            fi
            ;;
        mysql|mariadb|psql)
            DB_CONTAINER=$1
            shift
            if [ "$EXEC" == "yes" ]; then
                case "$DB_CONTAINER" in
                    mysql)
                        docker compose exec mysql bash -c 'MYSQL_PWD=${MYSQL_PASSWORD} mysql -u ${MYSQL_USER} ${MYSQL_DATABASE}'
                        ;;
                    mariadb)
                        docker compose exec mariadb bash -c 'MYSQL_PWD=${MYSQL_PASSWORD} mysql -u ${MYSQL_USER} ${MYSQL_DATABASE}'
                        ;;
                    psql)
                        docker compose exec pgsql bash -c 'PGPASSWORD=${PGPASSWORD} psql -U ${POSTGRES_USER} ${POSTGRES_DB}'
                        ;;
                esac
            else
                salo_is_not_running
            fi
            ;;
        shell|bash|root-shell)
            shift
            if [ "$EXEC" == "yes" ]; then
                docker compose exec -u salo "$APP_SERVICE" bash
            else
                salo_is_not_running
            fi
            ;;
        redis)
            shift
            if [ "$EXEC" == "yes" ]; then
                docker compose exec redis redis-cli
            else
                salo_is_not_running
            fi
            ;;
        share)
            shift
            if [ "$EXEC" == "yes" ]; then
                docker run --init --rm -p "$SALO_SHARE_DASHBOARD:4040" \
                    -t beyondcodegmbh/expose-server:latest share \
                    http://host.docker.internal:"$APP_PORT" \
                    --server-host="$SALO_SHARE_SERVER_HOST" \
                    --server-port="$SALO_SHARE_SERVER_PORT" \
                    --auth="$SALO_SHARE_TOKEN" \
                    --subdomain="" \
                    "$@"
            else
                salo_is_not_running
            fi
            ;;
        *)
            docker compose "$@"
            ;;
    esac
else
    docker compose ps
fi
