#!/usr/bin/env bash
# ==============================================================================
# Radius CLI Engine - Modular Git-Driven Deployment Manager
# Developed by: Codelxior (https://github.com)
# License: MIT
# ==============================================================================

set -e

# Terminal Style and Color Configurations
BOLD='\e[1m'            # Bold text style
DIM='\e[2m'             # Dim/Faint text style
UNDERLINE='\e[4m'       # Underlined text style
BLINK='\e[5m'           # Blinking text style

# Dark / Standard Colors (Low-Intensity)
DARK_RED='\e[31m'       # Dark Red
DARK_GREEN='\e[32m'     # Dark Green
DARK_YELLOW='\e[33m'    # Dark Yellow
DARK_BLUE='\e[34m'      # Dark Blue
DARK_MAGENTA='\e[35m'   # Dark Magenta
DARK_CYAN='\e[36m'      # Dark Cyan
DARK_WHITE='\e[37m'     # Light Grey

# Light / Bright Colors (High-Intensity)
LIGHT_RED='\e[91m'      # Bright Red (Errors)
LIGHT_GREEN='\e[92m'    # Bright Green (Success tags)
LIGHT_YELLOW='\e[93m'   # Bright Yellow (Warnings)
LIGHT_BLUE='\e[94m'     # Bright Blue (System info)
LIGHT_MAGENTA='\e[95m'  # Bright Magenta
LIGHT_CYAN='\e[96m'     # Bright Cyan (Data streams)
LIGHT_WHITE='\e[97m'    # Absolute White

# END Colors
NC='\e[0m'


# Set absolute script path here.
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
# Absolute path to home directory.
DIR_HOME="$(readlink -f "${SCRIPT_DIR}/..")"
# Absolute path to .config directory
DIR_CONFIG="${DIR_HOME}/config"
# Absolute path to library directory
DIR_LIB="${DIR_HOME}/include"
# Absolute path to logs directory
DIR_LOG="${DIR_HOME}/logs"

# File log.
FILE_LOG="${DIR_LOG}/$(date '+%d%m%Y').log"


[ -f "${DIR_CONFIG}/main" ] && source "${DIR_CONFIG}/main" || { 
    echo -e "Fatal Error: ${LIGHT_RED}Failed to locate main configuration file. ${DIR_CONFIG}/main${NC}"; exit 1; 
}

[ -f "${DIR_LIB}/helper.inc" ] && source "${DIR_LIB}/helper.inc" || { 
    echo -e "Fatal Error: ${LIGHT_RED}Failed to locate library file. ${DIR_LIB}/helper.inc${NC}"; exit 1; 
}

[ -f "${DIR_LIB}/function.inc" ] && source "${DIR_LIB}/function.inc" || { 
    echo -e "Fatal Error: ${LIGHT_RED}Failed to locate library file. ${DIR_LIB}/function.inc\e[0m"; exit 1; }


[ ! -d "${DIR_LOG}" ] && { mkdir "${DIR_LOG}"; chmod 775 "${DIR_LOG}"; }

[ ! -f "${FILE_LOG}" ] && { touch "${FILE_LOG}"; chmod 664 "${FILE_LOG}"; function_log "info: ${DARK_GREEN}New log file created.${NC}"; }



MODE_QUIET=false
ARGS_CLEAN=()

for arg in "$@"; do
    
    ARG_LOWER=$(function_strtolower "$arg")
    
    if [ "$ARG_LOWER" == "--quiet" ] || [ "$ARG_LOWER" == "-q" ]; then
    
        MODE_QUIET=true
    
    else
    
        ARGS_CLEAN+=("$arg")
    fi

done

set -- "${ARGS_CLEAN[@]}"



if [ -z "$1" ]; then
    
    function_printhelp

    exit 0

fi

PHP_BIN=""
COMPOSER_BIN=""
NODE_BIN=""
NPM_BIN=""

function_app_init

case "$1" in

    update)
        
        function_app_update
        ;;

    list-branch)
        
        function_app_list_branches
        ;;

    list-release)
        
        function_app_list_release
        ;;

    --)

        shift;
        ;;

    *)

        echo -e "${LIGHT_RED}Unknown operations command: $1${NC}"
        function_printhelp
        exit 1
        ;;

esac