#!/usr/bin/env bash

#SCREEN BLUE
# Store menu options selected by the user
#[ -f tmp ] &&  echo "Dir tmp not found! let me create it." && mkdir tmp
if [ -d /usr/share/doc/dialog ]
 then
    echo "ok"
else
    echo "need to install  dialog"
    apt-get install dialog -y
fi


INPUT=/tmp/menu.sh.$$
# Storage file for displaying cal and date command output
OUTPUT=/tmp/output.sh.$$
#mkdir tmp

# trap and delete temp files
trap "rm $OUTPUT; rm $INPUT; exit" SIGHUP SIGINT SIGTERM

GREEN="\e[38;5;2m"
NOC=$(tput sgr0)

#
# Purpose - display output using msgbox
#  $1 -> set msgbox height
#  $2 -> set msgbox width
#  $3 -> set msgbox title
#
display_output(){
	local h=${1-50}			# box height default 10
	local w=${2-91} 		# box width default 41
	local t=${3-Output} 	# box title
	dialog --colors --backtitle "Bash3lper // Help Linux Shell Script // by  Erreur32" --title "${t}" --clear --msgbox "$(<$OUTPUT)" ${h} ${w}
}

LSB=/usr/bin/lsb_release

# Purpose - display current helpscript  listing
#
show_help(){
        echo -e "\Zb\Z2HELP : \Zb\Z1 MENU aide tips : \Zb\Z3 test \n" >$OUTPUT
    display_output 25 60 "Show Help"
}

show_info_script(){
        dialog --colors --backtitle "Bash3lper // Help Linux Shell Script // by  Erreur32" --title "Info Script  dialog"  --msgbox "Info Bash3lper Script: \n Test : All Good !   \n You are running on latest script  \n Version : 0.32 \n Check also  show_help" 15 60
#        echo -e "Info Bash3lper Script: \n Test : All Good !   \n You are running on  $(hostname -f) \n Version : 0.32 \n Check also"  show_help
#        display_output 25 60 "Info Script"
}

show_date(){
	echo -e "Today is $(date) \n Machine : \Zb\Z3 $(hostname -f)." >$OUTPUT
        display_output 60 100 "Date and Time"
}

show_os_info(){
        os_info >$OUTPUT
        display_output 20 60 "Os Info"
}


show_all_info(){
#dialog --backtitle "Bash3lper // Help Linux Shell Script // by  Erreur32" --title "All INFO  dialog"  --msgbox os_info 35 60
	all_info >$OUTPUT
	display_output 85 160  "All Info"
}


show_host_info(){
        host_info >$OUTPUT
	display_output 20 100 "Host Info"
}

show_net_info(){
        net_info >$OUTPUT
        display_output 40 160 "Net Info"
}

show_stat_info(){
#        cat /etc/motd >$OUTPUT
	scripts/Monitor321.sh  -s -e -l -d  >$OUTPUT
        display_output 40 160 "Stat Info"
}

# System info
#
all_info(){
# truc="truc";
        echo -e "All info system : \Zb\Z1 $(hostname)  \Zb\Z0\n "
	echo -e " "
	os_info
	echo ""
	host_info
	echo ""
	mem_info
}

 redhawk(){
        php scripts/redhawk.php
}

# Get info about your operating system
os_info(){
#        write_header " System information "
        echo -e "Operating system : $(uname)"
        [ -x $LSB ] && $LSB -a || echo -e "$LSB command is not insalled (set \$LSB variable)"
        #pause "Press [Enter] key to continue..."
}

# Get info about host such as dns, IP, and hostname
host_info(){
        local dnsips=$(sed -e '/^$/d' /etc/resolv.conf | awk '{if (tolower($1)=="nameserver") print $2}')
        write_header " Hostname and DNS information "
        echo "Hostname : $(hostname -s)"
        echo "DNS domain : $(hostname -d)"
        echo "Fully qualified domain name : $(hostname -f)"
        echo "Network address (IP) :  $(hostname -i)"
        echo "DNS name servers (DNS IP) : ${dnsips}"
}

# Purpose - Network inferface and routing info
net_info(){
        devices=$(netstat -i | cut -d" " -f1 | egrep -v "^Kernel|Iface|lo")
        write_header " Network information "
        echo -e "Total network interfaces found : $(wc -w <<<${devices})"
        echo -e "*** IP Addresses Information ***"
        ip -4 address show
	echo -e " \Zb\Z0"
        echo "*** Network routing ***"
        echo "***********************"
        netstat -nr
        echo "*** Interface traffic information ***"
        echo "**************************************"
        netstat -i
}

##############################################################
# Purpose - Display a list of users currently logged on
#           display a list of receltly loggged in users
user_info(){
        local cmd="$1"
        case "$cmd" in
                who) write_header " Who is online "; who -H; pause ;;
                last) write_header " List of last logged in users "; last ; pause ;;
        esac
}

# Purpose - Display used and free memory info
mem_info(){
#clear
#        write_header " Free and used memory "
	echo ""
        free -m
	swapon --show
	echo ""
        echo -e " Virtual memory statistics "
        echo -e "*********************************"
	echo ""
        vmstat
	echo ""
        echo " Top 5 memory eating process "
        echo "***********************************"
        ps auxf | sort -nr -k 4 | head -5
}

Clock32(){
	tty-clock -csbr -C8
}


# ignore CTRL+C, CTRL+Z and quit singles using the trap
#trap '' SIGINT SIGQUIT SIGTSTP

#
# set infinite loop
#
while true
do

### display main menu ###
dialog --clear --colors  --help-button --backtitle " Bash3lper // Help Linux Shell Script // By Erreur32 " \
--title " [ M A I N - M E N U ] " \
--menu "You can use the UP/DOWN arrow keys, the first \n\
letter of the choice as a hot key, or the \n\
number keys 1-9 to choose an option.\n\
" 25 50 16 \
Exit "Exit Bash3lper" \
System "all_info" \
Host "Host Info System" \
Os "Os Info" \
Net "Net Info" \
Stat "Stats system" \
User "Show some user info" \
Hack "redhawk ..." \
Nano "Start Nano (text editor)" \
Clock32 "Clock on Screen" \
Date/time "Displays date and time" \
Clock32 "Clock on Screen ctrl+c to exit" \
Info "Info Bash3lper script"  2>"${INPUT}"

#--msgbox "This is an entirely open source software." 10 30

menuitem=$(<"${INPUT}")

# make decsion 
#Help) show_help ;;
case $menuitem in
	System) show_all_info;;
        Info) show_info_script;;
	Host) show_host_info;;
	Os) show_os_info;;
	Net) show_net_info;;
	Stat) show_stat_info;;
	User) show_user_info;;
	Hack) redhawk;;
	Nano) nano;;
	Date/time) show_date;;
        Clock32) Clock32;;

	Exit) clear ; echo -e "\n thanks for using $GREEN Bash3lper $(tput sgr0) \n ";  break ;;
esac

done

# if temp files found, delete em
[ -f $OUTPUT ] && rm $OUTPUT
[ -f $INPUT ] && rm $INPUT
