#!/bin/bash

# Define the base directory
BASE_DIR="/abs"  # Change this to your actual base directory

# Function to install Mobile Gateway
install_mobilegateway() {
    echo "Starting Mobile Gateway installation..."
    local mobilegateway_script="$BASE_DIR/GatewayApp/mobilegateway/mobilegateway_install.sh"

    if [[ -f "$mobilegateway_script" ]]; then
        # Ensure the mobilegateway script has execute permissions
        chmod +x "$mobilegateway_script"

        # Convert to Unix format
        dos2unix "$mobilegateway_script"

        # Run the mobilegateway script
        bash "$mobilegateway_script"
    else
        echo "Error: mobilegateway_install.sh not found."
    fi
}

# Function to install API Gateway
install_apigateway() {
    echo "Starting Mobile Gateway installation..."   
    local apigateway_script="$BASE_DIR/GatewayApp/APIGateway/APIGateway_install.sh"

    if [[ -f "$apigateway_script" ]]; then
        # Ensure the apigateway script has execute permissions
        chmod +x "$apigateway_script"

        # Convert to Unix format
        dos2unix "$apigateway_script"

        # Run the apigateway script
        bash "$apigateway_script"
    else
        echo "Error: APIGateway_install.sh not found."
    fi
}

# Function to display the menu
show_menu() {
    while true; do
        clear
        echo "|---------------------------------------------------------------------------------------|"
        echo "|Setup for ABSMobileGateway - release date 01.2025                                      |"
		echo "|Setup for APIGateway - release date 01.2025                                            |"
        echo  -e "|Dedicated for ABSSolute ABSSolute slot setup -\e[92m script version 1.0 \e[0m                     |"
        echo "|Check the configuration file in /abs/mobilegateway/config/custom_properties.cfg        |"
		echo "|Check the configuration file in /abs/APIGateway/config/custom_properties.cfg           |"
        echo "|---------------------------------------------------------------------------------------|"
        echo "===================================="
        echo "1) Install Mobile Gateway"
        echo "2) Install API Gateway"
		echo
        echo "0) Exit"
        echo "===================================="
        read -rp "Choose an option (1-2). Press 0 to exit : " choice

        case $choice in
            1) install_mobilegateway ;;
            2) install_apigateway ;;
            0) echo "Exiting..."; exit 0 ;;
            *) echo "Invalid option. Please choose a number between 1 and 2 - Press 0 to exit." ;;
        esac
    done
}

# Run the menu
show_menu
