#!/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 "Unsupported operating system [$(uname -s)]. Django Djando supports macOS, Linux, and Windows (WSL2)." >&2

    exit 1
fi

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

if [ "$MACHINE" == "linux" ]; then
    export SEDCMD="sed -i"
elif [ "$MACHINE" == "mac" ]; then
    export SEDCMD="sed -i .bak"
fi

if [ $# -gt 0 ]; then
    # Copy docker-composer.yml and runtimes to current directory
    if [ "$1" == "init" ]; then
      shift 1

      djandoinit

      exit 0
    fi
fi

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

    exit 1
fi

# Determine if Djando is currently up...
PSRESULT="$(docker-compose ps -q)"

if docker-compose ps | grep 'Exit'; then
    echo -e "${WHITE}Shutting down old Djando processes...${NC}" >&2

    docker-compose down > /dev/null 2>&1

    EXEC="no"
elif [ -n "$PSRESULT" ]; then
    EXEC="yes"
else
    EXEC="no"
fi

# Function that outputs Djando is not running...
function djando_is_not_running {
    echo -e "${WHITE}Djando is not running.${NC}" >&2
    echo "" >&2
    echo -e "${WHITE}You may Djando using the following commands:${NC} 'djando up' or '.djando up -d'" >&2

    exit 1
}

if [ $# -gt 0 ]; then
    # Source the ".env" file so Django's environment variables are available...
    if [ -f ./.env ]; then
        source ./.env
    fi

    # Proxy Python commands to the "python" binary on the application container...
    if [ "$1" == "python" ]; then
        shift 1

        if [ "$EXEC" == "yes" ]; then
            docker-compose exec \
                -u djando \
                "$APP_SERVICE" \
                bash -c 'source venv/bin/activate && python "$@"' bash "$@"
        else
            djando_is_not_running
        fi

    # Proxy Pip commands to the "pip" binary on the application container...
    elif [ "$1" == "pip" ]; then
        shift 1

        if [ "$EXEC" == "yes" ]; then
            docker-compose exec \
                -u djando \
                "$APP_SERVICE" \
                bash -c 'source venv/bin/activate && pip "$@"' bash "$@"
        else
            djando_is_not_running
        fi

    # Proxy django-admin commands to the "django-admin" binary on the application container...
    elif [ "$1" == "django-admin" ]; then
        shift 1

        if [ "$EXEC" == "yes" ]; then
            docker-compose exec \
                -u djando \
                "$APP_SERVICE" \
                bash -c 'source venv/bin/activate && django-admin "$@"' bash "$@"
        else
            djando_is_not_running
        fi

    # Proxy manage commands to the "python manage.py" on the application container...
    elif [ "$1" == "manage" ]; then
        shift 1

        if [ "$EXEC" == "yes" ]; then
            docker-compose exec \
                -u djando \
                "$APP_SERVICE" \
                bash -c 'source venv/bin/activate && python manage.py "$@"' bash "$@"
        else
            djando_is_not_running
        fi

    # Initiate a PostgreSQL CLI terminal session within the "pgsql" container...
    elif [ "$1" == "psql" ]; then
        shift 1

        if [ "$EXEC" == "yes" ]; then
            docker-compose exec \
                 pgsql \
                 bash -c 'PGPASSWORD=${PGPASSWORD} psql -U ${POSTGRES_USER} ${POSTGRES_DB}'
        else
            djando_is_not_running
        fi

    # Initiate a Bash shell within the application container...
    elif [ "$1" == "shell" ] || [ "$1" == "bash" ]; then
        shift 1

        if [ "$EXEC" == "yes" ]; then
            docker-compose exec \
                -u djando \
                "$APP_SERVICE" \
                bash -c 'source venv/bin/activate && bash'
        else
            djando_is_not_running
        fi

    # Initiate a root user Bash shell within the application container...
    elif [ "$1" == "root-shell" ] ; then
        shift 1

        if [ "$EXEC" == "yes" ]; then
            docker-compose exec \
                "$APP_SERVICE" \
                bash -c 'source venv/bin/activate && bash'
        else
            djando_is_not_running
        fi

    # Pass unknown commands to the "docker-compose" binary...
    else
        docker-compose "$@"
    fi
else
    docker-compose ps
fi