#!/bin/sh

DIALOG_CANCEL=1
DIALOG_ESC=255
HEIGHT=16
WIDTH=50
OCTO_VERSION=`cat /etc/octo_version`
MAC=`/sbin/ifconfig br-lan |  /usr/bin/awk '/HWaddr /{print substr($5,1)}' | /usr/bin/tr -d ":"`
IP=`ip addr show tun0 | grep "inet\b" | awk '{print $2}' | cut -d/ -f1`

FS=`mount | grep -q "/tmp/root on /"`
if [[ $FS == "1" ]]; then
  FS_DISPLAY='JFFS'
else
  FS_DISPLAY="TMP / RAMDISK / TUN:$IP "
fi

display_result() {
  dialog --title "$1" \
    --no-collapse \
    --msgbox "$result" 0 0
}


sub_analyse() {
sa="1"
while true; do
  exec 3>&1
  sa=$(dialog \
    --no-lines\
    --no-shadow\
    --default-item $sa \
    --backtitle "AP Version: $OCTO_VERSION / FS: $FS_DISPLAY / MAC:$MAC" \
    --title "Analyse" \
    --clear \
    --cancel-label "Beenden" \
    --menu "Auswahl" $HEIGHT $WIDTH 9 \
    "1" "HORST: WLAN Scanner 2.4" \
    "2" "HORST: WLAN Scanner 5" \
    "3" "iptraf-ng" \
    "4" "htop" \
    "5" "Wavemon AP Scanner 2.4" \
    "6" "Wavemon AP Scanner 5" \
    "7" "Memory (smemcap)" \
    2>&1 1>&3)
  exit_status=$?
  exec 3>&-
  case $exit_status in
    $DIALOG_CANCEL)
      clear
      return
      ;;
    $DIALOG_ESC)
      clear
      return
      ;;
  esac
  case $sa in
    0 )
      clear
      return
      ;;
    1 )
        iw dev wlan1 interface add mon1 type monitor
        horst -i mon1
      ;;
    2 )
        iw dev wlan0 interface add mon0 type monitor
        horst -i mon0
      ;;
    3 )
       iptraf-ng
      ;;
    4 )
      htop
      ;;
    5 )
      wavemon -i wlan1
      ;;
    6 )
      wavemon -i wlan0
      ;;
    7 )
      result=`smemcap | head -10 | tail -9`
      display_result "Smemcap"
  esac

done
}

sub_devlog() {
st="1"
trap "" SIGINT
while true; do
  exec 3>&1
  st=$(dialog \
    --no-lines\
    --no-shadow\
    --default-item $st \
    --backtitle "AP Version: $OCTO_VERSION / FS: $FS_DISPLAY / MAC:$MAC" \
    --title "Developer-Log" \
    --clear \
    --cancel-label "Beenden" \
    --menu "Auswahl" $HEIGHT $WIDTH 9 \
    "1" "TCP APCLIENT -> Central" \
    "2" "TCP NWIFI -> APCLIENT" \
    2>&1 1>&3)
  exit_status=$?
  exec 3>&-
  case $exit_status in
    $DIALOG_CANCEL)
      clear
      return
      ;;
    $DIALOG_ESC)
      clear
      return
      ;;
  esac
  case $st in
    0 )
      clear
      return
      ;;
    1 )
      tcpdump -i br-lan -s 0 -A "host cl.octogate.de and (port 443 or port 80 or port 53 or port 22)"
      ;;
    2 )
      tcpdump -i lo -s 0 -A port 1999
      ;;
   esac
 done
}

selection="1"
while true; do
  exec 3>&1
  selection=$(dialog \
    --no-lines\
    --no-shadow\
    --default-item $selection \
    --backtitle "AP Version: $OCTO_VERSION / FS: $FS_DISPLAY / MAC:$MAC" \
    --title "Menu" \
    --clear \
    --cancel-label "Beenden" \
    --menu "Auswahl" $HEIGHT $WIDTH 7 \
    "1" "Aktuelle Config ins Flash" \
    "2" "Firmware Update" \
    "3" "Analyse" \
    "4" "Developer-Logs" \
    "5" "Apclient/Nwifi update" \
    2>&1 1>&3)
  exit_status=$?
  exec 3>&-
  case $exit_status in
    $DIALOG_CANCEL)
      clear
      echo "Program terminated."
      exit
      ;;
    $DIALOG_ESC)
      clear
      echo "Program aborted." >&2
      exit 1
      ;;
  esac
  case $selection in
    0 )
      clear
      echo "Program terminated."
      ;;
    1 )
      result=`octo_flash_cfg.sh`
      display_result "Ergebnis Flash Config"
      ;;
    2 )
      octo_update_fw.sh
      ;;
    3 )
      sub_analyse
      ;;
    4 )
      sub_devlog 
      ;;
    5 )
     result=`octo_update_tools.sh`
     display_result "Update tools result"
      ;;	
  esac
done
reset

