#!/bin/sh
# Vector Linux First Time Configuration
#
# This script is a bit complicated, because there are many scenarios
# during the SETUP (stage1) and the FIRST BOOT (stage2)
#
# 1. STAGE 1
# For stage1, this script is called by setup-install (in the initrd)
# There are two possible cases: NORMAL and SHORT 
#
# 1.a. NORMAL STAGE 1
# This happen during normal setup from CDROM, or Windows loadlin
# The setup-install calls this script like this: 
#   chroot /mnt/target /sbin/config-stage1 vector-virgin <root-device> [$KEYMAP]
#
# Then, there are 3 possible scenarios:
#
# 1.a.1 NORMAL STAGE 1 - CORRECT KERNEL
# If the running kernel is present on the installed system,
# do FULL config then prepate the LIGHT stage2
#
# 1.a.2 NORMAL STAGE 1 - SKIP
# The kernel is correct, but user choose SKIP
# do SHORT config then prepare the HEAVY stage2
#
# 1.a.3. NORMAL STAGE 1 - INCORRECT KERNEL 
# The kernel is not present on the installed system.
# do SHORT config then prepare the HEAVY stage2
# Note: this feature allows the floppy install using small 2.4 kernel 
# although the installed system has the big 2.6 kernel
#
# 1.b. SHORT STAGE 1
# This is the case during direct ISO install by vinstall
# The setup-install calls this script like this: 
#    chroot /mnt/target /sbin/config-stage1 vector-virgin-short <root-device> 
#
# Because the running Linux environment is a stranger, safe option is
# do the SHORT config then prepare the HEAVY stage2
#
#
# 2. STAGE 2
# Stage 2 is prepared by STAGE-1 by making an auto-init script 
# /var/log/setup/init/config-stage2
# That auto-init script will be called by rc.S during the boot.
# According on how it is prepared by STAGE-1, the config-stage2 may
# call this script as LIGHT or HEAVY 
#
# 2.a. STAGE 2 HEAVY
# Called like this by config-stage2
#    config-stage1 vector-virgin-heavy <root-device> [$KEYMAP]
# This will do the FULL config
#
# 2.b. STAGE 2 LIGHT
# Called like this by config-stage2
#    config-stage1 vector-virgin-light <root-device> [$KEYMAP]
# This only setup the basic service and congratulates user
#
# Hah ... I can't believe I made this script.
#
# (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"

#####################################################
# Init some variables
RUN_MODE=$1
ROOT_DEVICE=$2 
KEYMAP=$3
KERNEL_VERSION=`uname -r`
SETUP_DIR="/var/log/setup"
INIT_DIR="$SETUP_DIR/init"
TMP_DIR="$SETUP_DIR/tmp"

ACTION_LIST=""

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

check_root
check_xwindow

#####################################################

## Making config-stage2
set_stage2_light()
{
mkdir -p $INIT_DIR
chmod 700 $INIT_DIR
cat <<EOF > $INIT_DIR/config-stage2
#!/bin/sh

exec /sbin/config-stage1 vector-virgin-light $ROOT_DEVICE $KEYMAP

EOF
chmod a+x $INIT_DIR/config-stage2
}

set_stage2_heavy()
{
mkdir -p $INIT_DIR
chmod 700 $INIT_DIR
cat <<EOF > $INIT_DIR/config-stage2
#!/bin/sh

exec /sbin/config-stage1 vector-virgin-heavy $ROOT_DEVICE $KEYMAP

EOF
chmod a+x $INIT_DIR/config-stage2
}

# Set default boot mode 
set_bootmode()
{
   finit=/etc/inittab
   cat $finit \
   | sed "s/^id:[0-9]:initdefault:/id:$1:initdefault:/" > ${finit}.new \
   && mv -f ${finit}.new $finit
}

# Set default boot mode 
get_bootmode()
{
   BOOT_MODE=2
   finit=/etc/inittab
   LINE=`grep "^id:[0-9]:initdefault:"`
   if [ $LINE ]; then
      BOOT_MODE=`echo $LINE | cut -f2 -d:`
   fi
}

# Initialising & set keymap
mk_keymap() {
MAPNAME=$1
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
}

set_keymap() {
## Detect if we need a KEYMAP menu
KMAPSET="off"
if [ "$KEYMAP" ]; then
    # 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 ]; then
	infobox "Setting up keymap $KEYMAP"
	mk_keymap $KEYMAP > /etc/rc.d/rc.keymap
	chmod +x /etc/rc.d/rc.keymap
	sleep 2
    else
	KMAPSET="on"
        KEYMAP="us"
    fi
else
    KEYMAP="us"
fi
}

set_hwclock()
{
    # Set the system time from the hardware clock using hwclock --hctosys.
    hwclock --localtime --hctosys  >> /dev/null
}

##################################################################
# INDIVIDUAL DIALOGS

#---------------------------------------------
menu_keymap()
{
    echo $ACTION_LIST | grep -qw "KEYMAP" || return $1
    $vdir/vkmapset
}

#---------------------------------------------
menu_restore()
{
    ## set hwclock first
    set_hwclock
    echo $ACTION_LIST | grep -qw "RESTORE" || return $1
    $vdir/vbackupsys --install
}


#---------------------------------------------
menu_autosetup()
{
    echo $ACTION_LIST | grep -qw "AUTOSETUP" || return $1
    if [ $1 = 0 ]; then
	$vdir/vhwconf
	## Update kernel modules
	depmod -A > /dev/null 2>&1
    else
	return $1
    fi
    return 0
}

#---------------------------------------------
menu_net()
{
    echo $ACTION_LIST | grep -qw "NETWORK" || return $1
    if modprobe eth0 &> /dev/null ; then 
	# Setup loopback
	# ifconfig lo 127.0.0.1 netmask 255.255.255.0 up
	# setup networking
	$vdir/vnetconf --no-start
	return $?
    else
	if [ $1 = 0 ]; then
	    infobox "Network card is not present. Skipping"
	    sleep 2
	fi
	return $1
    fi
    return 0
}

#---------------------------------------------
menu_x()
{

    echo $ACTION_LIST | grep -qw "XWINDOW" || return $1

    if [ -x /usr/X11R6/bin/XFree86 ]; then
	$vdir/vmkfx86cfg || return $?
    elif [ -x /usr/X11R6/bin/Xorg ]; then
	$vdir/vxconf || return $?
    else
	if [ $1 = 0 ]; then
	    infobox "X-Windows is not present. Skipping"
	    sleep 2
	fi
	return $1
    fi
    set_bootmode 4
    $vdir/vbootset
    if [ "$RUN_MODE" = "vector-virgin-heavy" ]; then
	telinit Q
    fi
    return 0
}

#---------------------------------------------
menu_alsa()
{
    echo $ACTION_LIST | grep -qw "SOUND" || return $1
    chmod +x /etc/rc.d/rc.alsa
    $vdir/valsaconf
}

#---------------------------------------------
menu_hw()
{
    echo $ACTION_LIST | grep -qw "HWINIT" || return $1
    $vdir/vhwset
}

#---------------------------------------------
menu_zone()
{
    echo $ACTION_LIST | grep -qw "ZONE" || return $1
    $vdir/vzoneset
}

#---------------------------------------------
menu_admin() {

echo $ACTION_LIST | grep -qw "ADMIN" || return $1

DEF_ITEM="ROOT"
while [ 0 ]; do
TITLE="USER ADMINISTRATION"
TEXT="\n
Linux is a secure system, thus every user must have an account
for logging into the system. The first and the most powerfull
account is called a 'root'. You should login as root on special
occasions only such as administering the system. For normal
every day work, it is safer to login as an ordinary user.\n\n
The following menu allows you to change the root password and 
to create some ordinary users."

$WCMD --backtitle "$BACKTITLE" --title "$TITLE" \
--default-item $DEF_ITEM --menu "$TEXT" 18 68 3 \
"ROOT"  "change root password" \
"USER"  "add a new user" \
"DONE"  "finish administering the users" \
2>$freply || return $?

case `cat $freply` in
    ROOT)
    $vdir/vpasswd root
    DEF_ITEM="USER"
    ;;
    USER)
    $vdir/vuseradd
    DEF_ITEM="DONE"
    ;;
    DONE)
    return 0
    ;;
esac

done
}

#---------------------------------------------
menu_finish1_short()
{

TEXT="\n
Vector Linux has been installed succesfully.
However, the current circumstance does not allow
full configuration. We will continue after rebooting."

$DCMD --backtitle "$BACKTITLE" --title "REBOOT IS NEEDED" \
--msgbox "$TEXT" 0 0 2> /dev/null

clean_exit 0
}

#---------------------------------------------
menu_finish1()
{
    infobox "Vector Linux has been installed and configured" "SUCCESS"
    set_stage2_light
    sleep 3
    clean_exit 0
}

#---------------------------------------------
menu_finish2()
{

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

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

clear
echoc "Vector Linux will be ready on the road in a moment ..." green 
echo "Backing up system files .."
touch /etc/fstab
touch /etc/lilo.conf 

# Setting up services
service -b &> /dev/null

# Backup 
backupsys &> /dev/null

sleep 2

clean_exit 0
}

###################################################################
# Wizard utilities

confirm_exit()
{
TEXT="\n
Configuration has not finished yet.\n
Are you going to quit now ?"
    $DCMD --title "CANCEL CONFIGURATION" --yesno "$TEXT" 7 40 2> /dev/null
    if [ $? = 0 ]; then
        clean_exit 1
    fi
}

wizard1()
{
   if [ -z "$1" ]; then
      # reaching here mean the wizard is success
      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
}

#############################################################
# Here we go the main configuration

menu_config()
{

FINISH=$1

TITLE="VECTOR LINUX CONFIGURATION"
TEXT="\n
So far so good. Now we are going configure this puppy.\n
The steps are quick and easy, because we will use autodetect.\n
However, you may skip some steps if you like.\n
Please use [space] bar to toggle the steps then hit <OK>:"

if ls /home/backupsys/*.tar.bz2 &>/dev/null; then

DIMENSION="20 74 9"

$DCMD --backtitle "$BACKTITLE" --title "$TITLE" \
--checklist "$TEXT" $DIMENSION \
"KEYMAP" "set keyboard map (default="$KEYMAP")" $KMAPSET \
"RESTORE" "restore backupsys found on /home (optional)" off \
"AUTOSETUP" "detect and set basic hardware (essential)" on \
"NETWORK" "set networking (recomended)" on \
"XWINDOW" "set X-Window GUI system (recomended)" on \
"SOUND" "set sound system (recomended)" on \
"HWINIT" "trimmed down hardware initialisation (optional)" on \
"ZONE" "set time zone (recomended)" on \
"ADMIN" "set root password and add users (recomended)" on \
2>$freply || return $?

ACTION_LIST=`cat $freply`

wizard1 menu_keymap menu_restore menu_autosetup menu_net menu_x \
   menu_alsa menu_hw menu_zone menu_admin $FINISH

else
DIMENSION="19 74 8"
$DCMD --backtitle "$BACKTITLE" --title "$TITLE" \
--checklist "$TEXT" $DIMENSION \
"KEYMAP" "set keyboard map (default="$KEYMAP")" $KMAPSET \
"AUTOSETUP" "set basic hardware (essential)" on \
"NETWORK" "set networking (optional)" on \
"XWINDOW" "set X-Window GUI system (optional)" on \
"SOUND" "set sound system (optional)" on \
"HWINIT" "trimmed down hardware initialisation (optional)" on \
"ZONE" "set time zone (recomended)" on \
"ADMIN" "set root password and add users (recomended)" on \
2>$freply || return $?

ACTION_LIST=`cat $freply`

wizard1 menu_keymap menu_autosetup menu_net menu_x \
   menu_alsa menu_hw menu_zone menu_admin $FINISH
fi

}

#############################################################
# Here we go the main procedures for STAGE 1

stage1_init()
{
# Do someting to impress
infobox "Checking and initializing ..." "Wait a moment please"

# check if we are running in a correct chroot directory
CHECK_FILES="/boot/vmlinuz /etc/vector-version"

for cfile in $TEST_FILES; do
  if [ ! -f $cfile ]; then
     errorbox "Cannot find $cfile. This is not me. I'm outa here !"
     clean_exit 1  
  fi
done

# Setup stage2-heavy as the default
set_stage2_heavy

# set time
set_hwclock
# Load libraries
ldconfig

# make standard directories
mkdir -p /mnt/cdrom /mnt/win /mnt/linux /mnt/pendrive /mnt/floppy

# initial settings
set_keymap
set_bootmode 2
service -b &> /dev/null

}

menu_lilo()
{
    while [ 0 ]; do 
	$vdir/vliloconf && return 0
	confirm_exit
    done
}

##-----------------------------------
stage1_short()
{
    stage1_init
    menu_lilo
    menu_finish1_short
    sleep 2
}

##------------------------------------
stage1()
{
    # if we have don't have the correct kernel modules go short
    if [ ! -d /lib/modules/$KERNEL_VERSION ]; then
	stage1_short
	return $?
    fi

    stage1_init
    menu_lilo
    while [ 0 ]; do
	menu_config menu_finish1
	confirm_exit
    done
}

#############################################################
# STAGE 2

stage2_init()
{
# Do someting to impress
infobox "Continuing first time configuration ..." "Almost there"

# Init system
set_hwclock
ldconfig
set_keymap
sleep 2
}

##------------------------------------
stage2_light()
{
   stage2_init
   menu_finish2
}

##------------------------------------
stage2_heavy()
{
  stage2_init
  while [ 0 ]; do
    menu_config menu_finish2
    confirm_exit
  done
}

########################################################
## Just a congratulations
keep_going()
{
if [ -f /var/log/system.record ]; then
  . /var/log/system.record
TEXT="\n
Vector Linux has been operating since\n
$INSTALL_DATE\n
Enjoy and keep happy Vectoring ..."
else
TEXT="\n
We wish you a very good time with Vector Linux.\n
Keep going ..."
fi

TITLE="FRIENDLY MESSAGE"

$DCMD --backtitle "$BACKTITLE" --title "$TITLE" \
--msgbox "$TEXT" 9 44 2> /dev/null
}

###################################################################
# Here we go the main menu
case $RUN_MODE in
    vector-virgin)
	stage1
	;;
    vector-virgin-short)
	stage1_short
	;;
    vector-virgin-heavy)
	stage2_heavy
	;;    
    vector-virgin-light)
	stage2_light
	;;
    *)
	# somebody is curious ... give him a cheer
	keep_going
	;;
esac
clean_exit 0
