#!/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.vectorlinux.com/forum)
# for the idea
# 
# (c) Eko M. Budi for Vector Linux
# Released under GNU GPL

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

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

To run this program, please:
- get the proper ISO-file from Vector Linux download site (or mirror)
- put the ISO-file on the hardisk under a read/write directory
- spare same space on that directory (around 10MB).
- cd to that directory, then run this script

EOF
exit 0
}

[ -z $1 ] && usage
[ ! -f $1 ] && usage
ISOFILE=$1
INSTALLRD="initrd.vinstall"

error_exit ()
{
    echo "ERROR: $1"
    umount loop/source 2>/dev/null && sleep 1
    umount loop 2>/dev/null && sleep 1
    #rm -f $INSTALLRD 2>/dev/null
    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 loop
    umount loop &>/dev/null
    mount $ISOFILE loop -v -o loop || error_exit "Cannot mount ISO file"

    ## Copy the initrd.img
    echo "Preparing initrd ..."
    gunzip -c -S img loop/isolinux/initrd.img > $INSTALLRD || \
	error_exit "cannot decompress initrd.img from ISO-file"

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


do_install() {
    ## Check validity
    file $INSTALLRD | grep -q "ext2 filesystem" || \
	error_exit "invalid initrd file"

    ## Mount the initrd 
    mount -o loop $INSTALLRD loop || \
	error_exit "cannot mount initrd file"

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

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

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

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

    ## Mount the ISOFILE inside
    mkdir -p loop/mnt/source
    mount -o loop $ISOFILE loop/mnt/source || \
	error_exit "cannot mount $ISOFILE as source"

    ## Make a fake mtab
    ROOT_DEV=`mount | grep -e 'on / ' | cut -f 1 -d ' '`
    echo "$ROOT_DEV /     auto defaults 0 1  > loop/etc/mtab
    echo "none      /proc proc defaults 0 1  > loop/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 loop /usr/sbin/setup --hosted

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

    ## Cleanup
    umount loop/mnt/source 2> /dev/null
    sleep 1
    umount loop/dev 2> /dev/null
    sleep 1
    umount loop/sys 2> /dev/null
    sleep 1
    umount loop/proc 2> /dev/null
    sleep 1
    umount loop 2> /dev/null
    sleep 1
    rm -f $INSTALLRD
    exit 0
}


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