#!/bin/sh
# Directly install a VL ISO file from a linus host.
# A wicked way to install a downloaded ISO file
# without burning a CDROM or buying a CDROM writer.
# Thanks to the VL community (www.vlocitylinux.com/forum)
# for the idea
# 
# (c) Eko M. Budi for VLocity Linux
# Released under GNU GPL

echo "Install a VLocity Linux ISO-file from a Linux host"

usage() {
cat << EOF
Usage: vinstall-iso-ng /<vl-VERSION.iso>

To run this program, please:
- get the proper ISO-file from VLocity Linux download site (or mirror)
- put the ISO-file on the hardisk under / or any other root directory
- for example  /mnt/sda2/vl-version.iso, then run this script.

EOF
exit 0
}

[ -z $1 ] && usage
[ ! -f $1 ] && usage
ISOFILE=$1
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() {
    ## Mount the ISO file
    echo "Mounting $ISOFILE ..."
    mkdir -p /mnt/loop
    umount /mnt/loop &>/dev/null
    mount $ISOFILE /mnt/loop -v -o loop || error_exit "Cannot mount ISO file"

    ## Copy the initrd.img
    echo "Preparing initrd ..."
    rm -rf $INSTALLRD 
    mkdir -p $INSTALLRD
    cd $INSTALLRD
    xz -dc /mnt/loop/isolinux/init.lz | cpio -id

    ## Umount the ISOFILE
    umount loop || \
	echo "cannot umount $ISOFILE"
    sleep 2
}


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
  
