#!/bin/sh
#
# (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-5.0.iso>

EOF
exit 0
}

error()
{
    echo "ERROR: $1"
    exit 1
}

[ -z $1 ] && usage
[ ! -f $1 ] && usage

ISO=$1
INITRD="vinstall.img"

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

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

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 0

}



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 1, 2, or 3

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

EOF
exit 0

}

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

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

Just a reminder, the $ISO
must remain in the mounted partition !

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

copy_initrd() {
    ## Mount the ISO file
    mkdir -p loop
    mount -o loop $ISO loop || error "Cannot mount ISO file"

    ## Copy the initrd.img
    if ! cp loop/isolinux/initrd.img $INITRD.gz; then
	umount loop
	error "Cannot copy $INITRD"
	exit 1
    fi

    ## Got it, uncompress
    if ! gunzip -f $INITRD.gz; then
	umount loop
	error "cannot decompress $INITRD"
	exit 1
    fi

    if ! file $INITRD | grep -q "ext2 filesystem";  then
	umount loop
	error "invalid $INITRD"
    fi

    ## Umount the ISO
    umount loop || error "cannot umount $ISO"
}

do_install() {
    ## Mount the initrd if not yet
    if [ ! -f loop/sbin/setup ]; then
	mount -o loop $INITRD loop || error "cannot mount $INITRD"
	UMOUNT=1
    fi
            
    ## Mount the ISO inside
    mkdir -p loop/mnt/source
    if ! mount -o loop $ISO loop/mnt/source; then
	umount loop
	error "cannot mount $ISO as source"
    fi

    ## Mount the /proc inside
    mkdir -p loop/proc
    if ! mount -t proc none loop/proc; then
	umount loop/mnt/source
        umount loop
	error "cannot mount /proc filesystem"
    fi

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

    ## Here we go
    EXCLUDE_PARTITIONS=""
    for PART in `mount | grep -e '^/dev' | awk '{print $1}'`; do
	EXCLUDE_PARTITIONS="$EXCLUDE_PARTITIONS $PART"
    done
    export EXCLUDE_PARTITIONS
    chroot loop /sbin/setup --hosted

    if [ $? = 255 ]; then
	clear
	echo "Installation has finished ..."
	echo "You may reboot the computer to start using Vector Linux"
    else 
	clear
	echo "Hmmm, was the installation successfull ?"
	echo "If yes, you may reboot the computer"
    fi
    umount loop/mnt/source 2> /dev/null
    umount loop/proc 2> /dev/null
    sleep 1
    if [ "$UMOUNT" ]; then
	umount loop 2> /dev/null
    fi
}

################################################
# Main program
check_console
check_runlevel
check_mount
copy_initrd
do_install
  