#!/bin/sh
# Vector Linux Setup Stage 2
# This script is supposed to be executed the first boot of VL 
# Called by rc.S if /tmp/setup-stage2.conf is present
#
# We are going to do a quick setup thus this baby can boot properly 
# - initialisation
# - autosetup 
# - hwset
# - user setup
# - DONE
#
# (c) Eko M. Budi, 2004
# (c) Vector Linux, 2004
#
# Released under GNU GPL
#

export PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin:/opt/bin"

#####################################################
# Variables
KEYMAP=""
AUTOSETUP=""

# Ok, now get the functions
vdir=$(dirname $0)
. $vdir/vasm-functions


FSTAGE2="/tmp/setup-stage2.conf"

# source the configuration
if [ -r $FSTAGE2 ]; then
   . $FSTAGE2 
else
    if [ "$1" != "vector-virgin" ]; then
	msgbox "This configurator is for first installation only" "WARNING"
	sleep 2
    fi
fi

menu_start()
{
DIMENSION="20 64 7"
[ "$CMD" ] && DIMENSION="21 70 7"
TITLE="WELCOME TO VECTOR LINUX"
TEXT="\n
Wow, you are here at last. Vector Linux is ready at your service.
But for the first time, this virgin need a little finishing touch
to run properly.\n\n
Here is our job list, hit <OK> so we can begin:"

$DCMD --backtitle "$BACKTITLE" --title "$TITLE" --menu "$TEXT" $DIMENSION \
"AUTOSETUP" "detect and configure the basic hardware" \
"NETWORK"   "configure networking" \
"XWINDOW"   "configure X-Window GUI system" \
"ALSA"      "configure sound system" \
"KEYMAP"    "set keyboard mapping" \
"ZONE"      "set time zone" \
"USER"      "set root password and add a new user" \
2> $freply || return $?

    mkdir -p /etc/sysconfig
    touch /etc/sysconfig/vector

    # If previous autodetect was failed, don't do that again
    if [ "$AUTOSETOP" = "started" ]; then
	yesnobox "Previous autosetup was failed. Try again ?" "AUTOSETUP"
	[ $? !=  0 ] && return 0
    fi

    # Put a MARK of AUTODETECT
    echo "AUTOSETUP='started'" >> $FSTAGE2

    $vdir/vhwconf

    # Autodetection is passed, remove the mark
    cat $FSTAGE2 | grep -v "AUTOSETUP=" > $FSTAGE2

    return 0
}

menu_net()
{
    if grep -qe "alias *eth0" /etc/modules.conf; then
	$vdir/vnetconf    
    else
	return $1
    fi
}

menu_x()
{
    if [ -x /usr/X11R6/bin/XFree86 ]; then
	$vvdir/vmkfx86cfg
    elif [ -x /usr/X11R6/bin/Xorg ]; then
	$vdir/vxconf
    else
	if [ $1 = 0 ]; then
	    infobox "X-Windows is not present. Skipping"
	    sleep 2
	fi
	return $1
    fi
}

menu_alsa()
{
    $vdir/valsaconf
}

### keymap is passed from STAGE1

# Initialising & set keymap
mk_keymap() {
cat <<EOF
#!/bin/sh
#
# /etc/rc.d/rc.keymap
#
# This file is called from: /etc/rc.d/rc.M
#
# Linux `date '+%a %b %e %Y'`

# You can load another keyboard keymap e.g. 'loadkeys fr' = french etc.
# The keymaps are in the /usr/lib/kbd/keymaps/i386 directory.

if [ -x /usr/bin/loadkeys ]; then
loadkeys $MAPNAME

# This is so you can open other consoles using the left-[Alt] & [Up]-arrow key.
echo "alt keycode 103 = Spawn_Console" | /usr/bin/loadkeys
fi

EOF
}

menu_kmap()
{
    [ -z "$KEYMAP" ] && return $1

    # If the keymap is there, just create it
    if [ -r /usr/share/kbd/i386/qwerty/$KEYMAP.map.gz ] || \
       [ -r /usr/share/kbd/i386/qwertz/$KEYMAP.map.gz ] || \
       [ -r /usr/share/kbd/i386/azerty/$KEYMAP.map.gz ] || \
       [ -r /usr/share/kbd/i386/dvorak/$KEYMAP.map.gz ] ; then
	infobox "Setting up keymap $KEYMAP"
	mk_keymap > /etc/rc.d/rc.keymap
	chmod +x /etc/rc.d/rc.keymap
	sleep 2
	return $1
    else
	$vdir/vkmapset
	return $?
    fi
    return $1
}

menu_zone()
{
    $vdir/vzoneset
}

menu_root() {
## USER: change root passwd and add a new user
TEXT="\n
Linux is a secure system, thus every user must have an account to\n
login into the system. The first and the most powerfull account is\n
called a 'root'. You should login as root on special occasion only\n
such as administering the system. For normal every day working, use\n
an ordinary user account.\n 
The following steps allow you to change the root password and to\n
create a new ordinary user."

$DCMD --backtitle "$BACKTITLE" --title "SET USERS" \
--msgbox "$TEXT" 14 74 || return $?

$vdir/vpasswd root
}

menu_user() {
    $vdir/vuseradd
}

menu_finish()
{
TEXT="\n
Vector Linux is now fully ready. 
For further configuration, please
login as root and use VASM.\n\n
Enjoy ..."

$DCMD --backtitle "$BACKTITLE" --title "CONGRATULATION" \
--msgbox "$TEXT" 12 40 2> /dev/null

clear
rm -f $FSTAGE2

clean_exit 0
}

confirm_exit()
{
TEXT="\n
You may leave your system unstable.\n
Cancel anyway?"
    $DCMD --title "CANCEL INSTALLATION" --yesno "$TEXT" 7 44 2> /dev/null
    if [ $? = 0 ]; then
	rm -f $FSTAGE2
        clean_exit 1
    fi
}

## Manage back/OK/cancel
## Call this wizard menuA [ menuB [ ... ]] 
wizard1()
{
   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
       wizard1 "$@"
       STATUS=$?
       ;;
       2|3|255)   # BACK
       return 3
       ;;     
       1) # Cancel 
       confirm_exit
       ;;
     esac
   done
}

wizard1 menu_start menu_net menu_x menu_alsa \
    menu_kmap menu_zone menu_root menu_user menu_finish

rm -f $FSTAGE2
clean_exit $?
