#!/bin/sh
#
# (c) Eko M. Budi for VLocity Linux
# Released under GNU GPL

echo "Install VLocity Linux from a Linux host."

usage() {
cat << EOF
vinstall - boot a VLocity Linux install image

Usage: vinstall [initrd.img]

If not specified, initrd.img must exist in the current directory.
You may get the initrd.img from VLocity Linux FTP site or CDROM
under isolinux/ directory

Make sure you have the VLocity Linux media, either:
- a partition with /veclinux directory on it.
- a VLocity Linux install CDROM (but try vinstall-cd instead)
- a partition with vl*.iso file on it (but try vinstall-iso).

EOF
exit 0
}

INITRD=${1:-init.lz}
[ ! -f $INITRD ] && usage
INSTALLRD="/tmp/initrd.vinstall"

error_exit ()
{
    echo "ERROR: $1"
    echo "you will need to clean the /tmp dir after a reboot!"
    exit 1
}

check_user()
{
[ "$UID" = "0" ] && return 0

cat << EOF

ERROR: must run as root

Please login as root or call su first

EOF

exit 1   
}


check_console()
{
    [ -z "$DISPLAY" ] && return 0
cat << EOF

ERROR: It is a bad idea to install from a GUI.

Remember these steps (write it on a paper):
1. Quit from this GUI
2. Drop to a console (press Crtl-Alt-F1)
3. Login as root
4. Switch to runlevel 2 (init 2)
5. Go to my directory (cd $PWD)
6. Call me again ($0 $@)

EOF
exit 1

}

check_runlevel()
{
RUNLEVEL=`runlevel | cut -f2 -d ' '`
case "$RUNLEVEL" in
    1|2|3) return 0
    ;;
esac

cat << EOF

ERROR: The system is not running on runlevel 2, or 3

For stability reason, use vinstall on runlevel 2, or 3.
To switch into the appropriate runlevel, you may:
- call "init 2" after this, or
- reboot this Linux with "linux 2" parameter

EOF
exit 1

}

check_mount()
{
if mount | grep -v 'on / ' | grep -qe '^/dev'; then
 
echo
echo "WARNING: these partitions are mounted:"
mount | grep -e '^/dev'
cat <<EOF

They will be excluded from installation process.
If you intend to use any of them for /, /home, etc.,
please cancel then umount them using this command:
   umount <the_partition>

Press <enter> to continue or <Ctrl>-<Break> to cancel.
EOF
read pause
fi
}

get_initrd() {
    ## prepare loop dir

    ## Copy the initrd.img
    echo "Preparing initrd ..."
    rm -rf $INSTALLRD 
    mkdir -p $INSTALLRD
    cd $INSTALLRD
    xz -dc $INITRD | cpio -id || \
	error_exit "cannot decompress $INITRD"
}

do_install() {
    
    ## Check if this is the right initrd
    [ -f usr/sbin/setup ] || \
	error_exit "initrd contains no setup program"

    ## Mount the /proc inside
    mkdir -p proc
    mount -o bind /proc proc || \
	error_exit "cannot mount /proc filesystem"

    mkdir -p sys
    mount -o bind /sys sys || \
	error_exit "cannot mount /sys filesystem"

    mkdir -p dev
    mount -o bind /dev dev || \
	error_exit "cannot mount /dev directory"

    ## Make a fake mtab
    ROOT_DEV=`mount | grep -e 'on / ' | cut -f 1 -d ' '`
    echo "$ROOT_DEV /     auto defaults 0 1  > etc/mtab
    echo "none      /proc proc defaults 0 1  >> etc/mtab

    ## prepare the excluded partitions
    EXCLUDE_PARTITIONS=""
    for PART in `mount | grep -e '^/dev' | cut -f1 -d ' '`; do
	EXCLUDE_PARTITIONS="$EXCLUDE_PARTITIONS $PART"
    done
    export EXCLUDE_PARTITIONS
    
    ## HERE WE GO ....
    chroot . /usr/sbin/setup --hosted

    ## Cleanup
    sleep 1
    umount dev 2> /dev/null||error_exit "cannot unmount dev directory"
    sleep 1
    umount sys 2> /dev/null||error_exit "cannot unmount sys directory"
    sleep 1
    umount proc 2> /dev/null||error_exit "cannot unmount proc directory"
    umount -a
    sleep 3
    cd 2> /dev/null
    sleep 1
    #rm -rf $INSTALLRD

    ## Welcomeback
    if [ $? = 255 ]; then
	clear
	echo "Installation has finished ..."
	echo "You may reboot the computer to start using VLocity Linux"
    else 
	clear
	echo "Hmmm, did the installer make it ?"
	echo "If yes, you may reboot the computer"
    fi
    exit 0
}

################################################
# Main program
check_user
check_console
check_runlevel
check_mount
get_initrd
do_install
