#!/usr/bin/bash
#
#  Copyright (C) 2002-20 - ntop.org
#
#  http://www.ntop.org/
#

# Global variable used to check if ntopng exists
CONF_NTOPNG="false"
CONFIG_NAME="nprobe"

NPROBE_CONFIG_PATH="/etc/nprobe"
NTOPNG_CONFIG_PATH="/etc/ntopng"

NPROBE_STD_NAME="nprobe.conf"
NTOPNG_STD_NAME="ntopng.conf"

TEMPLATE_HOME="/usr/share/nprobe/packages/wizard/templates"

##############################

LAN_INTERFACE=
WAN_INTERFACE=

if [ -f /etc/armbian-release ]; then
    LAN_INTERFACE="eth1"
    WAN_INTERFACE="eth0"
fi

##############################

# Exit function
function checkExit {
    {
	if (whiptail --title "Exit" --yesno "Do you want to exit?" 8 78); then
    	    exit
	else
    	    mainMenu
	fi
    }
}

# Go back function, return to the main menu
function goBack {
    {
	mainMenu
    }
}

# Function used to name the collector
function nameCollector {
    {
	CONFIG_NAME="nprobe"

	while true; do
	    NAME=$(whiptail --inputbox "Choose the name for the configuration. \n(e.g. configtest)" 12 80 --title "Configuration Name" 3>&1 1>&2 2>&3)

	    exitstatus=$?
	    if [ "$exitstatus" = "1" ]; then
	    	createMenu
	    fi
	    if [ "$NAME" = "" ]; then
		TMP_NAME="$CONFIG_NAME"
	    else
		TMP_NAME="$CONFIG_NAME-$NAME"
	    fi
	    
	    if [ ! -e "$NPROBE_CONFIG_PATH/$TMP_NAME.conf" ]; then
		CONFIG_NAME=$TMP_NAME
		break
	    fi

	    whiptail --title "Report" --msgbox "Please choose an other name. Configuration named $TMP_NAME already exists." 12 80
	done
    }
}

# Function used to forcibly nameing the collector
function checkForcedName {
	{
		CONFIG_NAME="nprobe-$1"
		if [ -e "$NPROBE_CONFIG_PATH/$CONFIG_NAME.conf" ]; then
			if (whiptail --title "Warning" --yesno "Configuration file for the same Interface found, the old configuration file will be override, are you sure to continue?" 8 78); then
            			rm "$NPROBE_CONFIG_PATH/$CONFIG_NAME.conf"
        		else
				whiptail --title "Report" --msgbox "Configuration nullified, returning to the Main Menu." 12 80
            			mainMenu
        		fi
		fi
	}
}

# Check if requesting the configuration of ntopng too
function checkNtopngExists {
    {
	if [ "$1" = "127.0.0.1" ] || [ "$1" = "*" ]; then
	    if [ -e "$NTOPNG_CONFIG_PATH/$NTOPNG_STD_NAME" ]; then
    		CONF_NTOPNG="true"
	    fi
	fi
    }
}

# Create ntopng configuration
function createntopng {
    {
	# Asking if the user want to configure ntopng
	if (whiptail --title "ntopng Configuration" --yesno "ntopng found, do you want to configure ntopng configuration too?" 8 78); then
            if [ -e "$NTOPNG_CONFIG_PATH/$NTOPNG_STD_NAME" ]; then
		# Renaming the actual configuration with a new name not existing into /etc/ntopng/
		i=0
		while true; do
		    if [ ! -e "$NTOPNG_CONFIG_PATH/$NTOPNG_STD_NAME.old.$i" ]; then
                        mv $NTOPNG_CONFIG_PATH/$NTOPNG_STD_NAME $NTOPNG_CONFIG_PATH/$NTOPNG_STD_NAME.old.$i
			cp $TEMPLATE_HOME/$NTOPNG_STD_NAME.flow_collector $NTOPNG_CONFIG_PATH/$NTOPNG_STD_NAME
			sed -i s/\$INTERFACE/$1/g $NTOPNG_CONFIG_PATH/$NTOPNG_STD_NAME

			exitstatus=$?
			if [ "$exitstatus" = "0" ]; then
				whiptail --title "Report" --msgbox "Ntopng configured successfully." 12 80
			else
				whiptail --title "Error" --msgbox "Error while configuring Ntopng." 12 80
			fi

			createMenu
		    fi

		    i=$((i+1))
		done
            fi
	fi
        createMenu
    }
}

# Create a probe configuration
function createProbe {
    {
	# Variables need to pass to the config template
	IF=""
	# Getting various options

	# Interface option
	FILES=$(cat /proc/net/dev | grep ':'| cut -d ':' -f 1 | tr -d '[:blank:]')
        ARGS=()
	i=0

        for LINE in $FILES; do
            ARGS+=("${LINE}")
	    if [ $LINE == "$LAN_INTERFACE" ]; then
		COMMENT="(LAN)"
	    else
		if [ $LINE == "$WAN_INTERFACE" ]; then
		    COMMENT="(WAN)"
		else
		    COMMENT=""
		fi
	    fi

	    ARGS+=("Interface ${LINE} ${COMMENT}")
            if [[ $i -eq 0 ]]; then
                ARGS+=("ON")
		i=$((i+1))
            else
                ARGS+=("OFF")
            fi
        done

        IF=$(
            whiptail --title "Interface list" --radiolist "Select an Interface:" 25 78 16 \
                     "${ARGS[@]}" 3>&2 2>&1 1>&3
	  )

	exitstatus=$?
        if [ "$exitstatus" = "1" ]; then
                createMenu
        fi

	# Collector nprobe option
	COLLECTOR_IP=""
        while true; do
                COLLECTOR_IP=$(
                whiptail --inputbox "Insert the IP address of the collector." 12 80 --title "IP address" 3>&1 1>&2 2>&3
                )

		exitstatus=$?
            	if [ "$exitstatus" = "1" ]; then
                	createMenu
            	fi

                if [[ $COLLECTOR_IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] || [ "$COLLECTOR_IP" = "*" ]; then
                        break
                fi

                whiptail --title "Input error" --msgbox "Invalid value. Please insert a valid IP address." 12 80
        done

	COLLECTOR_PORT=""
	while true; do
            COLLECTOR_PORT=$(
                whiptail --inputbox "Insert the port. The port number must be between 1 and 65535" 12 80 --title "Port" 3>&1 1>&2 2>&3
                          )
	    exitstatus=$?
            if [ "$exitstatus" = "1" ]; then
                createMenu
            fi

            if [[ $COLLECTOR_PORT -le 65535 ]] && [[ $COLLECTOR_PORT -ge 1 ]]; then
                break
            fi

	    whiptail --title "Input Error" --msgbox "Invalid value. Please insert a valid Port number." 12 80
        done	

        COLLECTOR="tcp:\/\/"
        COLLECTOR+=$COLLECTOR_IP
        COLLECTOR+=":"
        COLLECTOR+=$COLLECTOR_PORT
	
	# NetFlow version
        VERSION=$(
            whiptail --title "Create the Probe" --radiolist "Select the NetFlow version:" 25 78 16 \
                     "5" "NetFlow version 5" OFF \
                     "9" "NetFlow version 9" OFF \
                     "10" "NetFlow version 10 (IPFIX)" ON 3>&2 2>&1 1>&3
               )

	exitstatus=$?
        if [ "$exitstatus" = "1" ]; then
                createMenu
        fi

	# Checking if the template can be found
	TEMPLATE_NAME=$TEMPLATE_HOME/nprobe.conf.probe_collector
        if [ ! -e $TEMPLATE_NAME ]; then
            whiptail --title "ERROR" --msgbox "Unable to find the template $TEMPLATE_NAME. Error while creating the configuration." 12 80
            createMenu
        fi

	checkForcedName "$IF"
	CONFIG_FILE="$NPROBE_CONFIG_PATH/$CONFIG_NAME.conf"
	
	# Coping and modifing the template
        cp $TEMPLATE_HOME/nprobe.conf.probe_collector $CONFIG_FILE
        sed -i "s/\$I/$IF/g" $CONFIG_FILE
	sed -i "s/\$COLLECTOR/$COLLECTOR/g" $CONFIG_FILE
	sed -i "s/\$VERSION/$VERSION/g" $CONFIG_FILE

	if [ -e $CONFIG_FILE ]; then
        	whiptail --title "Report" --msgbox "Configuration successfully created." 12 80
	else
		whiptail --title "Error" --msgbox "Error occurred while creating the configuration." 12 80
	fi

	createMenu
    }
}

# Create a collector configuration
function createProxy {
    {
	nameCollector

	# Variables need to pass to the config template
        IF=""
	COLLECTOR=""
	VERSION=""

	# Collector port option of nprobe
	while true; do
            INGRESS_PORT=$(
                whiptail --inputbox "Insert the port. The port number must be between 1 and 65535." 12 80 --title "Port Number" 3>&1 1>&2 2>&3
			)
	    exitstatus=$?
            if [ "$exitstatus" = "1" ]; then
                createMenu
            fi

	    if [[ $INGRESS_PORT -le 65535 ]] && [[ $INGRESS_PORT -ge 1 ]]; then
		break
	    fi

	    whiptail --title "Input Error" --msgbox "Invalid value. Please insert a valid Port number." 12 80
	done

	# Collector option of nprobe
	COLLECTOR_IP=""
        while true; do
                COLLECTOR_IP=$(
                whiptail --inputbox "Insert the IP address of the collector." 12 80 --title "IP Address" 3>&1 1>&2 2>&3
                )

		exitstatus=$?
            	if [ "$exitstatus" = "1" ]; then
                	createMenu
            	fi

                if [[ $COLLECTOR_IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] || [ "$COLLECTOR_IP" = "*" ]; then
                        break
                fi

                whiptail --title "Input Error" --msgbox "Invalid value. Please insert a valid IP address." 12 80
        done

	COLLECTOR_PORT=""
	while true; do
            COLLECTOR_PORT=$(
                whiptail --inputbox "Insert the port. The port number must be between 1 and 65535" 12 80 --title "Port Number" 3>&1 1>&2 2>&3
                	  )
	    exitstatus=$?
            if [ "$exitstatus" = "1" ]; then
                createMenu
            fi

            if [[ $COLLECTOR_PORT -le 65535 ]] && [[ $COLLECTOR_PORT -ge 1 ]]; then
                break
            fi

	    whiptail --title "Input Error" --msgbox "Invalid value. Please insert a valid Port number." 12 80
        done

	COLLECTOR="tcp:\/\/"
	COLLECTOR+=$COLLECTOR_IP
	COLLECTOR+=":"
	COLLECTOR+=$COLLECTOR_PORT

	VERSION=$(
	    whiptail --title "Create the Probe" --radiolist "Select the NetFlow version:" 25 78 16 \
		     "5" "NetFlow version 5" OFF \
		     "9" "NetFlow version 9" OFF \
		     "10" "NetFlow version 10 (IPFIX)" ON 3>&2 2>&1 1>&3
	       )

	exitstatus=$?
        if [ "$exitstatus" = "1" ]; then
            createMenu
        fi

	# Checking if the template can be found
	TEMPLATE_NAME=$TEMPLATE_HOME/$NPROBE_STD_NAME.collector_collector
        if [ ! -e $TEMPLATE_NAME ]; then
            whiptail --title "Error" --msgbox "Unable to find the template $TEMPLATE_NAME. Error while creating the configuration." 12 80
            createMenu
        fi

	# Coping and modifing the collector template
	CONFIG_FILE="$NPROBE_CONFIG_PATH/$CONFIG_NAME.conf"

	cp $TEMPLATE_HOME/$NPROBE_STD_NAME.collector_collector $CONFIG_FILE
	sed -i "s/\$COLLECTOR_PORT/$INGRESS_PORT/g" $CONFIG_FILE
	sed -i "s/\$COLLECTOR/$COLLECTOR/g" $CONFIG_FILE
	sed -i "s/\$VERSION/$VERSION/g" $CONFIG_FILE

	if [ -e $CONFIG_FILE ]; then
                whiptail --title "Report" --msgbox "Configuration successfully created." 12 80
        else
                whiptail --title "Error" --msgbox "Error occurred while creating the configuration." 12 80
       	fi

	createMenu
    }
}

# Create a collector configuration
function probeToNtopng {
    {
	whiptail --title "Error" --msgbox "Configuration still in development." 12 80
	createMenu
    }
}

# Create a collector configuration
function flowsToNtopng {
    {
	nameCollector

        # Variables need to pass to the config template
        INGRESS_PORT=""
        ZMQ=""

        # Collector port option
        while true; do
            INGRESS_PORT=$(
                whiptail --inputbox "Insert the port. The port number must be between 1 and 65535." 12 80 --title "Port Number" 3>&1 1>&2 2>&3
			)
	    exitstatus=$?
            if [ "$exitstatus" = "1" ]; then
                createMenu
            fi

            if [[ $INGRESS_PORT -le 65535 ]] && [[ $INGRESS_PORT -ge 1 ]]; then
                break
            fi

	    whiptail --title "Input Error" --msgbox "Invalid value. Please insert a valid Port number." 12 80
        done

	# ZMQ option
	COLLECTOR_IP=""
	while true; do
            	COLLECTOR_IP=$(
            	whiptail --inputbox "Insert the IP address of the collector." 12 80 --title "IP Address" 3>&1 1>&2 2>&3
                )

		exitstatus=$?
            	if [ "$exitstatus" = "1" ]; then
                	createMenu
                fi

        	if [[ $COLLECTOR_IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] || [ "$COLLECTOR_IP" = "*" ]; then
                	break
		fi

		whiptail --title "Input Error" --msgbox "Invalid value. Please insert a valid IP address." 12 80
        done

        COLLECTOR_PORT=""
	#Cycling until a valid port is inserted
        while true; do
            COLLECTOR_PORT=$(
                whiptail --inputbox "Insert the port. The port number must be between 1 and 65535." 12 80 --title "Port Number" 3>&1 1>&2 2>&3
                          )
	    exitstatus=$?
            if [ "$exitstatus" = "1" ]; then
                createMenu
            fi

            if [[ $COLLECTOR_PORT -le 65535 ]] && [[ $COLLECTOR_PORT -ge 1 ]]; then
                break
            fi
	
	    whiptail --title "Input Error" --msgbox "Invalid value. Please insert a valid Port number." 12 80
        done

        ZMQ="tcp:\/\/"
        ZMQ+=$COLLECTOR_IP
        ZMQ+=":"
        ZMQ+=$COLLECTOR_PORT

	checkNtopngExists "$COLLECTOR_IP"

	# Checking if the template can be found
	TEMPLATE_NAME=$TEMPLATE_HOME/$NPROBE_STD_NAME.collector_ntopng
        if [ ! -e $TEMPLATE_NAME ]; then
            whiptail --title "Error" --msgbox "Unable to find the template $TEMPLATE_NAME. Error while creating the configuration." 12 80
            createMenu
        fi

	# Coping and modifing the template with the various options
	CONFIG_FILE="$NPROBE_CONFIG_PATH/$CONFIG_NAME.conf"

        cp $TEMPLATE_HOME/$NPROBE_STD_NAME.collector_ntopng $CONFIG_FILE
	sed -i "s/\$COLLECTOR_PORT/$INGRESS_PORT/g" $CONFIG_FILE
	sed -i "s/\$ZMQ/$ZMQ/g" $CONFIG_FILE

        if [ -e $CONFIG_FILE ]; then
                whiptail --title "Report" --msgbox "Configuration successfully created." 12 80
        else
                whiptail --title "Error" --msgbox "Error occurred while creating the configuration." 12 80
                createMenu
        fi

        if [ "$CONF_NTOPNG" = "true" ]; then
            createntopng $ZMQ
        else
            createMenu
        fi
    }
}

# Create a Flow collector configuration
function createFlowCollector {
    {
	nameCollector

	# Variables needed to pass to the config template
        IF=""
	ZMQ=""

        # Getting various options

	# Interface option
        FILES=$(cat /proc/net/dev | grep ':'| cut -d ':' -f 1 | tr -d '[:blank:]')
        ARGS=()
	i=0

        for LINE in $FILES; do
            ARGS+=("${LINE}")
            ARGS+=("Interface ${LINE}")
            if [ $i -eq 0 ]; then
		ARGS+=("ON")
		i=$((i+1))
	    else
		ARGS+=("OFF")
	    fi
        done

        IF=$(
            whiptail --title "Interface List" --radiolist "Select an Interface:" 25 78 16 \
                     "${ARGS[@]}" 3>&2 2>&1 1>&3
          )
	
	exitstatus=$?
        if [ "$exitstatus" = "1" ]; then
                createMenu
        fi

	# ZMQ collector option

	# IP address
        COLLECTOR_IP=""
        while true; do
                COLLECTOR_IP=$(
                whiptail --inputbox "Insert the IP address of the collector." 12 80 --title "IP address" 3>&1 1>&2 2>&3
                )

		exitstatus=$?
            	if [ "$exitstatus" = "1" ]; then
               		createMenu
            	fi

                if [[ $COLLECTOR_IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] || [ "$COLLECTOR_IP" = "*" ]; then
                        break
                fi

                whiptail --title "Input Error" --msgbox "Invalid value. Please insert a valid IP address." 12 80
        done

	# Port
        COLLECTOR_PORT=""
	# Cycling until a valid port is inserted
        while true; do
            COLLECTOR_PORT=$(
                whiptail --inputbox "Insert the port. The port number must be between 1 and 65535" 12 80 --title "Port Number" 3>&1 1>&2 2>&3
                	  )

	    exitstatus=$?
            if [ "$exitstatus" = "1" ]; then
                createMenu
            fi

            if [[ $COLLECTOR_PORT -le 65535 ]] && [[ $COLLECTOR_PORT -ge 1 ]]; then
                break
            fi

	    whiptail --title "Input Error" --msgbox "Invalid value. Please insert a valid Port number." 12 80
        done

        ZMQ="tcp:\/\/"
        ZMQ+=$COLLECTOR_IP
        ZMQ+=":"
        ZMQ+=$COLLECTOR_PORT

	# Checking if the template can be found
	TEMPLATE_NAME=$TEMPLATE_HOME/$NPROBE_STD_NAME.probe_ntopng
	if [ ! -e $TEMPLATE_NAME ]; then
	    whiptail --title "Error" --msgbox "Unable to find the template $TEMPLATE_NAME. Error while creating the configuration." 12 80
	    createMenu
	fi

	# Coping the template and modifing it with the various options
	CONFIG_FILE="$NPROBE_CONFIG_PATH/$CONFIG_NAME.conf"

	cp $TEMPLATE_HOME/$NPROBE_STD_NAME.probe_ntopng $CONFIG_FILE
        sed -i "s/\$I/$IF/g" $CONFIG_FILE
        sed -i "s/\$ZMQ/$ZMQ/g" $CONFIG_FILE

        if [ -e $CONFIG_FILE ]; then
                whiptail --title "Report" --msgbox "Configuration successfully created." 12 80
        else
                whiptail --title "Error" --msgbox "Error occurred while creating the configuration." 12 80
        fi

	createMenu
    }
}

# Create configuration for the application using a template
function createMenu {
    {
	# Creation of configuration menu
        CHOICE=$(
	    whiptail --title "Select the type of configuration you want to create." --menu "Choose an option:" 25 78 16 \
         	     "<-- Back" "Return to the main menu." \
         	     "Probe" "Capture packets and send flows to a flow collector." \
         	     "Flow Collector" "Collect flows and send them to a flow collector." \
         	     "Proxy" "Collect flows and export them to a flow collector." \
		     "Probe to ntopng" "Capture packets and send flows to ntopng." \
		     "Flows to ntopng" "Collect flows and send them to ntopng." \
		     3>&2 2>&1 1>&3
	      )

	exitstatus=$?
        if [ "$exitstatus" = "1" ]; then
                mainMenu
        fi

        # Choosing the choice
        case $CHOICE in
            "<-- Back")
                goBack
                ;;
	    "Probe")
                createProbe
                ;;
            "Flow Collector")
                createFlowCollector
                ;;
	    "Proxy")
		createProxy
		;;
	    "Flows to ntopng")
		flowsToNtopng
		;;
	    "Probe to ntopng")
		probeToNtopng
		;;
        esac
    }
}

# Start the probe service
function start {
    {
	# Removing the result from the stdout
	systemctl start "$1" 2>/dev/null
	exitstatus=$?
	
        if [ "$exitstatus" = "0" ]; then
                whiptail --title "Report" --msgbox "nProbe successfully started." 12 80
        else
                whiptail --title "Error" --msgbox "Error trying to start nProbe service." 12 80
        fi

	manageProbe "$2"
    }
}

# Stop the probe service
function stop {
    {
        # Removing the result from the stdout
        systemctl stop "$1" 2>/dev/null
	
	exitstatus=$?
        if [ "$exitstatus" = "0" ]; then
        	whiptail --title "Report" --msgbox "nProbe successfully stopped." 12 80
	else
		whiptail --title "Error" --msgbox "Error trying to stop nProbe service." 12 80
	fi
		
	manageProbe "$2"
    }
}


# Check status of the probe service
function status {
    {
        # Removing the result from the stdout
        OUT=`systemctl status "$1"`
	
        whiptail --title "Report" --scrolltext --msgbox "$OUT" 24 100
		
	manageProbe "$2"
    }
}


# Restart the probe service
function restart {
    {
	# Removing the result from the stdout
        systemctl restart "$1" 2>/dev/null
        
	exitstatus=$?
        if [ "$exitstatus" = "0" ]; then
                whiptail --title "Report" --msgbox "nProbe successfully restarted." 12 80
        else
                whiptail --title "Error" --msgbox "Error trying to restart nProbe service." 12 80
        fi

	manageProbe "$2"
    }
}

# Enabling the probe service startup
function enable {
    {
	# Removing the result from the stdout
        systemctl enable "$1" 2>/dev/null
	
	exitstatus=$?
        if [ "$exitstatus" = "0" ]; then
                whiptail --title "Report" --msgbox "nProbe successfully enabled." 12 80
        else
                whiptail --title "Error" --msgbox "Error trying to enable nProbe service." 12 80
        fi

	manageProbe "$2"
    }
}

# Disable the probe service startup
function disable {
    {
	# Removing the result from the stdout
        systemctl disable "$1" 2>/dev/null
	
	exitstatus=$?
        if [ "$exitstatus" = "0" ]; then
                whiptail --title "Report" --msgbox "nProbe successfully disabled." 12 80
        else
                whiptail --title "Error" --msgbox "Error trying to disable nProbe service." 12 80
        fi

	manageProbe "$2"
    }
}

# Delete the configuration of the probe
function delete {
    {
	FILE="$NPROBE_CONFIG_PATH/"
	FILE+=$1

	# Checking if the user is sure to remove the configuration
	if (whiptail --title "Remove Check" --yesno "Are you sure to remove $FILE?" 8 78); then
	    systemctl stop "$1" 2>/dev/null
	    systemctl disable "$1" 2>/dev/null
	    rm "$FILE"
	else
    	    manageProbe "$1"
	fi

	if [ -e "$FILE" ]; then
		whiptail --title "Error" --msgbox "Error while trying to delete $FILE." 12 80
        	manageProbe "$1"
	fi

        whiptail --title "Report" --msgbox "nProbe configuration successfully deleted." 12 80
	manageMenu
    }
}

# Manage menu of the selected probe
function manageProbe {
    {
        # Managing menu
        CHOICE=$(
	    whiptail --title "Manage $1" --menu "Choose an option:" 25 78 16 \
         	     "<-- Back" "Return to the main menu." \
         	     "Start" "Start $1 service." \
		     "Stop" "Stop $1 service." \
         	     "Restart" "Restart $1 service." \
		     "Status"  "Check $1 service status." \
         	     "Enable" "Enable $1 service startup." \
         	     "Disable" "Disable $1 service startup." \
         	     "Delete" "Delete $1 configuration." 3>&2 2>&1 1>&3
              )
	TMP=$1
	SERVICE=${TMP#*-}
	SERVICE=${SERVICE%.conf*}

	SERVICE_NAME=""
        if [ "$SERVICE" = "" ]; then
                SERVICE_NAME="nprobe"
        else
                SERVICE_NAME="nprobe@$SERVICE"
        fi
	
        # Choosing the choice
        case $CHOICE in
            "<-- Back")
                goBack
                ;;
            "Start")
                start "$SERVICE_NAME" $1
                ;;
	    "Stop")
		stop "$SERVICE_NAME" $1
		;;
            "Restart")
                restart "$SERVICE_NAME" $1
                ;;
            "Status")
                status "$SERVICE_NAME" $1
                ;;
            "Enable")
                enable "$SERVICE_NAME" $1
                ;;
	    "Disable")
                disable "$SERVICE_NAME" $1
                ;;
            "Delete")
                delete "$1"
                ;;
        esac
    }
}

# Managing menu
function manageMenu {
    {
	# Checking if no configurations exist
	if [ ! -d "$NPROBE_CONFIG_PATH/" ]; then
	    whiptail --title "No Configuration" --msgbox "No nProbe configuration found." 8 78
            mainMenu
	fi

	if [ -z "$(ls -A $NPROBE_CONFIG_PATH/*.conf 2>/dev/null)" ]; then
	    whiptail --title "No Configuration" --msgbox "No nProbe configuration found." 8 78
	    mainMenu
	fi

	# Getting the list of actual probes configured from the /etc/nprobe directory
	FILES=`/bin/ls $NPROBE_CONFIG_PATH/*.conf | grep -o '^[^\.]*'`

	ARGS=()
	i=0

	for LINE in $FILES; do
	    LINE+=".conf"
	    FILE=`basename ${LINE}`
    	    ARGS+=("${FILE}")
    	    ARGS+=("Manage ${FILE}")

	    # Choosing the first configuration as default
	    if [[ $i -eq 0 ]]; then
		ARGS+=("ON")
		i=$((i+1))
	    else
    		ARGS+=("OFF")
	    fi
	done

	# Radiolist of the actual probes configured
	CHOICE=$(
	    whiptail --title "Probe List" --radiolist "Select a probe to manage:" 25 78 16 \
		     "${ARGS[@]}" 3>&2 2>&1 1>&3
	      )

	exitstatus=$?
        if [ "$exitstatus" = "1" ]; then
                mainMenu
        fi

	manageProbe "$CHOICE"
    }
}

# Main menu function
function mainMenu {
    {
	# Main Menu
	CHOICE=$(
	    whiptail --title "Select the operation you want to do" --menu "Choose an option:" 25 78 16 \
         	     "<-- Exit" "" \
         	     "Create" "Create a new configuration." \
         	     "Manage" "Manage an existing configuration." 3>&2 2>&1 1>&3
	      )

	# Choosing the choice
	case $CHOICE in
            "<-- Exit")
                checkExit
        	;;
            "Create")
                createMenu
        	;;
            "Manage")
                manageMenu
        	;;
	esac
    }
}

# Changing the colors of whiptail
export NEWT_COLORS='
root=black,blue
listbox=black,lightgray
actlistbox=black,lightgray
checkbox=black,lightgray
'

if [ "$EUID" -ne 0 ]
then
    echo "This tool requires root privileges. Try again with \"sudo \" please ..."
    exit
fi

# Check for local templates presence
if [ -d "./templates" ]
then
    TEMPLATE_HOME="./templates"
fi

# Check for local templates presence
if [ ! -d "$NPROBE_CONFIG_PATH" ]
then
    echo "No $NPROBE_CONFIG_PATH directory: something went wrong."
    exit
fi

# Starting the main loop
mainMenu
