#!/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 - boot a Vector Linux install image

Usage: vinstall [vinstall.img]

If not specified, vinstall.img must exist in the current directory.

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

EOF
exit 0
}

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

INITRD=${1:-vinstall.img}
[ ! -f $INITRD ] && 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
    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>


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

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
do_install
