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

echo "Install Vector Linux from a Linux host."

usage() {
cat << EOF
vinstall-cd - install Vector Linux CDROM from a Linux host

Usage: vinstall-cd [cd_mount_point] [cd_device]

cd_mount_point : where to mount the cd, default = /mnt/cdrom
cd_device      : the actual cd device, default is taken from /etc/fstab

EOF
exit 0
}

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

CDROM=${1:-/mnt/cdrom}
CDDEVICE=${2:""}
INITRD=initrd.img
[ ! -d $CDROM ] && usage


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
    N|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 2, or 3.
Please:
- switch run level (init 2)
- call me again ($0 $@)

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>

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

get_initrd()
{
    if [ ! -f $CDROM/isolinux/$INITRD ]; then
	mount $CDDEVICE $CDROM || error_exit "cannot mount CDROM"
    fi
    cp -a $CDROM/isolinux/$INITRD || error_exit "cannot copy initrd.img"
    umount $CDROM || error_exit "cannot umount CDROM. Please get out from it !"
    mkdir -p loop
}


do_install() {
    ## Mount the initrd if not yet
    if [ ! -f loop/sbin/setup ]; then
	mount -o loop $INITRD loop || error_exit "cannot mount $INITRD"
	UMOUNT=1
    fi

    ## Mount the /proc inside
    mkdir -p loop/proc
    if ! mount -t proc none loop/proc; then
	umount loop/mnt/source
        umount loop
	error_exit "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 install successfull ?"
	echo "If yes, you may reboot the computer"
    fi
    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
get_initrd
do_install
  