#!/bin/sh
# Vector Linux Setup Configuration
# This script is supposed to be executed in a chrooted environment
# to configure the Vector Linux virgin
# Called by setup-install like this:
#    setup-config  <ROOT_DEVICE>
#
# We are going to do a quick setup thus this baby can boot properly 
# - initialisation
# - liloconf
# - 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"

#####################################################
# Arguments from second stage
ROOT_DEVICE=$2 
KEYMAP=$3

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

if [ "$1" != "vector-virgin" ]; then
    infobox "This script is supposed to be first installation only" "WARNING"
    sleep 2
fi

# 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
}

# Asy someting to impress
infobox "Checking and initializing ..." "VECTOR LINUX CONFIGURATION"

# Mount procfs
if ! mount | grep -e '^proc'; then
    MOUNT_PROC=1
    mkdir -p /proc   &> /dev/null
    mount -t proc none /proc &> /dev/null
fi

# Should mount devfs or udev here
# But ... I'm afraid it will hung. so don't

# Reset the library first
ldconfig
sleep 2

menu_start()
{
DIMENSION="20 64 6"
[ "$CMD" ] && DIMENSION="21 70 4"
TITLE="VECTOR LINUX CONFIGURATION"
TEXT="\n
Your system looks fine. Now we are going to configure it.
The most crucial step is installing LILO so you can boot
this system. The others are also important if you want 
this system to run properly.\n\n
Hit <OK> so we can begin."

$DCMD --backtitle "$BACKTITLE" --title "$TITLE" --menu "$TEXT" $DIMENSION \
"LILO"      "install Linux loader (and Windows too)" \
"AUTOSETUP" "detect and configure basic hardware" \
"NETWORK"   "configure networking" \
"XWINDOW"   "configure X-Window GUI system" \
"ALSA"      "configure sound system" \
"USER"      "set root password and add a user" \
2> $freply

    return $?
}

## AUTO SETUP hardware, X-Window, network, sound
menu_lilo()
{
    $vdir/vliloconf / $ROOT_DEVICE
}

menu_hw()
{
  DIMENSION="13 60 2"
  [ "$CMD" ] && DIMENSION="21 70 4"
  TITLE="HARDWARE AUTO SETUP"
  TEXT="\n
We are going to autodetect and configure the hardware.
This process is quite smart. However, many new or
too old hardware could confuse the autodetection.\n
So, you are the boss. Autosetup or not ?"

$DCMD --backtitle "$BACKTITLE" --title "$TITLE" --menu "$TEXT" $DIMENSION \
"AUTOSETUP"  "do it. Try my computer !" \
"SKIP"  "not now. I'll do it after reboot" \
2>$freply || return $?

   if [ "$(cat $freply)" = "AUTOSETUP" ]; then
        $vdir/vhwconf
   else
	return 0
   fi
}

menu_net()
{
    $vdir/vnetconf
}

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


menu_alsa()
{
    $vdir/valsaconf
}

# keymap is passed from STAGE1
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()
{
    infobox " Basic configuration has been performed"
    sleep 3
}


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
        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_lilo menu_hw menu_net menu_x menu_alsa \
    menu_kmap menu_zone menu_root menu_user menu_finish

clean_exit $?
