#!/bin/sh
# Vector Linux SETUP INSTALL
# This script must be called when the media is ready on $SOURCE_MNT
#
# The DUMMY was invented on 5 August 2004.
# Do not claim it, even more patent it !!!!
#
# Author: Eko M. Budi for Vector Linux
# License : GNU GPL
#
# 20060315 : Eko M. Budi
#    - add lzma bulk decoding
#    - add optional mount points (home, opt, usr, var, tmp, /mnt/win)
#    - add tips on swap
#    - add Ctrl-C capability on checking

BACKTITLE="Vector Linux Install"

mke2fs -m 0 /dev/ram1
mount /dev/ram1 /tmp

vdir=$(dirname $0)
. $vdir/setup-functions

## Variables
BULK_FILES=""
PACK_FILES=""
PACKAGES_TXT="$SOURCE_MNT/packages/PACKAGES.TXT"
SEVERITY_TXT="$SOURCE_MNT/packages/SEVERITY.TXT"

DEV_SWAP=""

MP_ROOT="/"
DEV_ROOT=""
FS_ROOT=""

MP_HOME="/home"
DEV_HOME=""
FS_HOME=""

MP_OPT="/opt"
DEV_OPT=""
FS_OPT=""

MP_USR="/usr"
DEV_USR=""
FS_USR=""

MP_VAR="/var"
DEV_VAR=""
FS_VAR=""

MP_TMP="/tmp"
DEV_TMP=""
FS_TMP=""

MP_WIN="/mnt/win"
DEV_WIN=""
FS_WIN=""

SELECTED_PARTITIONS=""
MOUNTED_PARTITIONS=""

error() {
    for MNT in $MOUNTED_PARTITIONS; do
      umount $MNT 2>/dev/null
    done
    echo
    echo "ERROR! There was a problem!  Installation is not completed!"
    echo
    exit 
}

# Check the source, abort if not found
if [ ! -r $SETUP_CONF ]; then
    msgbox "Cannot find $SETUP_CONF on install media. Aborting" "ERROR" 
    clean_exit 1
fi

. $SETUP_CONF

# check the existence of BULK0 (the essential one)
VECLINUX_BULK=`echo $BULK0 | cut -f 1 -d ':'`
if [ ! -r $BULK_DIR/$VECLINUX_BULK ]; then
  msgbox "Cannot find $VECLINUX_BULK on install media. Aborting" "ERROR"
  clean_exit 1
fi
VLCONFIG_BULK=`echo $CONF1 | cut -f 1 -d ':'`
################################################################################
# CHECK MEDIA CHECKSUM
# Media has been mounted on $SOURCE_MNT 

bulkerror() {
  $DCMD --backtitle "$BACKTITLE" --title "ERROR" --yesno "\n
Something is wrong with the $1 file. If you 
are using a CD-ROM maybe it is dirty, damaged,
or it was created from a corrupt ISO file.\n
You should abort the installation. Agree?" 0 0
    if [ $? = 0 ]; then
	clean_exit 1 
    fi
}

build_check_menu()
{
   for line in "$BULK0" "$BULK1" "$BULK2" "$BULK3" "$BULK4" \
      "$BULK5" "$BULK6" "$BULK7" "$BULK8" "$BULK9"; do
      if [ "$line" ]; then
        bulk=`echo $line | cut -f 1 -d ':'`
        size=`ls -s1 $BULK_DIR/$bulk`
        size=`echo $size | cut -f 1 -d ' '`
        echo "'$bulk'" "'bulk size=$size KB' off \\"
      fi
   done
}

menu_check()
{
    TITLE="CHECK MEDIA"
DIMENSION="20 74 8"
    TEXT="\n
The installation files should be checked for errors, especially the
$VECLINUX_BULK. If there are any errors there's no point installing it.
The checking will take a few minutes. You may skip it if you don't
have time, but take the risk of possibly later failure.\n
Use [SPACE] to select the files you want to check then hit <Ok>:"

    echo '$WCMD --backtitle "$BACKTITLE" --title "$TITLE" \' > $fmenu
    echo '--checklist "$TEXT" $DIMENSION \' >> $fmenu
    build_check_menu >> $fmenu
    echo '2> $freply' >> $fmenu

    . $fmenu
    
    status=$?
    [ $status != 0 ] && return $status

    BULK_FILES=$(cat $freply | sed 's/"//g')

    dbug "Check=$BULK_FILES"
    ERROR_FILES=""
    for BULK in $BULK_FILES; do
    	infobox "Checking $BULK ... " "Please wait"
    	check-bulk $BULK_DIR/$BULK >> $flog 2>&1
	status=$?
	case $status in
	  0) continue
	  ;;
	  130)
    	    infobox "Checking $BULK was canceled" "Checking Media"
	    sleep 3
	    ;;
	  *)
	    bulkerror "$BULK (error code = $status)"
            ERROR_FILES="$ERROR_FILES $BULK"
	    ;;
	esac
    done
    return 0
}


##############################################################
# SWAP partitions
build_swap_menu()
{
   MENU=""
   while read LINE; do
      PART=$(echo $LINE | cut -f 1 -d ' ')
      SIZE=$(echo $LINE | cut -f 4 -d ' ')
      MENU="$MENU $PART size=$SIZE on"
   done
}

menu_swap()
{
    probepart | grep "Linux swap$" > /tmp/hdlist
    build_swap_menu < /tmp/hdlist

    if [ -z "$MENU" ]; then
	TITLE="NO SWAP" infobox "Skipping swap installation ..."
	sleep 3
	return 0
    fi

    TITLE="SWAP PARTITIONS"
    DIMENSION="20 72 4"
    TEXT="\n
Linux utilizes the swap partition(s) as virtual memory to expand
the capacity of the actual memory (RAM). Some tips:\n
- swap is optional, you may forget it if you have abundant RAM.\n
- the recomended space for swap partition is 2xRAM.\n
- add some more space, then you may use it as swap based tmpfs,\n
  which is faster than ordinary temporary files (/tmp).\n
- if you have multiple harddisks, put a swap partition on each\n
  harddisk to get the power of concurrent swap.\n
Use [SPACE] to enable the swap partition(s) then hit <Ok>:"

    $WCMD --backtitle "$BACKTITLE" --title "$TITLE" \
    --checklist "$TEXT" $DIMENSION $MENU 2> $freply

    status=$?
    [ $status != 0 ] && return $status

    DEV_SWAP=$(cat $freply | sed 's/"//g')
    dbug SWAP=$DEV_SWAP

    return 0
}

##################################################################################
# set ROOT PARTITIONS
build_root_menu()
{
   while read LINE; do
      PART=$(echo $LINE | cut -f 1 -d ' ')
      ## skip if in EXCLUDE_PARTITIONS
      if [ "$EXCLUDE_PARTITIONS" ]; then
        if echo "$EXCLUDE_PARTITIONS " | grep -q "$PART "; then
	  continue
	fi
      fi 
      ## skip if already mounted, should be SOURCE or MEDIA
      if mount | grep -qe "^$PART "; then
         continue
      fi
      ## if this is a raid, only raid1
      PART1=$(echo $PART | cut -b6-)
      RAID=$(grep -e "^$PART1" /proc/mdstat 2>/dev/null)
      if [ "$RAID" ]; then
        if ! echo $RAID | grep -q " raid1 "; then
	   continue
	fi
      fi
      SIZE=$(echo $LINE | cut -f 4 -d ' ')
      MENU="$MENU $PART size=$SIZE"
   done
}

dummy_box () {
$DCMD --backtitle "$BACKTITLE" \
--title "WAKE UP MATE !" --msgbox "\n
DUMMY is not to be chosen. I'll tell you the story.\n
Once upon a time, a vecteloper immediately pressed the [Enter]
key while half a sleep testing this program. In the morning,
he found out that his precious DATA partition had been 
helplessly formatted. In agony, he donated the DUMMY to prevent
you doing the same mistake.\n" 13 65 2> /dev/null
}

menu_root() {
    ## Select linux partitions
    probepart | grep -e " Linux$" | sed 's/\*/ /' > /tmp/hdlist
    
    DEV_ROOT=""
    MENU=""
    build_root_menu < /tmp/hdlist
    if [ -z "$MENU" ]; then
	errorbox "No linux partition found. Back to square one ..."
	clean_exit 0
    fi

while [ 0 ]; do
    TITLE="ROOT PARTITION"
    DIMENSION="20 70 7"
    TEXT="\n
Now we need a partition for root (the top level directory).
It will contain all system and program files, so make sure it 
has enough space. The basic install needs $HD_ROOT_MIN MB, while
the complete install requires around $HD_ROOT_MAX MB. If the
partition is too small, the installation will crash later!\n
Please choose the right root partition (I don't check):"

    $WCMD --backtitle "$BACKTITLE" --title "$TITLE" \
    --menu "$TEXT" $DIMENSION \
    "DUMMY" "do not choose this" \
    $MENU 2> $freply

    status=$?
    [ $status != 0 ] && return $status

    DEV_ROOT=$(cat $freply)
    dbug "ROOT=$DEV_ROOT"
    [ "$DEV_ROOT" != "DUMMY" ] && break
    
    dummy_box
    
done
return 0
}


#############################################################################
# FORMAT ROOT
menu_rootfs() {

TITLE="ROOT FILESYSTEM"
DIMENSION="20 76 3"
TEXT="\n
You have selected $DEV_ROOT for root installation. It will be
formatted, no matter what. If not sure, hit [Esc] now to go back.\n
Otherwise, let's choose the filesystem to format this partition.
The EXT2 filesystem has been the standard on Linux for many years.
The EXT3 system add journaling capability on top of EXT2, thus it is
more reliable although requires more space. Reiserfs is relatively new,
some benchmarks showed it is faster and stores more data than EXT3.\n
XFS combines advanced journaling technology with full 64-bit addressing 
and scalable structures and algorithms. \n
Please choose a file system to format $DEV_ROOT:"

    $WCMD --backtitle "$BACKTITLE" --title "$TITLE" \
    --menu "$TEXT" $DIMENSION \
"reiserfs" "The reiser journaling filesystem" \
"xfs"   "A high performance journaling file system" \
"jfs"   "IBM's Journaled File System (jfs)" \
"ext3"  "Extend ext2 with journaling file system" \
"ext2"  "Older proven Linux file system" \
2> $freply

    status=$?
    [ $status != 0 ] && return $status

    FS_ROOT=`cat $freply`
    
    dbug "FS_ROOT=$FS_ROOT"
    return 0
}


#############################################################################
# ADDITIONAL MOUNTS

# show dialog to choose file system
# set MOUNT_FS according to selection

menu_mount_fs() {

MOUNT_FS=""

TITLE="CHOOSE FILESYSTEM"
DIMENSION="20 72 6"
TEXT="\n
You have opted to mound $1 into $2. If this partition has been
properly formatted and contains usefull data, you may KEEP it.
Otherwise, it is better to format and clean it up. Formating with
reiserfs or ext3 is recommended since they offer journaling system
which is more reliable in case of unexpected computer power off.
Choose ext2 if you want more free space and faster access:"

    $WCMD --backtitle "$BACKTITLE" --title "$TITLE" \
    --menu "$TEXT" $DIMENSION \
"KEEP" "do not format $1" \
"KEEP" "do not format $1" \
"reiserfs" "format $1 with reiserfs (journaling)" \
"xfs"   "format $1 with xfs (journaling)" \
"jfs"   "format $1 with jfs (journaling)" \
"ext3"  "format $1 with ext3 fs (journaling)" \
"ext2"  "format $1 with ext2 fs (classic)" \
"KEEP"  "yeah, 3rd times. So you pay attention ;-)" \
2> $freply

    status=$?
    [ $status != 0 ] && return $status

    MOUNT_FS=`cat $freply`
    
    dbug "$1=$MOUNT_FS"
    return 0
}


build_mount_menu()
{
   NOLIST="$EXCLUDE_PARTITIONS $DEV_ROOT $DEV_HOME $DEV_OPT $DEV_USR $DEV_VAR $DEV_TMP $DEV_WIN"
   while read LINE; do
      PART=`echo $LINE | cut -f 1 -d ' '`
      ## Skip exclude partitions
      
      if echo "$NOLIST " | grep -q "$PART "; then
	  continue;
      fi
      ## skip if already mounted, should be SOURCE or MEDIA
      if mount | grep -qe "^$PART "; then
         continue
      fi
      SIZE=$(echo $LINE | cut -f 4 -d ' ')
      MENU="$MENU $PART size=$SIZE "
   done
}

# show dialog to choose devices 
# set MOUNT_DEV according to selection
menu_mount_dev() {
    ## emptying
    MOUNT_DEV=""
        
    ## Select linux partitions
    probepart | grep -e " Linux$" | sed 's/\*/ /' > /tmp/hdlist

    MENU=""
    build_mount_menu < /tmp/hdlist
    if [ -z "$MENU" ]; then
	errorbox "No more partition is available." "WARNING"
	return 0
    fi

    TITLE="MOUNT PARTITION"
    TEXT="\nPlease select the partition that will be mounted on $1:"
    DIMENSION="20 70 8"

    $WCMD --backtitle "$BACKTITLE" --title "$TITLE" \
    --menu "$TEXT" $DIMENSION \
    "NONE" "put $1 in the root partition" \
    $MENU 2> $freply

    status=$?
    if [ $status != 0 ]; then
      return $status
    fi
    
    SELECT=$(cat $freply)
    dbug "$1=$SELECT"
    if [ "$SELECT" != "NONE" ]; then
	MOUNT_DEV="$SELECT"
    fi
    return 0
}

menu_mount_win() {
    MOUNT_DEV=""
    
    ## Select windows partitions
    probepart | grep -e "^/dev.*NTFS$" | sed 's/\*/ /' > /tmp/hdlist
    probepart | grep -e "^/dev.*FAT32$" | sed 's/\*/ /' >> /tmp/hdlist
    probepart | grep -e "^/dev.*FAT$" | sed 's/\*/ /' >> /tmp/hdlist

    MENU=""
    build_mount_menu < /tmp/hdlist
    if [ -z "$MENU" ]; then
	errorbox "No more partition is available" "WARNING"
	echo ""
	return 0
    fi

    TITLE="SHARED WINDOWS/LINUX PARTITION"
    TEXT="\n
The $1 mount point is intended for sharing the data
between Linux/Windows, possibly on a dual-boot system.
You should have made and formatted this partition from
Windows first, because it is better like that.\n
Select the partition you want to use as $1:"
    DIMENSION="20 70 4"

    $WCMD --backtitle "$BACKTITLE" --title "$TITLE" \
    --menu "$TEXT" $DIMENSION \
    "NONE" "no shared folder please" \
    $MENU 2> $freply

    status=$?
    if [ $status != 0 ]; then
      return $status
    fi
    
    SELECT=$(cat $freply)
    dbug "$1=$SELECT"
    if [ "$SELECT" != "NONE" ]; then
	MOUNT_DEV="$SELECT"
    fi
    return 0
}


menu_mount()
{
while [ 1 ]; do    ## Select linux partitions
    if [ "$DEV_HOME" ]; then
	INFO_HOME="$DEV_HOME ($FS_HOME)"    
    else
	INFO_HOME="the place for users's precious data"    
    fi
    if [ "$DEV_OPT" ]; then
	INFO_OPT="$DEV_OPT ($FS_OPT)"    
    else
	INFO_OPT="optional or big programs like KDE, GNOME or Java are here"
    fi

    if [ "$DEV_USR" ]; then
	INFO_USR="$DEV_USR ($FS_USR)"    
    else
	INFO_USR="most of the main programs are here, including X11"    
    fi

    if [ "$DEV_VAR" ]; then
	INFO_VAR="$DEV_VAR ($FS_VAR)"    
    else
	INFO_VAR="for growing data, recomended for a server"    
    fi

    if [ "$DEV_TMP" ]; then
	INFO_TMP="$DEV_TMP ($FS_TMP)"    
    else
	INFO_TMP="small tmp may bost the speed (but swap+tmpfs is better)"
    fi

    if [ "$DEV_WIN" ]; then
	INFO_WIN="$DEV_WIN ($FS_WIN)"
    else
	INFO_WIN="Linux/Windows shared folder"
    fi

    TITLE="OPTIONAL PARTITIONS"
    TEXT="\n
After the root partition, Linux can mount additional partitions into some
standard folders as below. Particulary, a separate /home partition is
highly recomended for keeping the data intact in case you have to
reinstall this system in the future.\n
Please select the mount point folder:\n"
    $WCMD --backtitle "$BACKTITLE" --title "$TITLE" \
    --menu "$TEXT" 20 74 7 \
    "DONE" "no more additional partition, thank's" \
    "$MP_HOME" "$INFO_HOME" \
    "$MP_OPT"  "$INFO_OPT" \
    "$MP_USR" "$INFO_USR" \
    "$MP_VAR" "$INFO_VAR" \
    "$MP_TMP" "$INFO_TMP" \
    "$MP_WIN" "$INFO_WIN" \
    2> $freply

    status=$?
    [ $status != 0 ] && return $status

    MOUNT_POINT=$(cat $freply)
    dbug "MOUNT_POINT=$MOUNT_POINT"
    if [ "$MOUNT_POINT" = "DONE" ]; then
       return 0
    fi
        
    ## return variables
    MOUNT_DEV=""
    MOUNT_FS=""

    ## Windows
    if [ "$MOUNT_POINT" = "$MP_WIN" ]; then
       menu_mount_win $MOUNT_POINT
       status=$?
       [ $status != 0 ] && continue
       DEV_WIN=$MOUNT_DEV
       FS_WIN="KEEP"
       continue
    fi

    ## Linux
    menu_mount_dev $MOUNT_POINT
    status=$?
    [ $status != 0 ] && continue

    if [ "$MOUNT_DEV" ]; then
	menu_mount_fs $MOUNT_DEV $MOUNT_POINT
        status=$?
        [ $status != 0 ] && continue
    fi

    ## This should be simpler ...
    case "$MOUNT_POINT" in
	"$MP_HOME")
	    DEV_HOME=$MOUNT_DEV
	    FS_HOME=$MOUNT_FS
	    ;;
	"$MP_OPT")
	    DEV_OPT=$MOUNT_DEV
	    FS_OPT=$MOUNT_FS
	    ;;
	"$MP_USR")
	    DEV_USR=$MOUNT_DEV
	    FS_USR=$MOUNT_FS
	    ;;
	"$MP_VAR")
	    DEV_VAR=$MOUNT_DEV
	    FS_VAR=$MOUNT_FS
	    ;;
	"$MP_TMP")
	    DEV_TMP=$MOUNT_DEV
	    FS_TMP=$MOUNT_FS
	    ;;
    esac
done

}

###########################################################################
# Select optional packages
# The veclinux directory should contain the bz2 file 
build_bulk_menu()
{
   for line in "$BULK0" "$BULK1" "$BULK2" "$BULK3" "$BULK4" \
        "$BULK5" "$BULK6" "$BULK7" "$BULK8" "$BULK9"; do
	if [ "$line" ]; then
	  bulk=$(echo $line | cut -f 1 -d ':')
	  if echo ERROR_FILES | grep -q "$bulk" > /dev/null 2>&1; then
	    echo "'$bulk'" "'corrupted file !' off \\"
          elif echo $line | grep -q "required"; then
            echo $bulk \ >/tmp/bulk_req
	  else
	    desc=$(echo $(echo $line | cut -f 3 -d ':'))
	    size=$(echo $(echo $line | cut -f 2 -d ':'))
	    size=$(expr $size '/' 1024)
	    state="on"
	    if echo $line | grep -q "Opt"; then
	      state="off"
	    fi
	    if echo $BULK_FILES | grep -q "$bulk"; then
	      state="on"
	    fi
	    echo "'$bulk'" "'$desc [$size MB]' $state \\"
	  fi
      fi
  done
}

menu_bulks() {
if [ `ls $BULK_DIR/optional|wc -l` -lt 1 ];then
  build_bulk_menu > /dev/null
  BULKS=$(cat /tmp/bulk_req)
  return 0
fi

TITLE="BULKS SELECTION"
DIMENSION="20 74 8"
TEXT="\n
Great, now it's time to choose your software packages.
Vector Linux provides bulk files for base and highly integrated
packages, thus the installation will be a snap.\n
Just select the bulks you want (but remember your HD space):"

    echo '$WCMD --backtitle "$BACKTITLE" --title "$TITLE" \' > $fmenu
    echo '--checklist "$TEXT" $DIMENSION \' >> $fmenu
    build_bulk_menu >> $fmenu
    echo '2> /tmp/bulk_opt' >> $fmenu

    . $fmenu
    
    status=$?
    [ $status != 0 ] && return $status

    BULK_FILES=$(cat /tmp/bulk_opt | sed 's/"//g')
    BULKS=$(cat /tmp/bulk_req)
    return 0
}

###############################################################
# SELECT PACKAGES
# MUST have the /packages/PACKAGES.TXT

short_size()
{
    local S1=$1
    if [ $S1 -lt 10240 ]; then
	echo $S1
    else
	S1=`expr $S1 '/' 1024`
	if [ $S1 -lt 10240 ]; then
	    echo ${S1}K
	else
	    S1=`expr $S1 '/' 1024`
	    echo ${S1}M
	fi    
    fi
}

## Find pkg in the PACKAGES.TXT
## $1 = /lib/GROUP/PNAME
pkgfind()
{
    PNAME=${1##*/}
    PPATH=${1%/*}
    PLONG=""
    PLOCATION=""
    while read LINE; do
	case "${LINE%: *}" in
	"PACKAGE NAME")
	    PFULL=`echo ${LINE#*:}`
	    if [ "$PNAME" != `pkgname $PFULL` ]; then
		PFULL=""
	    fi
	    ;;
        "PACKAGE LOCATION")
	    if [ "$PFULL" ]; then
		PLOCATION=`echo ${LINE#*:}`
		PGROUP=`basename $PLOCATION`
		PLIB=`dirname $PLOCATION`
		PLIB=`basename $PLIB`
		if [ "$PLIB/$PGROUP" = "$PPATH" ]; then
		    echo ${PLOCATION#./}/$PFULL
		    return 0
		fi
	    fi
	    ;;
	esac
    done
    return 1
}

## Make pkg menu from PACKAGES.TXT
build_pkg_menu()
{
    PLONG=""
    PLOCATION=""
    PSIZE=""
    PNAME=""
    while read LINE; do
	case "${LINE%: *}" in
	    "")	;;
	    "PACKAGE NAME")
		PFULL=${LINE#*:}
		PNAME=`pkgname $PFULL`
		;;
	    "PACKAGE LOCATION")
		PLOCATION=`echo ${LINE#*:}`
		;;
	    "PACKAGE SIZE (uncompressed)")
		PSIZE=`echo ${LINE#*:}`
		;;
	    "$PNAME")
		PDESC=`echo ${LINE#*:} | sed "s/'//"`
		PGROUP=`basename $PLOCATION`
		PLOCATION=`dirname $PLOCATION`
		PLIB=`basename $PLOCATION`
		if echo $PDESC | grep -qe '(.*)'; then
		    PDESC=`echo $PDESC | cut -f2 -d'(' | cut -f1 -d')'`
		fi
		PSTATE="on"
		if [ -f $SEVERITY_TXT ]; then
		    if grep -qe "^${PNAME} *: *optional" $SEVERITY_TXT; then
			PSTATE="off"
		    fi
		fi
		echo "'$PLIB/$PGROUP/$PNAME' '$PDESC($PSIZE)' $PSTATE \\" >> $fmenu
		PNAME=""
		;;
	esac
    done
}

menu_pkg()
{

TITLE="PACKAGES SELECTION"
TEXT="\n
This menu allows you to add additional packages. They are organized
in library/GROUP. Some packages are related, such as backend/frontend,
and should be installed together. Please choose the packages:"

DIMENSION="20 76 10"

    echo '$WCMD --backtitle "$BACKTITLE" --title "$TITLE" \' > $fmenu
    echo '--checklist "$TEXT" $DIMENSION \' >> $fmenu
    build_pkg_menu < $PACKAGES_TXT
    echo '2> $freply' >> $fmenu

    . $fmenu
        
    status=$?
    [ $status != 0 ] && return $status

    PKG_FILES=$(cat $freply | sed 's/"//g')

    # count the selections
    PKG_NUM=0
    for PKG in $PKG_FILES; do
	PKG_NUM=`expr $PKG_NUM '+' 1`
    done
    
    return 0
}

###########################################################################
# Confirmation
menu_confirm() {

FORMATTED="${DEV_ROOT##*/}($MP_ROOT) "
MOUNTED=""
if [ "$DEV_HOME" ]; then
  if [ "$FS_HOME" = "KEEP" ]; then
    MOUNTED="$MOUNTED${DEV_HOME##*/}($MP_HOME) "
  else
    FORMATTED="$FORMATTED${DEV_HOME##*/}($MP_HOME) "
  fi
fi

if [ "$DEV_OPT" ]; then
if [ "$FS_OPT" = "KEEP" ]; then
  MOUNTED="$MOUNTED${DEV_OPT##*/}($MP_OPT) "
else
  FORMATTED="$FORMATTED${DEV_OPT##*/}($MP_OPT) "
fi
fi

if [ "$DEV_USR" ]; then
if [ "$FS_USR" = "KEEP" ]; then
  MOUNTED="$MOUNTED${DEV_USR##*/}($MP_USR) "
else
  FORMATTED="$FORMATTED${DEV_USR##*/}($MP_USR) "
fi
fi

if [ "$DEV_VAR" ]; then
if [ "$FS_VAR" = "KEEP" ]; then
  MOUNTED="$MOUNTED${DEV_VAR##*/}($MP_VAR) "
else
  FORMATTED="$FORMATTED${DEV_VAR##*/}($MP_VAR) "
fi
fi

if [ "$DEV_TMP" ]; then
if [ "$FS_TMP" = "KEEP" ]; then
  MOUNTED="$MOUNTED${DEV_TMP##*/}($MP_TMP) "
else
  FORMATTED="$FORMATTED${DEV_TMP##*/}($MP_TMP) "
fi
fi

if [ "$DEV_WIN" ]; then
if [ "$FS_WIN" = "KEEP" ]; then
  MOUNTED="$MOUNTED${DEV_WIN##*/}($MP_WIN) "
else
  FORMATTED="$FORMATTED${DEV_WIN##*/}($MP_WIN) "
fi
fi

unset CHICKEN
while [ 0 ]; do
TITLE="READY TO INSTALL"
DIMENSION="19 70 3"
TEXT="\n
I'm ready to install Vector Linux with these settings:\n
SWAP  : $DEV_SWAP\n
FORMAT: $FORMATTED\n
MOUNT : $MOUNTED\n
BULKS : $BULK_FILES\n
PACKS : $PKG_NUM\n
This is a one way ticket. If not sure, you can ABORT or get BACK.\n
Otherwise, say 'come on' whenever you are ready too:"

    $WCMD --backtitle "$BACKTITLE" --title "$TITLE" \
    --menu "$TEXT" $DIMENSION \
    "INSTALL" "come on mate, go for it !!!" \
    "BACK" "wait ... I changed my mind" \
    "ABORT" "I'm scared my system will turn into toast" \
    2> $freply

    status=$?
    [ $status != 0 ] && return $status

    case `cat $freply` in
       ABORT)
    	    if [ "$CHICKEN" ]; then
		clean_exit 0
	    fi
	    clear
	    for CC in $YELLOW $LTRED $CYAN $LTGREEN; do
		echo -e ${CC}Chicken !!! $OFF
		sleep 1
	    done
	    echo "I offer you one more ticket, and that's it ..."
	    CHICKEN="yes"
	    sleep 3
        ;;
       BACK)
            return 255
	    ;;
       INSTALL)
	    return 0
	    ;;
    esac
done
}


####################################################
# Install the system !!!!

format_error()
{
    $DCMD --backtitle "$BACKTITLE" --title "ERROR" --msgbox "\n
Cannot format $1 with $2 filesystem.
Maybe your harddisk has bad sectors or some other failure.\n
Sorry, but the installation must be aborted" 8 50
    clean_exit 1 
}

mount_error()
{
    $DCMD --backtitle "$BACKTITLE" --title "ERROR" --msgbox "\n
Cannot mount device $1 as $2.\n
Maybe the device has not been formatted, wrong file
system, or it is being mounted already.\n
Sorry, but the installation must be aborted" 9 50
    clean_exit 1 
}

tar_error()
{
    $DCMD --backtitle "$BACKTITLE" --title "ERROR" --msgbox "\n
Cannot extract $1.
Maybe the file is corrupt, or the partition is full.\n
The current installation is in unknown state.
We better abort and reinstall from the start." 9 50
    clean_exit 1 
}

# format <title> <device> <file_system>
format_dev()
{
    umount $MOUNT_DEV >/dev/null 2>&1
    dbug "Format $MOUNT_DEV with $MOUNT_FS"
    infobox "Formatting $MOUNT_DEV with $MOUNT_FS" "$TITLE"
    case $MOUNT_FS in
    "ext2")
	mke2fs -q $MOUNT_DEV > /dev/null 2>&1
	;;
    "ext3")
	mke2fs -q -j $MOUNT_DEV > /dev/null 2>&1
	;;
    "reiserfs")
	mkreiserfs --format 3.6 -ff $MOUNT_DEV > /dev/null 2>&1
	;;
    "xfs")
        mkfs.xfs -f $MOUNT_DEV >/dev/null 2>$1
        ;;   
    "jfs")
        mkfs.jfs -q $MOUNT_DEV >/dev/null 2>$1
        ;;   
    esac
}

# mount <title> <device> <mount_point> <file_system>
# set the actual MOUNT_FS
mount_dev()
{
    dbug "Mount $MOUNT_DEV with $MOUNT_FS"
    infobox "Mounting $MOUNT_DEV with $MOUNT_FS ..." "$TITLE"
    umount $MOUNT_TARGET > /dev/null 2>&1
    mkdir -p $MOUNT_TARGET > /dev/null 2>&1
    if [ "$MOUNT_FS" != "auto" ]; then
        if mount $MOUNT_DEV $MOUNT_TARGET -t $MOUNT_FS >> $flog 2>&1; then
	    sleep 2
	    return 0
	fi
    fi
    
    ## auto or fail .. try again in this order ...
    for TT in reiserfs xfs jfs ext3 ext2 vfat ntfs auto; do
      if mount $MOUNT_DEV $MOUNT_TARGET -t $TT >> $flog 2>&1; then
	if [ "$MOUNT_FS" = "auto" ]; then
	  MOUNT_FS=`mount | grep -e "^$MOUNT_DEV" | cut -f 5 -d ' '`
	else
	  MOUNT_FS="$TT"
	fi
	sleep 1
	return 0
      fi
    done
    # fail til the last attempt
    return 1
}

umount_all() {
#  umount $ROOT_MNT/sys 1>/dev/null 2>&1
#  umount $ROOT_MNT/proc 1>/dev/null 2>&1
  for DEV in $DEV_HOME $DEV_OPT $DEV_USR $DEV_VAR; do
    umount -v $DEV  1>>$flog 2>&1
  done
  umount -v $DEV_ROOT 1>>$flog 2>&1
}

# format_mount <NAME> <device> <mount_point> <file_system> 
# set the actual MOUNT_FS
format_mount() {
    TITLE=$1
    MOUNT_DEV=$2
    MOUNT_TARGET=$3
    MOUNT_FS=$4
    
    # skip if no device
    if [ -z $MOUNT_DEV ]; then
       return 0
    fi
   
    # format if MOUNT_FS is defined
    case "$MOUNT_FS" in
      reiserfs|ext3|ext2|xfs|jfs) 
         if ! format_dev $TITLE $MOUNT_DEV $MOUNT_FS; then
	     umount_all
	     format_error $HOME_DEV $HOME_FS
	     exit 100
	 fi
	 ;;
      *)
         MOUNT_FS="auto"
	 ;;
    esac

    # try to mount the device
    if ! mount_dev $TITLE $MOUNT_DEV $MOUNT_TARGET $MOUNT_FS; then
       umount_all
       mount_error $MOUNT_DEV $MOUNT_FS
       exit 101
    fi

    # success return, and MOUNT_FS is set
}


# Format & mount the devices then extract the packages
menu_install1()
{
infobox "You may leave for a moment if you want" "Here we go..."
sleep 1

# Making and activating swap devices
if [ "$HOSTED" ]; then
    infobox "Hosted install, swap initialization is skipped" "WARNING"
    sleep 1
else
    for DEV in $DEV_SWAP; do
	infobox "Making swap partition $DEV ..." "SWAP"
	if mkswap -c $DEV > /dev/null 2>&1; then
	    echo "$DEV   none   swap   sw   0  0" >> /etc/fstab
	else
	    infobox "Cannot make swap on $DEV !" "WARNING"
	    sleep 2
	fi
    done
    swapon -a > /dev/null 2>&1
fi

# Formatting and mounting ROOT FS
format_mount "ROOT" "$DEV_ROOT" "$ROOT_MNT" "$FS_ROOT"
FS_ROOT=$MOUNT_FS

# Formatting and mounting additionals
format_mount "HOME" "$DEV_HOME" "$ROOT_MNT$MP_HOME" "$FS_HOME"
FS_HOME=$MOUNT_FS
format_mount "OPT" "$DEV_OPT" "$ROOT_MNT$MP_OPT" "$FS_OPT"
FS_OPT=$MOUNT_FS
format_mount "USR" "$DEV_USR" "$ROOT_MNT$MP_USR" "$FS_USR"
FS_USR=$MOUNT_FS
format_mount "VAR" "$DEV_VAR" "$ROOT_MNT$MP_VAR" "$FS_VAR"
FS_VAR=$MOUNT_FS
format_mount "TMP" "$DEV_TMP" "$ROOT_MNT$MP_TMP" "$FS_TMP"
FS_TMP=$MOUNT_FS

# do not mount WIN
FS_WIN=`probepart | grep "$DEV_WIN"`
if echo $FS_WIN | grep -q FAT32 ; then
  FS_WIN="vfat"
elif echo $FS_WIN | grep -q NTFS ; then
  FS_WIN="ntfs"
else
  FS_WIN="auto"
fi

# umount /mnt/win, since we dont need it during installation
umount $DEV_WIN 1>/dev/null 2>&1

if ! cat /proc/cmdline | grep -q light-setup; then
    ($vdir/setup-credit &) 2> /dev/null
fi

# OK, install the bulks
# Add VECLINUX_BULK since it is a must
if ! echo $BULK_FILES | grep -q $VECLINUX_BULK; then
    BULK_FILES="$VECLINUX_BULK $BULKS $BULK_FILES"
fi
for bulk in $BULK_FILES; do
    size=1024
    for line in "$BULK0" "$BULK1" "$BULK2" "$BULK3" "$BULK4" \
       "$BULK5" "$BULK6" "$BULK7" "$BULK8" "$BULK9"; do
       bulk1=$(echo $line | cut -f 1 -d ':')
       if [ "$bulk" = "$bulk1" ]; then
	    size=$(echo $line | cut -f 2 -d ':')
            break         
       fi
    done
    echo "0" > /tmp/setup.status
    (install-bulk "$BULK_DIR/$bulk" "$ROOT_MNT" &) 2> /dev/null
    $vdir/dfgauge $bulk $size $DEV_ROOT $DEV_HOME $DEV_OPT $DEV_USR $DEV_VAR $DEV_TMP
    #sleep 1
    if [ "$(cat /tmp/setup.status)" != "0" ]; then
	killall setup-credit > /dev/null 2>&1
	sleep 0
	umount_all
	tar_error $bulk
    fi
sleep .1
killall dfgauge >/dev/null 2>$1
done

# Install packages
(
if [ "$PKG_FILES" ]; then
    rm -f /tmp/pkgerror &> /dev/null
    PROGRESS=0
    for PKG in $PKG_FILES; do
	LNAME=`cat $PACKAGES_TXT | pkgfind $PKG`
	PROGRESS=`expr $PROGRESS '+' 1`
	pct=`expr 99 '*' $PROGRESS '/' $PKG_NUM`
	echo "XXX"
	echo $pct
	echo "\nInstalling $PKG ($PROGRESS of $PKG_NUM)"
	echo "XXX"
        if [ $LNAME ]; then
    	    install-pkg $SOURCE_MNT/packages/$LNAME "$ROOT_MNT" > /dev/null 2>&1
	    if [ $? != 0 ]; then
		echo $PKG >> /tmp/pkgerror
	    fi
	else
	    echo $PKG >> /tmp/pkgerror
	fi
	sleep 1
    done
fi
echo "XXX"
echo "99"
echo "\nCopying documentation files"
echo "XXX"

### install vlconfig2.tlz after packages
for bulk in $VLCONFIG_BULK; do
    size=1024
    for line in "$CONF1"; do
       bulk1=$(echo $line | cut -f 1 -d ':')
       if [ "$bulk" = "$bulk1" ]; then
	    size=$(echo $line | cut -f 2 -d ':')
            break         
       fi
    done
    echo "0" > /tmp/setup.status
    (install-bulk "$BULK_DIR/$bulk" "$ROOT_MNT" &) 2> /dev/null
    $vdir/dfgauge $bulk $size $DEV_ROOT $DEV_HOME $DEV_OPT $DEV_USR $DEV_VAR $DEV_TMP
    sleep 1
    if [ "$(cat /tmp/setup.status)" != "0" ]; then
	killall setup-credit > /dev/null 2>&1
	sleep 0
	umount_all
	tar_error $bulk
    fi
done


if [ -d $DOC_DIR ] && mkdir -p $ROOT_MNT/usr/doc/VLHelpCenter; then
    cp -af $DOC_DIR/* $ROOT_MNT/usr/doc/VLHelpCenter 
    chown -R root:root $ROOT_MNT/usr/doc/VLHelpCenter
    find $ROOT_MNT/usr/doc/VLHelpCenter | while read line; do
	if [ -d $line ]; then
	    chmod 755 $line &> /dev/null
	else 
	    chmod 644 $line &> /dev/null
	fi
    done
fi

chmod 0440 $ROOT_MNT/etc/sudoers

. $SOURCE_MNT/veclinux/SETUP.CONF
echo "$VERSION built on $builddate">$ROOT_MNT/etc/vector-version
echo "$VERSION Installed on $(date)">$ROOT_MNT/var/log/pkgtools

echo "XXX"
echo "100"
echo "\nThat's all !"
echo "XXX"

) | dialog --backtitle " " --title "A bit more ..." --gauge "" 8 66 0

killall setup-credit > /dev/null 2>&1
sleep 0
infobox "Installation is DONE"
sleep 2

}

fs_options()
{
    case $1 in
    "reiserfs")
	echo "noatime"
	;;
    "vfat")
	echo "fmask=111,dmask=0,gid=users,shortname=mixed,quiet,user"
	;;
    "ntfs")
	echo "umask=0,gid=users,user"
	;;
    "ext2"|"ext3"|*)
        echo "defaults"
	;;
    esac
}

mk_fstab_new()
{
# Header
cat <<EOF
# /etc/fstab: static file system information.
#
# The following is an example. Please see fstab(5) for further details.
# Please refer to mount(1) for a complete description of mount options.
#
# Format:
# <file system>   <mount point>   <type>   <options>   <dump>	<pass>
#
# dump(8) uses the <dump> field to determine which file systems need
# to be dumped. fsck(8) uses the <pass> column to determine which file
# systems need to be checked--the root file system should have a 1 in
# this field, other file systems a 2, and any file systems that should
# not be checked (such as MS-initrd/mnt or NFS file systems) a 0.

# The Linux partitions
EOF

# Linux partitions
echo "$DEV_ROOT $MP_ROOT $FS_ROOT " `fs_options $FS_ROOT` " 0  1"
[ "$DEV_HOME" ] && echo "$DEV_HOME $MP_HOME $FS_HOME" `fs_options $FS_HOME` " 0 2"
[ "$DEV_OPT" ] && echo "$DEV_OPT $MP_OPT $FS_OPT " `fs_options $FS_OPT` " 0 2" 
[ "$DEV_USR" ] && echo "$DEV_USR $MP_USR $FS_USR " `fs_options $FS_USR` " 0 2"
[ "$DEV_VAR" ] && echo "$DEV_VAR $MP_VAR $FS_VAR " `fs_options $FS_VAR` " 0 2"
[ "$DEV_TMP" ] && echo "$DEV_TMP $MP_TMP $FS_TMP " `fs_options $FS_TMP` " 0 2"

# Windows partitions
cat <<EOF

# Shared Windows/Linux partition
#/dev/hda1   /mnt/dos  msdos  umask=0   0  0
#/dev/hda1   /mnt/win  vfat   fmask=111,dmask=0,quiet,shortname=mixed,user  0  0
#/dev/hda1   /mnt/win  ntfs   umask=0   0  0
EOF
[ "$DEV_WIN" ] && echo "$DEV_WIN $MP_WIN $FS_WIN " `fs_options $FS_WIN` " 0 0"


cat <<EOF

# Floppy disks
# The 'noauto' option indicates that the file system should not be mounted
# with 'mount -a' 'user' indicates that normal users are allowed to mount
# the file system.
/dev/fd0   /mnt/floppy	auto	defaults,noauto,user	0 0
#/dev/fd1  /mnt/floppy	auto	defaults,noauto,user	0 0

# If you have a ls-120 floppy drive, it could be on /dev/hda b c d etc.
#/dev/hdd	/mnt/ls120	auto	defaults,noauto,user	0 0

# CDROM, CDWRITER, DVD 
/dev/cdrom    /mnt/cdrom	iso9660	defaults,noauto,ro,user	0 0
#/dev/cdwriter /mnt/cdwriter	iso9660	defaults,noauto,rw,user	0 0
#/dev/dvd      /mnt/dvd 	auto	defaults,noauto,ro,user	0 0

# NFS file systems:
#linux01.gwdg.de:/suse/6.3/i386.de  /mnt/nfs  nfs  defaults  0 0

# proc file system:
proc	/proc	proc	defaults	0 0

# Unix98 devpts filesystem:
none  /dev/pts  devpts  gid=5,mode=666  0 0

# Shared memory filesystem:
tmpfs   /dev/shm    tmpfs defaults 0  0 

# Basic USB filesystem
sysfs  /sys  sysfs  defaults  0 0

# example of a VFAT USB pendrive
#/dev/sda1  /mnt/pendrive vfat   fmask=111,dmask=0,noauto,user,quiet,shortname=mixed 0 0

EOF

# Swap partitions
# 
cat <<EOF

# Swap partitions
# The 'sw' option means auto activating with 'swapon -a'.
EOF
for DEV in $DEV_SWAP; do
    echo "$DEV   none   swap   sw   0  0"
done

}

# Setup the TARGET, then launch the configuration
menu_install2()
{
# get  keymap
if [ -r $SETUP_KEYMAP ]; then
   KEYMAP=$(cat $SETUP_KEYMAP)
fi

dbug "Making fstab" 
mkdir -p $ROOT_MNT/etc/rc.d
mk_fstab_new > $ROOT_MNT/etc/fstab

# Make a fake mtab
echo "$DEV_ROOT / $FS_ROOT defaults 0 1"  > $ROOT_MNT/etc/mtab

# assume this will always success
mkdir -p $ROOT_MNT/proc
mount -v none $ROOT_MNT/proc -t proc >> $flog 2>&1
echo "none /proc proc defaults 0 0" >> $ROOT_MNT/etc/mtab

if grep -w sysfs /proc/filesystems 1> /dev/null 2>&1 ; then
  mkdir -p $ROOT_MNT/sys
  mount -t sysfs none $ROOT_MNT/sys >>$flog 2>&1
  echo "none /sys  sysfs defaults 0 0" >> $ROOT_MNT/etc/mtab
fi

[ "$DEV_HOME" ] && echo "$DEV_HOME $MP_HOME $FS_HOME defaults 0 2" >> $ROOT_MNT/etc/mtab
[ "$DEV_OPT" ] && echo "$DEV_OPT $MP_OPT $FS_OPT defaults 0 2" >> $ROOT_MNT/etc/mtab
[ "$DEV_USR" ] && echo "$DEV_USR $MP_USR $FS_USR defaults 0 2" >> $ROOT_MNT/etc/mtab
[ "$DEV_VAR" ] && echo "$DEV_VAR $MP_VAR $FS_VAR defaults 0 2" >> $ROOT_MNT/etc/mtab
[ "$DEV_TMP" ] && echo "$DEV_TMP $MP_TMP $FS_TMP defaults 0 2" >> $ROOT_MNT/etc/mtab

# then here we go
dbug "Going chroot config-stage1"
if [ "$HOSTED" ]; then
    chroot $ROOT_MNT /sbin/config-stage1 vector-virgin-short $DEV_ROOT $KEYMAP
else
    chroot $ROOT_MNT /sbin/config-stage1 vector-virgin $DEV_ROOT $KEYMAP
fi
result=$?

chroot $ROOT_MNT /sbin/ldconfig

dbug "Unmounting ..."
sync
#umount $ROOT_MNT/dev 1>/dev/null 2>&1
umount -v $ROOT_MNT/sys 1>>$flog 2>&1
umount -v $ROOT_MNT/proc 1>>$flog 2>&1
umount_all 

if [ $result != 0 ]; then

umount $SOURCE_MNT >/dev/null 2>&1
swapoff -a >/dev/null 2>&1

$DCMD --backtitle "$BACKTITLE" --title "HMMMMMM...." --msgbox "\n
Vector Linux has been installed on your system,
but I'm not sure about the configuration.\n
So, let's reboot and see what will happen." \
10 54 2> /dev/null

else

umount $SOURCE_MNT >/dev/null 2>&1
swapoff -a >/dev/null 2>&1

$DCMD --backtitle "$BACKTITLE" \
--title "LET'S THE GAME BEGIN" --msgbox "\n
Vector Linux has been installed on this computer.\n
We are going to reboot. If there is a CDROM or floppy 
disk in the drive, take it out now then hit <Ok>.\n
After rebooting, LILO will present you a boot menu.\n
Please select 'Linux' to start your new Vector Linux." \
11 62 2> /dev/null
fi

reboot_exit
}

#############################################################################
# MAIN PROGRAM

echo VECTOR LINUX SETUP > $flog

## Just to make sure, lets cleanup first
umount_all
if umount $ROOT_MNT 2>&1 | grep "device is busy"; then

   $DCMD --backtitle "$BACKTITLE" --title "ERROR" --msgbox "\n
Strange, $ROOT_MNT is busy. Something is going wrong.\n
Maybe we need to reboot first" \
0 60 2> /dev/null

reboot_exit

fi

TITLE="VECTOR LINUX INSTALL"
TEXT="\n
Alright, let's do it. We are about to install Vector Linux then 
configure it. Here is the plan, you may select the starting step
to skip the optional steps.\n
We will begin as soon as you hit <OK>:"

$DCMD --backtitle "$BACKTITLE" --title "$TITLE" --menu "$TEXT" 20 70 8 \
"CHECK"  "check installation files (optional)" \
"SWAP"   "select swap partition (recomended)" \
"ROOT"   "select root partition (mandatory)" \
"MOUNT"  "select additional partitions" \
"BULK"   "choose the integrated packages to install" \
"PACKAGE"  "choose optional packages to install" \
"INSTALL" "install Vector Linux into the harddisk" \
"CONFIGURE" "continue with the configuration" 2> $freply

    status=$?
    [ $status != 0 ] && clean_exit 0

    REPLY=`cat $freply`
    case "$REPLY" in
	"CHECK")
	wizard menu_check menu_swap \
	    menu_root menu_rootfs menu_mount menu_bulks menu_pkg \
	    menu_confirm menu_install1 menu_install2 
	    ;;
	"SWAP")
	wizard menu_swap \
	    menu_root menu_rootfs menu_mount menu_bulks menu_pkg \
	    menu_confirm menu_install1 menu_install2 
	    ;;
	*)
	wizard menu_root menu_rootfs menu_mount menu_bulks menu_pkg \
	    menu_confirm menu_install1 menu_install2 
	    ;;
    esac

clean_exit 2

