#!/bin/bash
# Copyright 2021-2024 The Wazo Authors  (see the AUTHORS file)
# SPDX-License-Identifier: GPL-3.0-or-later

select_mode() {
    if(whiptail --title "Enable/Disable debug logs" --yesno "Would you like to enable/disable the debug mode for a Wazo service?" --yes-button "Enable" --no-button "Disable"  10 70 3>&1 1>&2 2>&3) then
        whiptail --title "Enable/Disable debug logs" --msgbox \
    "Warning: enabling or disabling debug logs will restart the Wazo service. This may cause a service interruption." 10 70 3>&1 1>&2 2>&3
        select_service 0
    else
        whiptail --title "Enable/Disable debug logs" --msgbox \
    "Warning: enabling or disabling debug logs will restart the Wazo service. This may cause a service interruption." 10 70 3>&1 1>&2 2>&3
        select_service 1
    fi
}

select_service() {
    services=("wazo-auth" "wazo-plugind" "wazo-webhookd" "wazo-provd" "wazo-chatd" "wazo-confd" "wazo-dird")
    choice_cmd="whiptail --separate-output --title \"Select the Wazo services\" --checklist \"Select services to enable debug logs\" 20 100 8 "
    for service in "${services[@]}"
    do
	debug_status "$service"
	service_status=$?
	if [ $service_status = 0 ]; then
	    word="ON"
	else
	    word="OFF"
	fi
	choice_cmd+="\"$service\" \"Enable debug logs for service $service\" $word "
    done
    SERVICE=$(eval $choice_cmd 3>&1 1>&2 2>&3)
    exitstatus=$?
    if [ $exitstatus = 0 ]; then
        for services in $SERVICE; do active_debug $services $1; done
    else
        echo "Cancelled."
    fi
}

debug_status() {
    debug_file="/etc/$1/conf.d/debug.yml"
    if [ -f "$debug_file" ]; then
        if [ "$1" == "wazo-provd" ]; then
            grep 'verbose: true' "$debug_file" > /dev/null
        else
            grep 'debug: true' "$debug_file" > /dev/null
        fi
    else
        return 1
    fi
}

active_debug() {
    {
        if [ $2 = 0 ]; then
            echo -e "XXX\n00\nWriting... \nXXX"
            sleep 0.2
            if [ "$1" == "wazo-provd" ]; then
                echo "verbose: true" > /etc/$1/conf.d/debug.yml
            else
                echo "debug: true" > /etc/$1/conf.d/debug.yml
            fi
            echo -e "XXX\n50\nWriting... OK \nXXX"
            sleep 0.5
            echo -e "XXX\n50\nRestarting service... \nXXX"
            systemctl restart $1
        else
            echo -e "XXX\n00\nWriting... \nXXX"
            sleep 0.2
            if [ "$1" == "wazo-provd" ]; then
                echo "verbose: false" > /etc/$1/conf.d/debug.yml
            else
                echo "debug: false" > /etc/$1/conf.d/debug.yml
            fi
            echo -e "XXX\n50\nWriting... OK \nXXX"
            sleep 0.5
            echo -e "XXX\n50\nRestarting service... \nXXX"
            systemctl restart $1
            echo -e "XXX\n100\nRestarting service... OK \nXXX"
            sleep 0.5
        fi
    } | whiptail --gauge "Please wait..." 6 60 0
    bye $2 $1
}


bye() {
    if [ $1 = 0 ]; then
        whiptail --title "$2" --msgbox "Debug logs are enabled for service $2" 10 60
    else
        whiptail --title "$2" --msgbox "Debug logs are disabled service $2" 10 60
    fi
}

select_mode
