#!/bin/bash
source pakuri.conf
source $MODULES_PATH/misc_module.sh
WINDOW_NAME="${modules[1]}"

function port_scan_menu()
{
    local KEY
    
    box_1 "Port Scan"
    echo -e "-------- Select Action ---------"
    if ps -ef|grep nmap|grep -v "grep" >/dev/null;then
        echo -e "Scan process is Running!"
    else
        echo -e "Do you want to perform a process??" 
        yes-no-help
        read -n 1 -s KEY
        if [ $KEY -eq 1 ];then
            tmux send-keys -t $WINDOW_NAME.1 "$MODULES_PATH/service_act.sh nscan $TARGETS" C-m
            tmux select-pane -t $WINDOW_NAME.0
        elif [ $KEY -eq 3 ];then
            tmux send-keys -t $WINDOW_NAME.1 "$MODULES_PATH/help/scan_help_module.sh 11" C-m 
            tmux select-pane -t $WINDOW_NAME.0
        fi
    fi
}

function enum_menu()
{
    local KEY

    box_2 "Enumeration"
    echo -e "-------- Select Action ---------"
    if [ -f $WDIR/nmap_*.grep ];then
        echo -e "Do you want to perform a process??" 
        yes-no-help
        read -n 1 -s KEY
        if [ $KEY -eq 1 ];then
            tmux send-keys -t $WINDOW_NAME.1 "$MODULES_PATH/service_act.sh enumctrl" C-m
            tmux select-pane -t $WINDOW_NAME.0
        elif [ $KEY -eq 3 ];then
            tmux send-keys -t $WINDOW_NAME.1 "$MODULES_PATH/help/scan_help_module.sh 12" C-m 
            tmux select-pane -t $WINDOW_NAME.0
        fi
    else
        echo -e "A port scan has not been performed."
        read -p "Press enter key to continue..."
    fi
}
function openvas_menu()
{
    local KEY
    local NEW_WINDOW

    NEW_WINDOW=""
    box_3 "OpenVAS"
    echo -e "-------- Select Action ---------"
    echo -e "Do you want to perform a process??" 
    yes-no-help
    read -n 1 -s KEY
    if [ $KEY -eq 1 ];then
        NEW_WINDOW=$(tmux list-window -a|grep OpenVAS|awk '{print $2}')
        if [[ "$NEW_WINDOW" == "" ]];then
            NEW_WINDOW="OpenVAS"
            tmux new-window -n "$NEW_WINDOW"
            tmux split-window -t "$NEW_WINDOW".0 -v -p 15
            tmux send-keys -t "$NEW_WINDOW".1 "$MODULES_PATH/service_act.sh back" C-m
            tmux send-keys -t "$NEW_WINDOW".0 "$MODULES_PATH/service_act.sh openvas ;tmux kill-window -t $NEW_WINDOW" C-m
            tmux select-pane -t $NEW_WINDOW.0
        else
            tmux select-window -t "OpenVAS"
        fi
    elif [ $KEY -eq 3 ];then
        tmux send-keys -t $WINDOW_NAME.1 "$MODULES_PATH/help/scan_help_module.sh 13" C-m 
        tmux select-pane -t $WINDOW_NAME.0
    fi
}

function result_menu()
{
    tmux send-keys -t $WINDOW_NAME.1 "$MODULES_PATH/service_act.sh result" C-m
    tmux select-pane -t $WINDOW_NAME.1

    box_4 Result
    echo -e "-------- Select Action ---------"
    echo -e "Select the output file in the right panel."
    echo -e "Display the contents of a file with the less command"
    echo -e ""
    echo -e "Be sure to select \"Quit\" when you exit."
    read -p "Press enter key to continue..."
}

function scan_manage()
{
    local KEY

    while :
    do
        scan_banner
        select_5 "Port Scan" "Enumeration" "OpenVAS" "Result" "Help"
        read -n 1 -t 25 -s KEY
        
        scan_banner
        case "$KEY" in
            1 )
                port_scan_menu ;;
            2 )
                enum_menu ;;
            3 )
                openvas_menu ;;
            4 )
                result_menu ;;
            5 )
                tmux send-keys -t $WINDOW_NAME.1 "$MODULES_PATH/help/scan_help_module.sh main" C-m 
                tmux select-pane -t $WINDOW_NAME.0 ;;
            9 )
                tmux select-window -t "${modules[0]}" ;;
            * )
                ;;
        esac
    done
}
scan_manage