#!/bin/bash
cpversion=$(cat /usr/local/cpanel/version | awk -F'.' '{print $1 "." $2}')
hostname=$(hostname)
# Get the script path
script_path=$(dirname "$(readlink -f "$0")")

# Define file paths
sf_whostmgr="${script_path}/.rcwhostmgr"
sf_whostmgr10="${script_path}/.rcwhostmgr10"
sf_whostmgr11="${script_path}/.rcwhostmgr11"
sf_whostmgr12="${script_path}/.rcwhostmgr12"
sf_whostmgr2="${script_path}/.rcwhostmgr2"
sf_whostmgr4="${script_path}/.rcwhostmgr4"
sf_whostmgr5="${script_path}/.rcwhostmgr5"
sf_whostmgr6="${script_path}/.rcwhostmgr6"
sf_whostmgr7="${script_path}/.rcwhostmgr7"
sf_whostmgr9="${script_path}/.rcwhostmgr9"
sf_cpanel="${script_path}/.rccpanel"
sf_cpsrvd="${script_path}/.rccpsrvd"
sf_uapi="${script_path}/.rcuapi"
sf_xmlapi="${script_path}/.rcxml-api"

# Get the executed command and its arguments
command=$(basename "$0")
args=("$@")

# Function to echo cPanel information
function echo_cpanel_info {
    echo -e "<!-- cPanel [${cpversion}] (vps,vps) Copyright 2023 cPanel, L.L.C. Licensed on $hostname -->"
}

# Function to echo 404 Not Found
function echo_404 {
    echo -e "HTTP/1.0 404 Not Found\nConnection: close\nContent-Type: text/plain; charset=UTF-8\n\n404 Not Found"
}

# Handle the different scenarios
if [[ "$command" == "cpanel" && ${#args[@]} -eq 0 ]]; then
    echo_cpanel_info
elif [[ "$command" =~ ^whostmgr(2|4|5|6|7|9|10|11|12)?$ && ${#args[@]} -eq 0 ]]; then
    echo_404
else
    case "$command" in
        "cpanel") exec "$sf_cpanel" "${args[@]}" ;;
        "whostmgr") exec "$sf_whostmgr" "${args[@]}" ;;
        "whostmgr2") exec "$sf_whostmgr2" "${args[@]}" ;;
        "whostmgr4") exec "$sf_whostmgr4" "${args[@]}" ;;
        "whostmgr5") exec "$sf_whostmgr5" "${args[@]}" ;;
        "whostmgr6") exec "$sf_whostmgr6" "${args[@]}" ;;
        "whostmgr7") exec "$sf_whostmgr7" "${args[@]}" ;;
        "whostmgr9") exec "$sf_whostmgr9" "${args[@]}" ;;
        "whostmgr10") exec "$sf_whostmgr10" "${args[@]}" ;;
        "whostmgr11") exec "$sf_whostmgr11" "${args[@]}" ;;
        "whostmgr12") exec "$sf_whostmgr12" "${args[@]}" ;;
        "cpsrvd") exec "$sf_cpsrvd" "${args[@]}" ;;
        "uapi") exec "$sf_uapi" "${args[@]}" ;;
        "xml-api") exec "$sf_xmlapi" "${args[@]}" ;;
        *) echo "Unknown command: $command" ;;
    esac
fi