#!/bin/bash

# --- Configuration ---
ServerDir=/home
GsLogFileDir="/$ServerDir/logs"
GsLogFileName=GameService_Log

# Define color codes
txtred='\033[0;31m'
txtgrn='\033[0;32m'
txtnrm='\033[0m'

# Create logs directory if it doesn't exist
mkdir -p "$GsLogFileDir"

# --- Helper Functions ---

# Function to start a service
start_service() {
    local service_name=$1
    local run_file=$2
    local config_file=$3
    local number_file=$4
    local log_file="$GsLogFileDir/$service_name.log"

    echo -e "=== [${txtred} STARTING ${txtnrm}] $service_name ==="
    cd "$ServerDir/$service_name" || { echo "Failed to change directory to $ServerDir/$service_name"; exit 1; }
 
    # Use the number_file parameter if needed
    if [ -n "$number_file" ]; then
        ./"$run_file" "$config_file" "$number_file" > "$log_file" 2>&1 &
    else
        ./"$run_file" "$config_file" > "$log_file" 2>&1 &
    fi

    echo -e "=== [${txtgrn} OK ${txtnrm}] Started $service_name ==="
    echo -e ""
    sleep 2
}

# Function to start gamed instances
start_gamed() {
  local service_name=$1
  local run_file=$2
  local config_file=$3
  local gmserver_conf=$4
  local gsalias_conf=$5
  local log_file="$GsLogFileDir/$service_name-$config_file.log"
 
   echo -e "=== [${txtred} STARTING ${txtnrm}] $service_name ($config_file) ==="
    cd "$ServerDir/$service_name" || { echo "Failed to change directory to $ServerDir/$service_name"; exit 1; }
    ./"$run_file" "$config_file" "$gmserver_conf" "$gsalias_conf" > "$log_file" 2>&1 &
    echo -e "=== [${txtgrn} OK ${txtnrm}] Started $service_name ($config_file) ==="
    echo -e ""
    sleep 2

}

# Function to stop a service
stop_service() {
    local service_name=$1
    echo -e "[${txtred} STOPPING ${txtnrm}] $service_name"
    pkill -9 "$service_name"
    echo -e "[${txtgrn} OK ${txtnrm}] Stopped $service_name"
}

# Function to check service status
status_service() {
    local service_name=$1
    if pgrep -x "$service_name" > /dev/null; then
        echo -e "[${txtgrn} RUNNING ${txtnrm}] $service_name"
    else
        echo -e "[${txtred} STOPPED ${txtnrm}] $service_name"
    fi
}

# Function to display the start menu
show_start_menu() {
    echo "======================"
    echo "Select service to start"
    echo "======================"
    echo "0. All"
    echo "1. logservices"
    echo "2. glinkd"
    echo "3. authd"
    echo "4. gdeliveryd"
    echo "5. gacd"
    echo "6. gs"
    echo "7. gfactiond"
    echo "8. uniquenamed"
    echo "9. gamedbd"
    echo "a. mono"
    echo "x. Exit"
    echo "======================"
    echo -n "Enter your choice: "
}

# Function to handle start menu selection
handle_start_option() {
    case $1 in
        1)
            echo "Starting logservices!"
            start_service "logservice" "logservice" "logservice.conf"
            ;;
        2)
            echo "Starting glinkd!"
            start_service "glinkd" "glinkd" "gamesys.conf" "1"
            #start_service "glinkd" "glinkd" "gamesys.conf" "2"
            #start_service "glinkd" "glinkd" "gamesys.conf" "3"
            #start_service "glinkd" "glinkd" "gamesys.conf" "4"
            ;;
        3)
            echo "Starting authd!"
            start_service "gauthd" "gauthd" "gamesys.conf"
            ;;
        4)
            echo "Starting gdeliveryd!"
            start_service "gdeliveryd" "gdeliveryd" "gamesys.conf"
            ;;
        5)
            echo "Starting gacd!"
            start_service "gacd" "gacd" "gamesys.conf"
            ;;
        6)
            echo "Starting gs!"
            start_gamed "gamed" "gs" "gs01" "gmserver.conf" "gsalias.conf"
            start_gamed "gamed" "gs" "is61" "gmserver.conf" "gsalias.conf"
            start_gamed "gamed" "gs" "is62" "gmserver.conf" "gsalias.conf"
            start_gamed "gamed" "gs" "is69" "gmserver.conf" "gsalias.conf"
            start_gamed "gamed" "gs" "is72" "gmserver.conf" "gsalias.conf"

            ;;
        7)
            echo "Starting gfactiond!"
            start_service "gfactiond" "gfactiond" "gamesys.conf"
            ;;
        8)
            echo "Starting uniquenamed!"
            start_service "uniquenamed" "uniquenamed" "gamesys.conf"
            ;;
        9)
            echo "Starting gamedbd!"
            start_service "gamedbd" "gamedbd" "gamesys.conf"
            ;;
    a)
        echo "Starting mono!"
        # mono doesn't need starting in the script
        ;;
        0)
            echo "Starting all services!"
            start_service "logservice" "logservice" "logservice.conf"
            start_service "gauthd" "gauthd" "gamesys.conf"
            start_service "uniquenamed" "uniquenamed" "gamesys.conf"
            start_service "gamedbd" "gamedbd" "gamesys.conf"
            start_service "gacd" "gacd" "gamesys.conf"
            start_service "gfactiond" "gfactiond" "gamesys.conf"
            start_service "gdeliveryd" "gdeliveryd" "gamesys.conf"
            start_service "glinkd" "glinkd" "gamesys.conf" "1"
            #start_service "glinkd" "glinkd" "gamesys.conf" "2"
            #start_service "glinkd" "glinkd" "gamesys.conf" "3"
            #start_service "glinkd" "glinkd" "gamesys.conf" "4"
           
            # maps
            start_gamed "gamed" "gs" "gs01" "gmserver.conf" "gsalias.conf"
            start_gamed "gamed" "gs" "is61" "gmserver.conf" "gsalias.conf"
            start_gamed "gamed" "gs" "is62" "gmserver.conf" "gsalias.conf"
            start_gamed "gamed" "gs" "is69" "gmserver.conf" "gsalias.conf"
            start_gamed "gamed" "gs" "is72" "gmserver.conf" "gsalias.conf"
            ;;
        x)
            echo "Exiting. Goodbye!"
            exit 0
            ;;
        *)
            echo "Invalid option. Please try again."
            ;;
    esac
}
# Function to display the stop menu
show_stop_menu() {
    echo "======================"
    echo "Select service to stop"
    echo "======================"
    echo "0. All"
    echo "1. logservices"
    echo "2. glinkd"
    echo "3. authd"
    echo "4. gdeliveryd"
    echo "5. gacd"
    echo "6. gs"
    echo "7. uniquenamed"
    echo "8. gamedbd"
    echo "9. mono"
    echo "x. Exit"
    echo "======================"
    echo -n "Enter your choice: "
}

# Function to handle stop menu selection
handle_stop_option() {
    case $1 in
        1)
            echo "Stop logservices!"
            stop_service "logservices"
            ;;
        2)
            echo "Stop glinkd!"
            stop_service "glinkd"
            ;;
        3)
            echo "Stop authd!"
            stop_service "authd"
            ;;
        4)
            echo "Stop gdeliveryd!"
            stop_service "gdeliveryd"
            ;;
        5)
            echo "Stop gacd!"
            stop_service "gacd"
            ;;
        6)
            echo "stop gs!"
            stop_service "gs"
            ;;
        7)
            echo "Stop gfactiond!"
            stop_service "gfactiond"
            ;;
        8)
            echo "stop uniquenamed!"
            stop_service "uniquenamed"
            ;;
        9)
            echo "Stop gamedbd!"
            stop_service "gamedbd"
            ;;
        0)
            echo "Stop all!"
            stop_service "logservices"
            stop_service "glinkd"
            stop_service "authd"
            stop_service "gdeliveryd"
            stop_service "gacd"
            stop_service "gs"
            stop_service "gfactiond"
            stop_service "uniquenamed"
            stop_service "gamedbd"
            stop_service "mono"
            ;;
        x)
            echo "Exiting. Goodbye!"
            exit 0
            ;;
        *)
            echo "Invalid option. Please try again."
            ;;
    esac
}


# --- Main Script Logic ---
case "$1" in
    "start")
        while true; do
        show_start_menu
                read choice
                handle_start_option $choice
                echo # Blank line for readability
        done
        ;;
    "stop")
        while true; do
            show_stop_menu
            read choice
            handle_stop_option $choice
            echo # Blank line for readability
      done
        ;;
    "status")
        echo "Service Status:"
        status_service "logservice"
        status_service "gauthd"
        status_service "uniquenamed"
        status_service "gamedbd"
        status_service "gacd"
        status_service "gfactiond"
        status_service "gdeliveryd"
        status_service "glinkd"
        status_service "gs"
        ;;
    *)
        echo "Usage: $0 {start|stop|status}"
        exit 1
        ;;
esac

exit 0
