#!/bin/bash

# This script performs managing of CSDAC-related services.

AZUREAD_DECORATOR_BINARY="azuread_decorator.pex"
BEE_BINARY="bee"

help() {
    cat <<EOF
Control and monitor CSDAC-related services.
Usage:
    $(basename $0) [COMMAND]
Commands:
    help                           Get this help
    debug-on <process_name>        Turn debug on for the specified process
    debug-off <process_name>       Turn debug off for the specified process
    trace-on <process_name>        Enable trace logs for the specified process
    trace-off <process_name>       Disable trace logs for the specified process
    ts-gen                         Generate troubleshoot for CSDAC related processes
EOF
}

# Enable/disable the debugger for the specified process.
debug() {
    local flag=$1
    local process=$2
    local pid=$(pmtool status | grep "$process" | grep -oE '[0-9]+(\.[0-9]+)?' | head -n1)
    if [ -z "$pid" ] ; then
        echo "No running process found with such a name: $process. Please check $process status."
        exit 1
    fi
    case "$process" in
        "")
            echo "Please provide process name." ;;
        "bee")
            debug_bee $flag ;;
        "azuread_decorator")
            debug_azuread_decorator $flag $pid ;;
        *)
            echo "Not supported process with such a name: $process. Please check the name." ;;
    esac
}

# Enable/disable the debugger for bee.
debug_bee() {
    local flag=$1
    local process="bee"
    if [ $flag == "on" ] ; then
        $BEE_BINARY debug
    else
        $BEE_BINARY debug-off
    fi
    status=$?
    if [ "$status" -eq 0 ] ; then
        echo "Debug-$flag bee."
    fi
}

# Enable/disable the debugger for azuread_decorator (old implementation).
debug_azuread_decorator() {
    local flag=$1
    local pid=$2
    local process="azuread_decorator"
    if [ $flag == "on" ] ; then
        local signal="USR1"
    else
        local signal="USR2"
    fi
    kill -"$signal" "$pid"
    status=$?
    if [ "$status" -eq 0 ] ; then
        echo "Debug-$flag $process."
    fi
}

# Enable trace logs for the specified process
trace_on() {
    local process=$1
    echo "Enabling tracing for $process..."
    case "$process" in
        "")
            echo "Please provide process name."
            ;;
        "azuread_decorator")
            $AZUREAD_DECORATOR_BINARY set_log_level DEBUG
            $AZUREAD_DECORATOR_BINARY set_log_format --from_consume --from_produce -F HUMAN_READABLE
            ;;
        *)
            echo "Not supported process with such a name: $process. Please check the name."
            ;;
    esac
}

# Disable trace logs for the specified process
trace_off() {
    local process=$1
    echo "Disabling tracing for $process..."
    if [ -z "$process" ] ; then
        echo "Please provide process name."
    elif [ "$process" == "azuread_decorator" ] ; then
        $AZUREAD_DECORATOR_BINARY set_log_level INFO
        $AZUREAD_DECORATOR_BINARY set_log_format
    else
        echo "Not supported process with such a name: $process. Please check the name."
    fi
}

# Generate troubleshoot for CSDAC related processes.
ts_gen() {
    printf "Generating troubleshoot bundle.\n"
    local current_time=$(date "+%Y.%m.%d-%H.%M.%S")
    local ts_name="ts-bundle-$current_time"
    local ts_dir=$(mktemp -d "/tmp/$ts_name-XXX")
    local status_log="$ts_dir/status.log"
    mkdir -p $ts_dir/azuread_decorator
    ts_gen_azuread_decorator $status_log $ts_dir/azuread_decorator
    local file_name="$ts_name.tar"
    mv -f $ts_dir "/tmp/$ts_name"
    tar cf $file_name -C /tmp $ts_name
    printf "Troubleshoot bundle $file_name generated.\n"
    rm -rf "/tmp/$ts_name"
}

# Generate troubleshoot for azuread_decorator.
ts_gen_azuread_decorator() {
    local status_log=$1
    local ts_dir=$2
    local enable_file=/ngfw/etc/sf/azuread_decorator.run
    if [ -f $enable_file ]; then
        printf "$enable_file file exists. Generating troubleshoot for azuread_decorator...\n\n" >> $status_log
        $AZUREAD_DECORATOR_BINARY troubleshoot $ts_dir >> $ts_dir/status.log 2>&1
    else
        printf "$enable_file file does NOT exist. The 'azuread_decorator' process is not running.\n\n" >> $status_log
    fi
}

# The 'main()' function.
case $1 in
    help | "") help ;;
    debug-on) debug "on" "$2" ;;
    debug-off) debug "off" "$2" ;;
    trace-on) trace_on "$2" ;;
    trace-off) trace_off "$2" ;;
    ts-gen) ts_gen ;;
    *)
        echo "Unknown command: $1"
        echo
        help
        exit 1
        ;;
esac
exit 0
