#!/bin/bash
print_black() {
    echo -e "\033[30m$1\033[0m"
}

print_red() {
    echo -e "\033[31m$1\033[0m"
}

print_green() {
    echo -e "\033[32m$1\033[0m"
}

print_yellow() {
    echo -e "\033[33m$1\033[0m"
}

print_blue() {
    echo -e "\033[34m$1\033[0m"
}

print_magenta() {
    echo -e "\033[35m$1\033[0m"
}

print_cyan() {
    echo -e "\033[36m$1\033[0m"
}

print_grey() {
    echo -e "\033[37m$1\033[0m"
}

print_white() {
    echo "$1"
}

clear
PROGRAM="SecureTunnel"

service=""
mirror="https://pkg.zeroteam.top"

print_cyan "$PROGRAM manage script"

# Read parameters
{
    # Parse parameters
    while [ $# -gt 0 ]; do
        case $1 in
        --service)
            service=$2
            shift
            ;;
        --mirror)
            mirror=$2
            shift
            ;;
        *)
            print_red " Unknown parameter: $1"
            exit
            ;;
        esac
        shift
    done

    # Check parameters validity
    {
        if [ -z "$mirror" ]; then
            print_red " Parameter 'mirror' not found"
            exit 2
        fi
    }
}

# Check system
{
    print_yellow " ** Checking system info..."

    # Check systemd
    command -V systemctl >/dev/null
    if [ "$?" -ne 0 ]; then
        print_red "Not found systemd"
        exit 1
    fi
}

# Check installed program
{
    print_yellow " ** Checking installation info..."

    if [ -n "$service" ]; then
        if [ "$service" == "all" ]; then
            print_red "Service '$service' invalid!"
            exit 1
        fi

        if [ ! -d "/opt/$service" ]; then
            print_red "'/opt/$service' not exists!"
            exit 1
        fi

        if [ ! -f "/opt/$service/$PROGRAM" ]; then
            print_red "'/opt/$service' not include program!"
            exit 1
        fi
    else
        DIRS=()

        if [ ! -d "/opt" ]; then
            print_red "No installed program found, please run install script first!"
            exit 1
        fi

        for DIR in $(ls /opt); do
            [ -f "/opt/$DIR/$PROGRAM" ] && DIRS=("${DIRS[@]}" "$DIR")
        done

        case ${#DIRS[@]} in
        0)
            print_red "No installed program found, please run install script first!"
            exit 1
            ;;
        1)
            service="${DIRS[0]}"
            version=$(/opt/$service/$PROGRAM -v)

            print_white " Detected installed direction: /opt/$service (Version $version)"
            ;;
        *)
            print_white " Detected ${#DIRS[@]} installed directions, please select one:"
            for DIR in ${DIRS[@]}; do
                version=$(/opt/$DIR/$PROGRAM -v)

                print_white " - $DIR (Version $version)"
            done

            while [ -z "$service" ] || [ ! -f "/opt/$service/$PROGRAM" ]; do
                [ -n "$service" ] && print_red " Wrong input!"

                read -ep " Please type directions you want to manage: " service
            done
            ;;
        esac
    fi

    version=$(/opt/$service/$PROGRAM -v)
}

# Output menu
{
    clear

    print_cyan "$PROGRAM manage script"

    print_yellow "Current selected direction: /opt/$service (Version $version)"
    print_white ""
    print_magenta "---- Menu ----"
    print_green "1. Start program"
    print_green "2. Stop program"
    print_green "3. Restart program"
    print_green "4. View program status"
    print_magenta "---- Maintenance ----"
    print_green "5. View service and program log"
    print_green "6. Upgrade program to lastest version"
    print_green "7. Downgrade program to specialed version"
    print_green "8. Uninstall program"
    print_magenta "---- Optimize ----"
    print_green "9. Install tcp optimize config"
    print_green "10. Uninstall tcp optimize config"
    print_magenta "---- Other ----"
    print_green "0. Exit script"
    print_white ""

    while [ true ]; do
        read -ep "Please select an action: " choice

        case $choice in
        0)
            print_white "Exiting..."
            exit 0
            ;;
        1)
            systemctl start $service

            exit 0
            ;;
        2)
            systemctl stop $service

            exit 0
            ;;
        3)
            systemctl restart $service

            exit 0
            ;;
        4)
            systemctl status $service -l

            exit 0
            ;;
        5)
            journalctl -fu $service

            exit 0
            ;;
        6)
            bash <(curl -sSL "https://scripts.zeroteam.top/PortForwardGo/update_securetunnel.sh") --service $service --mirror $mirror

            exit 0
            ;;
        7)
            read -ep " Please type a version what you want to downgrade to: " version
            bash <(curl -sSL "https://scripts.zeroteam.top/PortForwardGo/update_securetunnel.sh") --service $service --mirror $mirror --version $version

            exit 0
            ;;
        8)
            bash <(curl -sSL "https://scripts.zeroteam.top/PortForwardGo/uninstall_securetunnel.sh") --service $service

            exit 0
            ;;
        9)
            bash <(curl -sSL "https://scripts.zeroteam.top/PortForwardGo/tcp.sh")

            exit 0
            ;;
        10)
            rm -f /etc/sysctl.d/98-tcp.conf
            sysctl --system 2>&1 >/dev/null

            print_green "Successfully uninstall tcp optimized config, may you need to reboot to apply some config!"
            exit 0
            ;;
        *)
            print_red " Wrong input!"
            ;;
        esac
    done
}
