#!/bin/sh

# Setup is supposed to be from TUI only
CMD=""
DCMD="dialog"
WCMD="dialog"
BACKTITLE="Vector Linux Install"

# But debugging is easier from GUI
#if [ "$DISPLAY" ]; then
#  CMD="xterm -bg black -fg white"
#  DCMD="Xdialog"
#  WCMD="Xdialog --extra-button --extra-label Back"
#  BACKTITLE=""
#fi

fmenu=/tmp/setup.menu
freply=/tmp/setup.reply
flog=/tmp/setup.log

# from /etc/color
BLINK="\033[05m"   	
REVERSE="\033[07m"    	
BLACK="\033[0;30m"
BLUE="\033[0;34m"
LTBLUE="\033[1;34m"
WHITE="\033[1;37m"
NRED="\033[0;31m"
LTRED="\033[1;31m"
GREEN="\033[0;32m"
LTGREEN="\033[1;32m"
PURPLE="\033[0;35m"
LTPURP="\033[1;35m"
YELLOW="\033[1;33m"
BROWN="\033[0;33m"
CYAN="\033[0;36m"
LTCYAN="\033[1;36m"
DKGREY="\033[1;30m"
LTGREY="\033[0;37m"
OFF="\033[0m"

clean_exit()
{
   rm -f $fmenu
   rm -f $freply
   rm -f $freply
   exit $1
}

# For debugging
dbug ()
{
   if [ "$CMD" ]; then
     echo $*
   else
     echo $* >> /tmp/setup.log
   fi
}

## Manage back/OK/cancel
## Call this wizard menuA [ menuB [ ... ]] 
wizard()
{
   if [ -z "$1" ]; then
      clean_exit 0;
   fi

   local PROG=$1
   shift
   
   # Invoke the menu
   while [ 0 ]; do
     dbug $PROG
     $PROG
     STATUS=$?
     dbug $PROG result=$STATUS
     case $STATUS in
       0)   # OK
       wizard "$@"
       ;;
       2|3|255)   # Use ESC as back  too
       return 3
       ;;     
       1) # Cancel 
       confirm_quit
       ;;
       *)
       echo "Fatal error, aborting"
       clean_exit $STATUS
     esac
   done
}


infobox()
{
   TITLE=${2:-"INFO"}
   $DCMD --backtitle "$BACKTITLE" --title "$TITLE" --infobox "\n$1" 6 50
}

msgbox()
{
   $DCMD --backtitle "$BACKTITLE" --title "$2" --msgbox "\n$1" 0 0
}

errorbox()
{
   $DCMD --backtitle "$BACKTITLE" --title "$2" --ok-label "Cancel" --msgbox "\n$1" 0 0
}

confirm_quit()
{
   TEXT="\n
You are about to abort this installation.
Your system will not work properly.\n
Do you really want to quit ?"

   $DCMD --backtitle "$BACKTITLE" --title "QUIT" --defaultno \
   --yesno "$TEXT" 9 50 

   [ $? = 1 ] && return 0

   clean_exit 1
}
