#!/bin/bash

# Debug log function
log_debug() {
    local message="$1"
    debug_logs+=("$message")
}

# Initialize debug logs array
declare -a debug_logs=()

# Get query
QUERY_STRING=$(echo "${QUERY_STRING}" | sed 's/;//g')
if [ "${QUERY_STRING}" ]; then
    export IFS="&"
    for cmd in ${QUERY_STRING}; do
        if [ "$(echo $cmd | grep '=')" ]; then
            key=$(echo $cmd | awk -F '=' '{print $1}')
            value=$(echo $cmd | awk -F '=' '{print $2}')
            eval $key=$value
            log_debug "Received parameter: $key=$value"
        fi
    done
fi

setTTL=$(printf '%b\n' "${ttlvalue//%/\\x}")
if [ -n "${setTTL}" ]; then
    log_debug "Stopping service to remove rules"
    /usrdata/simplefirewall/ttl-override stop

    # Convert ttlvalue to integer
    if [ "${ttlvalue}" ]; then
        ttlvalue_int=$(echo "${ttlvalue}" | sed 's/[^0-9]//g')
        log_debug "Converted ttlvalue to integer: $ttlvalue_int"
    fi

    # Call the sh script with the appropriate parameters
    if [ "${ttlvalue_int}" != 0 ]; then
        log_debug "Enabling TTL with value: ${setTTL}"
        /usrdata/simpleadmin/script/ttl_script.sh enable "${setTTL}"
        commandRan="/usrdata/simpleadmin/script/ttl_script.sh enable ${setTTL}"
        ttlenabled=true
        ttlvalue=$ttlvalue_int
    elif [ "${ttlvalue_int}" = 0 ]; then
        log_debug "Disabling TTL"
        /usrdata/simpleadmin/script/ttl_script.sh disable 0
        commandRan="/usrdata/simpleadmin/script/ttl_script.sh disable 0"
        ttlenabled=false
        ttlvalue=0
    fi

    log_debug "Starting service to apply rules"
    /usrdata/simplefirewall/ttl-override start
fi

echo "Content-type: text/text"
echo ""
cat <<EOT
{
    "debug_logs": [
        $(printf '"%s",' "${debug_logs[@]}")
    ]
}
EOT