#!/bin/bash

# Everything else needs to be run as root
BNAME=`basename $0`
if [[ ${EUID} -ne 0 ]]; then
  TITLE="Error !"
  MSG="Script must be run as root.\n\nTry 'sudo $BNAME'\n"
  dialog --colors --clear --title " \Z1$TITLE\Zn " --msgbox "\n\Z1$MSG\Zn" 10 70
  exit 1
fi

if ! ping -c 1 aigo.serveftp.org >> /dev/null 2>&1; then
  TITLE="Network Error !"
  MSG="You need to have working Internet connection to run this script."
  dialog --colors --clear --title " \Z1$TITLE\Zn " --msgbox "\n\Z1$MSG\Zn" 10 70
  exit 1
fi

# TODO: check disk space

VER=`dpkg -s aigo-tools | grep Version | awk -F ': ' '{print $2}' > /dev/null 2>&1`

AUTHOR="Contributed by Cheng-Chang Ho."

AIGOUSER=`echo $HOME | awk -F '/' '{print $3}'`
AIGO_VER=`cat /etc/aigo_version`

INTERACTIVE=True
ASK_TO_REBOOT=0

AIGO_USER=aigo
AIGO_HOME=/home/$AIGO_USER
AUTOSTART_DIR=$AIGO_HOME/.config/autostart

INPUT=/tmp/aigo_config.subinput
OUTPUT=/tmp/aigo_config.suboutput

[ -f $INPUT ] && rm -f $INPUT
[ -f $OUTPUT ] && rm -f $OUTPUT

CONFDIR=/etc/hostapd
CONFFILE=hostapd.conf

SUBDIR=`date +%Y%m%d-%H%M%S`
LOG_DIR=/var/log/$AIGO_USER/$SUBDIR

mkdir -p $LOG_DIR

inst_oacapture() {
        apt-get update | tee $LOG_DIR/apt-update_oacapture.log

        apt install --reinstall oacapture -y | tee $LOG_DIR/inst_oacapture.log

        apt clean
}

inst_skychart() {
	# Need to get 335 MB of archives.
	# After this operation, 864 MB of additional disk space will be used.
	FREE_SPACE=`df | grep root | awk '{print $4}'`
	if [ $FREE_SPACE -gt 1024000 ] ; then
		apt-key adv --keyserver keyserver.ubuntu.com --recv-keys AA716FC2 | tee $LOG_DIR/add-key_AA716FC2.log
		echo 'deb http://www.ap-i.net/apt unstable main' > /etc/apt/sources.list.d/skychart-unstable.list
		apt-get update | tee $LOG_DIR/apt-update_skychart.log

		apt install --reinstall skychart -y | tee $LOG_DIR/inst_skychart.log

		apt clean
	else
		FREE_SPACE=`df -h | grep root | awk '{print $4}'`
		dialog --colors --clear --title " \Z1Not enough free disk space !\Zn " --msgbox "\n\Z1Total available space \Zn\Zr\Z7$FREE_SPACE\Zn\Z1 bytes.\nNeed \Zr\Z71GB\Zn\Z1 bytes free disk space.\n\nFree disk space or expand filesystem.\Zn" 10 55
	fi
}

inst_firecapture() {
        apt-get update | tee $LOG_DIR/apt-update_firecapture.log

        apt install --reinstall firecapture -y | tee $LOG_DIR/inst_firecapture.log

        apt clean
}

inst_lin-guider-starspi() {
        apt-get update | tee $LOG_DIR/apt-update_lin-guider-starspi.log

        apt install --reinstall lin-guider-starspi -y | tee $LOG_DIR/inst_lin-guider-starspi.log

        apt clean
}


#=== Main Menu ===#
# Color
# \Z 正常底色,字色 0=深灰, 1=紅, 2=綠, 3=黃, 4=藍, 5=紫, 6=亮藍, 7=黑底, 白字
# \Zr\Z 灰字, 底色 0=灰, 1=紅, 2=綠, 3=黃, 4=藍, 5=紫, 6=亮藍, 7=白底, 黑字
# Bold is set by 'b', reset by 'B'. Reverse is set by 'r', reset by 'R'. Underline is set by 'u', reset by 'U'.
# Restore normal settings with "\Zn"

#BackTitle="\Z7AiGO $AIGO_VER\Zn - $BNAME v$VER"
BackTitle="\Z7AiGO $AIGO_VER\Zn - aigo-tools ver: $VER"

MenuTitle="\Zr\Z7 AiGO Software Configuration Tool (aigo_config) \Zn"

MenuHeight=25
MenuWidth=100
MenuItemHeight=10

Menu01Tag="1 oaCapture"

Menu02Tag="2 Skychart"

Menu03Tag="3 FireCapture"

Menu04Tag="4 Lin-guider StarsPi"

Menu05Tag="5"

Menu00Tag="0 Back"
Menu00Item="Back to main menu."

while true
do

CURRENT_SSID=`grep "^ssid=" /etc/hostapd/hostapd.conf | awk -F '=' '{print $2}'`
CURRENT_WPAPASS=`grep "^wpa_passphrase=" /etc/hostapd/hostapd.conf | awk -F '=' '{print $2}' | cut -c 1-3`"**"
CURRENT_WANIP=`ifconfig wlan0 | grep "inet addr" | awk '{print $2}' | awk -F ':' '{print $2}'`
CURRENT_LANIP=`ifconfig eth0 | grep "inet addr" | awk '{print $2}' | awk -F ':' '{print $2}'`

MenuText="\
WiFi SSID     : \Z4$CURRENT_SSID\Zn\n\
WiFi IP       : \Z4$CURRENT_WANIP\Zn\n\
WiFi Password : \Z4$CURRENT_WPAPASS\Zn\n\
LAN IP        : \Z4$CURRENT_LANIP\Zn\n\
\nYou can use the UP/DOWN arrow keys, the number keys 0-9 to choose an option.\n\
\nChoose the item"

# check is installed
`dpkg -s oacapture > /dev/null 2>&1`
if [ $? -eq 1 ] ; then
#	Installed01="\Zr\Z1QHY5.HEX problem\Zn"
	Menu01Item="\Zr\Z0Not Installed\Zn - Planetary imaging application for Linux and MacOS."
else
	Installed01=`dpkg -s oacapture | grep Version | awk -F ': ' '{print $2}'`
	Menu01Item="\Zr\Z4$Installed01\Zn - Planetary imaging application for Linux and MacOS."
fi

`dpkg -s skychart > /dev/null 2>&1`
if [ $? -eq 1 ] ; then
	Menu02Item="\Zr\Z0Not Installed\Zn - Cartes du Ciel planetarium software"
else
	Installed02=`dpkg -s skychart | grep Version | awk -F ': ' '{print $2}'`
	Menu02Item="\Zr\Z4$Installed02\Zn - Cartes du Ciel planetarium software"
fi

`dpkg -s firecapture > /dev/null 2>&1`
if [ $? -eq 1 ] ; then
	Menu03Item="\Zr\Z0Not Installed\Zn - THE LEADING PLANETARY CAPTURE TOOL"
else
	Installed03=`dpkg -s firecapture | grep Version | awk -F ': ' '{print $2}'`
	Menu03Item="\Zr\Z4$Installed03\Zn - THE LEADING PLANETARY CAPTURE TOOL"
fi

`dpkg -s lin-guider-starspi > /dev/null 2>&1`
if [ $? -eq 1 ] ; then
	Menu04Item="\Zr\Z0Not Installed\Zn - Lin_guider StarsPi patch"
else
	Installed04=`dpkg -s lin-guider-starspi | grep Version | awk -F ': ' '{print $2}'`
	Menu04Item="\Zr\Z4$Installed04\Zn - Lin_guider StarsPi patch"
fi


dialog --no-shadow --visit-items --colors --clear \
--default-item "0 Back" \
--cancel-label "Back" \
--backtitle "$BackTitle" \
--title "$MenuTitle" \
--menu "$MenuText" \
$MenuHeight $MenuWidth $MenuItemHeight \
"$Menu01Tag" "$Menu01Item" \
"$Menu02Tag" "$Menu02Item" \
"$Menu03Tag" "$Menu03Item" \
"$Menu04Tag" "$Menu04Item" \
"$Menu05Tag" "$Menu05Item" \
"$Menu00Tag" "$Menu00Item" \
2>"${INPUT}"

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

case $menuitem in
        "$Menu01Tag")
                inst_oacapture
                break
                ;;
        "$Menu02Tag")
                inst_skychart
                break
                ;;
        "$Menu03Tag")
                inst_firecapture
                break
                ;;
        "$Menu04Tag")
                inst_lin-guider-starspi
                break
                ;;
        "$Menu05Tag")
                ;;
        "$Menu00Tag")
                clear
                echo "Back to main menu."
                break
                ;;
        *)
                clear
                echo "Back to main menu."
                break
                ;;
esac

done

[ -f $OUTPUT ] && rm -f $OUTPUT
[ -f $INPUT ] && rm -f $INPUT

#clear
