#!/bin/bash
# Store menu options selected by the user
INPUT=/tmp/menu.sh.$$

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

# get text editor or fall back to vi_editor
vi_editor=${EDITOR-vi}

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

#
# Purpose - display output using msgbox
#  $1 -> set msgbox height
#  $2 -> set msgbox width
#  $3 -> set msgbox title
#
function display_output() {
  local h=${1-10}     # box height default 10
  local w=${2-41}     # box width default 41
  local t=${3-Output} # box title
  dialog --backtitle "SRZONE SYSTEM MENU" --title "${t}" --clear --msgbox "$(<$OUTPUT)" ${h} ${w}
}
#
# Purpose - display current system date & time
#
function show_date() {
  echo "Today is $(date) @ $(hostname -f)." >$OUTPUT
  display_output 6 60 "Date and Time"
}
#
# Purpose - display a calendar
#
function show_calendar() {
  cal >$OUTPUT
  display_output 13 25 "Calendar"
}
#

function print_array() {
  echo "hello world"
}

function timezone() {
  sudo dpkg-reconfigure tzdata
}

function show_interface() {
  echo "$(ls -1 /sys/class/net | grep -v 'lo')" >$OUPUT
  display_output 13 25 "Interfaces"
}
#function show iinterface menu
#####################
function display_interface() {
  myArray=($(ls -1 /sys/class/net | grep -v 'lo'))
  elems=${#myArray[@]}
  echo "elements: $elems"

  echo " the array size is ${#myArray[@]}"
  #####################
  dialog --clear --backtitle "SRZONE SYSTEM MENU" \
    --title "[ S R Z O N E - M A I N - M E N U ]" \
    --menu "Select interface to edit settings \n" 15 50 4 \
    $(for ((i = 0; i < elems; i++)); do
      #echo "${myArray[$i]}"
      printf "${myArray[$i]}	\"Edit\"\n"
    done) 2>"${INPUT}"
  menuitem=$(<"${INPUT}")

  netplan_file="/etc/netplan/01-netcfg.yaml" # Replace with the path to your Netplan configuration file
  current_ip=$(awk -F'[:,]' '/addresses:/ {gsub(/[\[\] ]/, ""); print $2}' "$netplan_file")
  current_gateway=$(awk -F'[:,]' '/gateway4:/ {gsub(/[ ]/, ""); print $2}' "$netplan_file")
  current_dns=$(grep -A 5 "nameservers:" /etc/netplan/01-netcfg.yaml | grep '"' | tr -d '" -' | xargs)

  if [ $menuitem = ""]; then
    return 0
  fi

  dialog --backtitle "$menuitem interface settings" --title "$menuitem Settings" \
    --form "\nSettings for $menuitem" 10 38 3 \
    "IP address	:" 1 1 "$current_ip" 1 16 25 30 \
    "Gateway	:" 2 1 "$current_gateway" 2 16 25 30 \
    "DNS	:" 3 1 "$current_dns" 3 16 25 30 \
    2>/tmp/form.$$

  #save interface setting to a file
  check=$(cat /tmp/form.$$)
  if [ "$check" = "" ]; then
    return 0
  fi
  data=$(cat /tmp/form.$$)
  echo "$data" >/tmp/$menuitem
  #putting form data into varialbles
  ipaddress="$(cat /tmp/$menuitem | awk 'FNR==1')"
  gateway="$(cat /tmp/$menuitem | awk 'FNR==2')"
  dnsaddress="$(cat /tmp/$menuitem | awk 'FNR==3')"
  netplan_yaml="network:
  version: 2
  renderer: networkd
  ethernets:
    $menuitem:
      addresses: [ $ipaddress ]
      gateway4: $gateway
      nameservers:
          search: [ xyz ]
          addresses:
              - \"$dnsaddress\""

  rm -R /etc/netplan/01-netcfg.yaml
  echo "$netplan_yaml" >/etc/netplan/01-netcfg.yaml
  
  # Removing CIDR notation from the IP address
  ipaddress_only=$(echo "$ipaddress" | cut -d'/' -f1)

  > /opt/srzone/defaults/machineIP  # This clears the file
  echo "$ipaddress_only" > /opt/srzone/defaults/machineIP

  netplan apply
}

#####
#reboot function
function reboot() {
  DIALOG=${DIALOG=dialog}

  $DIALOG --title "WARNING" --clear \
    --yesno "Are you sure you want to reboot the system" 6 50

  case $? in
  0)
    shutdown -r now
    ;;
  1)
    return 0
    ;;
  255)
    return 0
    ;;
  esac
}
#####
#####
#shutdown
function turnoff() {
  DIALOG=${DIALOG=dialog}

  $DIALOG --title "WARNING" --clear \
    --yesno "Are you sure you want to Shutdown the system" 6 50

  case $? in
  0)
    shutdown now -h
    ;;
  1)
    return 0
    ;;
  255)
    return 0
    ;;
  esac
}

#####
# sharebox password reset function
#####
function resetAdmin() {
  dialog --clear --backtitle "SRZONE SYSTEM MENU" \
    --title "[ Reset Admin Password ]" \
    --msgbox "Admin password has been reset to 'admin'" 6 50
  mysql -u srzone -psrzone123 srzone -e 'UPDATE `srzone_admin_users` SET `password` = "$2y$10$MW6B.qT6OXCTB1TKyQ8DZengNQ4diiu.2/970SP946ZqOt/fy4wMy" WHERE `id` = 1'
}

###
#about us
####
function about() {
  DIALOG=${DIALOG=dialog}

  dialog --title 'About' --msgbox 'SRZONE SYSTEM MENU / Copyright SRZONE (2024)' 7 60
}

## drop to shell function
function shell() {
  clear
  sh /etc/profile.d/srzone.sh
  bash
}

###
####another menu open for interfaces

####################################
#
#
# set infinite loop
#
while true; do

  ### display main menu ###
  dialog --clear --backtitle "SRZONE SYSTEM MENU" \
    --title "[ S R Z O N E - M A I N - M E N U ]" \
    --menu "Tasks :" 20 50 10 \
    Network/IP/DNS "IP settings" \
    Timezone "Set Time Zone" \
    SRZONE/Admin "Reset SRZONE admin" \
    About "About" \
    Reboot "Reboot System" \
    Shutdown "Shutdown System" \
    Exit "Logout Session" \
    Shell "Exit to shell" 2>"${INPUT}"
  menuitem=$(<"${INPUT}")

  case $menuitem in
  Network/IP/DNS) display_interface ;;
  Network/Gateway) gateway ;;
  Reboot) reboot ;;
  Shutdown) turnoff ;;
  About) about ;;
  Editor) ifconfig ;;
  SRZONE/Admin) resetAdmin ;;
  Shell) shell ;;
  Timezone) timezone ;;
  Tunnel) tunnel ;;
  Exit) exit ;;
  "")
    exit
    break
    ;;
  esac

done

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

