#!/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
ROOT_DEVICE=$2 
KEYMAP=$3
KERNEL_VERSION=`uname -r`
SETUP_DIR="/var/log/setup"
INIT_DIR="$SETUP_DIR/init"
TMP_DIR="$SETUP_DIR/tmp"

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

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

## 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()
{
   cat $finit \
   | sed "s/^id:[0-9]:initdefault:/id:$1:initdefault:/" \
   > $finit.new
   mv -f $finit.new $finit
}

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

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

#---------------------------------------------
# LILO is only called on stage1
menu_lilo()
{
    $vdir/vliloconf / $ROOT_DEVICE
}

#---------------------------------------------
menu_backup()
{
    $vdir/vbackupsys --install
}

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

#---------------------------------------------
menu_net()
{
    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
    else
	if [ $1 = 0 ]; then
	    infobox "Network card is not present. Skipping"
	    sleep 2
	fi
	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"
	    setbootmode 2
	    sleep 2
	fi
	return $1
    fi
}

#---------------------------------------------
menu_alsa()
{
    $vdir/valsaconf
}

#---------------------------------------------
menu_zone()
{
    $vdir/vzoneset
}

#---------------------------------------------
# this is for stage 1 only
menu_kmap1()
{
    [ -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 $KEYMAP > /etc/rc.d/rc.keymap
	chmod +x /etc/rc.d/rc.keymap
	sleep 2
	return $1
    fi

    $vdir/vkmapset
}

#---------------------------------------------
menu_user() {
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 "SO FAR SO GOOD" \
--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 is not finished yet.\n
Are you sure ?"
    $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 procedures for STAGE 1

menu_stage1()
{

while [ 0 ]; do

TITLE="VECTOR LINUX CONFIGURATION"

# Check if we have backupsys
if ls /home/backupsys/*.tar.bz2 &>/dev/null; then
DIMENSION="20 74 4"
TEXT="\n
Hmmm ... your system looks fine. So let's configure it.\n
The good news is, we found some system backup volumes. If you think
the backup belongs to this system, choose one of the BACKUP method.
Otherwise, use AUTOSETUP to automatically detect the hardware.
Select SKIP only if you are sure this system will be knocked down
by the autosetup. Your choice please:"

$DCMD --backtitle "$BACKTITLE" --title "$TITLE" --menu "$TEXT" $DIMENSION \
"AUTOSETUP" "detect hardware then configure" \
"BACKUP-AUTO" "restore the backup, then continue autosetup" \
"BACKUP"    "setup configuration from the backup only" \
"SKIP"      "the autosetup has failed me before" \
2>$freply || return $?

else
DIMENSION="14 60 2"
TEXT="\n
Hmmm ... this system looks fine. So let's configure it.
The easy, recomended way is using AUTOSETUP. However,
strange hardware could confuse the detection and hung
the system. So you may SKIP and do it after rebooting.\n
Your choice:"

$DCMD --backtitle "$BACKTITLE" --title "$TITLE" --menu "$TEXT" $DIMENSION \
"AUTOSETUP"  "do it. Make my puppy run !" \
"SKIP"  "not now, please." \
2>$freply || return $?
fi

case `cat $freply` in
    BACKUP)
	wizard1 menu_backup menu_user menu_finish1
	;;
    BACKUP-AUTO)
	wizard1 menu_backup menu_autosetup menu_net menu_x menu_alsa \
	        menu_kmap1 menu_zone menu_user menu_finish1
	;;
    BACKUP-MAN)
	wizard1 menu_backup menu_net menu_x menu_alsa \
	        menu_kmap1 menu_zone menu_user menu_finish1
	;;
    AUTOSETUP)
	wizard1 menu_autosetup menu_net menu_x menu_alsa \
	        menu_kmap1 menu_zone menu_user menu_finish1
	;;
    SKIP)
	infobox "Allright, by passing configuration ..."
	sleep 1
	clean_exit 0
	;;
esac

done

}

stage1_init()
{
# Do someting to impress
infobox "Checking and initializing ..." "VECTOR LINUX CONFIGURATION"

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

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

# Load libraries
ldconfig
}

##-----------------------------------
stage1_short()
{
stage1_init
while [ 0 ]; do
    wizard1 menu_lilo menu_finish1_short
    infobox "no way back, mate !"
    sleep 2
done
}

##------------------------------------
stage1()
{
stage1_init

while [ 0 ]; do
    # if we have the correct kernel modules in this virgin
    if [ -d /lib/modules/$KERNEL_VERSION ]; then
	wizard1 menu_lilo menu_stage1 menu_finish1
    else
	wizard1 menu_lilo menu_finish1_short
    fi
    infobox "no way back, mate !"
    sleep 2
done
}



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

#---------------------------------------------
menu_stage2()
{
    # Detect if we need a KEYMAP menu
    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
	    # KEYMAP is not needed anymore
	    KEYMAP=""
	fi
    fi

while [ 0 ]; do

TITLE="WELCOME TO VECTOR LINUX"

# Check if we have backupsys
if ls /home/backupsys/*.tar.bz2 &>/dev/null; then

TEXT="\n
Hello, Vector Linux is ready at your service, almost.\n
We still need a little bit configuration. The good news is, trace
of previous life is found on /home/backupsys. If you are sure that
the backup belongs to this widow, select one of the BACKUP method.
Otherwise let's start a new life with AUTOSETUP. Choosing SKIP is
not a good idea."

    if [ "$KEYMAP" ]; then
	$DCMD --backtitle "$BACKTITLE" --title "$TITLE" \
	--menu "$TEXT" 19 74 5 \
	"KEYMAP" "select keyboard mapping first" \
	"AUTOSETUP" "detect hardware then configure" \
	"BACKUP-AUTO" "restore the backup, then continue autosetup" \
	"BACKUP"    "setup configuration from the backup only" \
	"SKIP"      "let my widow alone." \
	2>$freply || return $?
    else
	$DCMD --backtitle "$BACKTITLE" --title "$TITLE" \
	--menu "$TEXT" 18 62 4 \
	"AUTOSETUP" "detect hardware then configure" \
	"BACKUP-AUTO" "restore the backup, then continue autosetup" \
	"BACKUP"    "setup configuration from the backup only" \
	"SKIP"      "let my widow alone." \
	2>$freply || return $?
    fi
else

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.\n
Let's choose AUTOSETUP and hit <OK> so we can begin:"

    if [ "$KEYMAP" ]; then
	$DCMD --backtitle "$BACKTITLE" --title "$TITLE" \
	--menu "$TEXT" 15 58 3 \
	"KEYMAP"  "change the keyboard map first" \
	"AUTOSETUP"  "do it. shag my computer !" \
	"SKIP"  "let my virgin alone." \
	2>$freply || return $?
    else
	$DCMD --backtitle "$BACKTITLE" --title "$TITLE" \
	--menu "$TEXT" 14 58 2 \
	"AUTOSETUP"  "do it. shag my computer !" \
	"SKIP"  "let my virgin alone." \
	2>$freply || return $?
    fi
fi

case `cat $freply` in
    KEYMAP)
	$vdir/vkmapset && KEYMAP=""
	;;
    BACKUP)
	wizard1 menu_backup menu_user menu_finish2
	;;
    BACKUP-AUTO)
	wizard1 menu_backup menu_autosetup menu_net menu_x \
	    menu_alsa menu_zone menu_user menu_finish2
	;;
    BACKUP-MAN)
	wizard1 menu_backup menu_net menu_x \
	    menu_alsa menu_zone menu_user menu_finish2
	;;
    AUTOSETUP)
	wizard1 menu_autosetup menu_net menu_x menu_alsa \
	    menu_zone menu_user menu_finish2
	;;
    SKIP)
	infobox "Skipping configuration at your command ..."
	sleep 2
	clear
	clean_exit 0
	;;
esac
done
}


##------------------------------------
stage2_heavy()
{
  service -b &> /dev/null
  while [ 0 ]; do
    wizard1 menu_stage2 menu_finish2
    infobox "no way back, mate !"
    sleep 2
  done
}

##------------------------------------
stage2_light()
{
   service -b &> /dev/null
   menu_finish2
}


########################################################
## 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 $1 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
