#!/bin/sh
#
# General purpose settings. Sourced by various setup scripts
#
# Author: Eko M. Budi for Vector Linux
# License : GNU GPL


## Setting, this will be overrode by setup.info, if found

# Distro name
DISTRO="Vector Linux"

# version
VERSION=""

# Formatted version must be 8 characters
VERSION8="       "

# Harddisk requirements (in MB)
HD_ROOT_MIN="1000"
HD_ROOT_MAX="2000"
HD_SWAP="64"
HD_HOME="500"

# veclinux essential BULK NAME
VECLINUX_BULK="required/veclinux.tlz"
BULK0="$VECLINUX_BULK:819200:Base system (absolutely required)"

# Standard directory and files
ROOT_MNT="/mnt/target"
HOME_MNT="/mnt/target/home"
SOURCE_MNT="/mnt/source"
TARGET_MNT="/mnt/target"
MEDIA_MNT="/mnt/media"

BULK_DIR=$SOURCE_MNT/veclinux
PACK_DIR=$SOURCE_MNT/packages
DOC_DIR=$SOURCE_MNT/doc
SETUP_CONF=${BULK_DIR}/SETUP.CONF
README=${SOURCE_MNT}/README

SETUP_KEYMAP=/tmp/setup.keymap

# The DIALOG 
CMD=""
DCMD="dialog"
WCMD="dialog"
BACKTITLE=${BACKTITLE:-"Vector Linux, when choice matters !"}

# Global variables
fmenu=/tmp/setup.menu
freply=/tmp/setup.reply
flog=/tmp/setup.log

# Colors
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"

# Calculate the swap, twice the RAM or 512 max !
calculate_swap()
{
HD_SWAP=128
MEM=$(echo `cat /proc/meminfo | grep 'MemTotal:'` | cut -f 2 -d ' ')
if [ "$MEM" ]; then
    if [ $MEM -gt 196 ]; then
	HD_SWAP="512"
    elif [ $MEM -gt 127 ]; then
	HD_SWAP="256"
    elif [ $MEM -gt 63 ]; then
	HD_SWAP="128"
    else
	HD_SWAP="64"
    fi
fi
}

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

reboot_exit()
{
   clear
   umount $HOME_MNT > /dev/null 2>&1
   umount $ROOT_MNT > /dev/null 2>&1
   if [ "$HOSTED" ]; then
      exit 255
   fi
   umount $SOURCE_MNT > /dev/null 2>&1
   umount $MEDIA_MNT > /dev/null 2>&1
   echo -e "${YELLOW}Rebooting the system in 3 seconds ....${OFF}"
   sync 2>/dev/null
   reboot
   sleep 10
   exit 255
}

# For debugging
dbug ()
{
   if [ "$CMD" ]; then
     echo "$@"
   else
     echo "$@" >> $flog
   fi
}

infobox()
{
  TEXT="\n$1"
  WIDTH=`expr ${#1} + 6`
  if [ "$2" ]; then
       $DCMD --backtitle "$BACKTITLE" --title "$2" --infobox "\n$1" 5 $WIDTH
  else
       $DCMD --backtitle "$BACKTITLE" --infobox "\n$1" 5 $WIDTH
  fi    
}

msgbox()
{
   local BTITLE=${2:-INFO}
   $DCMD --backtitle "$BACKTITLE" --title "$BTITLE" --msgbox "\n$1" 0 0
}

errorbox()
{
    local BTITLE=${2:-ERROR}
    $DCMD --backtitle "$BACKTITLE" --title "$BTITLE" --msgbox "\n$1" 0 0
}

confirm_quit()
{
   local TEXT="\n
Are you sure to abort this process ?"

   $DCMD --backtitle "$BACKTITLE" --title "CANCEL" --defaultno \
   --yesno "$TEXT" 0 0

   [ $? = 1 ] && return 0

   clean_exit 0
}

## synch before umount
sumount() {
    sync
    umount $1
}

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

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