#!/bin/sh
# Vector Linux SETUP
# Usage:
# setup          : normal setup
# setup --hosted : called by vlinstall from another Linux host
#
# Author: Eko M. Budi for Vector Linux
# License : GNU GPL

if [ "$1" = "--hosted" ]; then
    export HOSTED="1"
    echo "Vector Linux Setup HOSTED"
    echo "Excluded partitions:"
    echo "$EXCLUDE_PARTITIONS"
    #sleep 3
fi

#mke2fs -m 0 /dev/ram1
#mount /dev/ram1 /tmp

vdir=$(dirname $0)
. $vdir/setup-functions

#------------------------
go_exit()
{
if [ -z "$HOSTED" ]; then

cat << EOF

Welcome to the Linux shell.

Some programs that might be useful
mc            : file manager
cfdisk        : friendly partition utility
fdisk         : partition utility (use this for a brand new HD)
parted        : command line partition utility and re-sizer
probedisk     : list hard disk(s) and cdrom(s)
probepart     : list partition(s)
dmesg         : dump kernel boot message
uname -a      : print machine information
reboot        : reboot the computer (or press Ctrl-Alt-Del)
busybox --help: seeking help :)

If you want the friendly setup back, call $0

Good luck.
EOF
fi

clean_exit
}

#-------------
error_exit() {
clear
echo -e "${LTRED}Something has gone wrong. DON'T PANIC !!! ${OFF}"
if [ "$HOSTED" ]; then
cat <<EOF
I will send you to the shell prompt.
You may try to nail it, probably /tmp/setup.log will give a clue.
EOF
else
cat <<EOF
Lets go back to the host
EOF
fi
go_exit
}

ok_exit() {
clear
echo -e "${YELLOW}OK you wanna quit already so be it.....${OFF}"
go_exit
}

# $1 = base dir
# $2 = physical position
check_source()
{
    if [ -r $1/veclinux/SETUP.CONF ]; then
	. $1/veclinux/SETUP.CONF
	$DCMD --backtitle "$BACKTITLE" --title "MEDIA" --yesno "\n
Found $DISTRO $VERSION\n
on $2.\n
Is it the right one ?"  0 0 && return 0
	infobox "Trying again ..."
    fi
    return 1
}

# Mount a device and look if there is a /veclinux on it
# $1 = the device
# $2 = type
search_media() {
    MEDIA_TYPE=$1
    MEDIA_DEV=$2
    TYPE=${3:+"-t $3"}

    # umount for sure
    umount $MEDIA_MNT 2>/dev/null
    dbug "mount $TYPE $MEDIA_DEV $MEDIA_MNT"
    if (mount $TYPE $MEDIA_DEV $MEDIA_MNT) >/dev/null 2>&1; then
	if check_source $MEDIA_MNT "$MEDIA_TYPE ($MEDIA_DEV)"; then
	    umount $MEDIA_MNT >/dev/null 2>&1
	    (mount $TYPE $MEDIA_DEV $SOURCE_MNT) >/dev/null 2>&1
	    return $?
	fi
	## Try ISO then
	for ISO in $MEDIA_MNT/vl*.iso; do
	    [ ! -f $ISO ] && continue
	    if (mount -r -o loop $ISO $SOURCE_MNT) >/dev/null 2>&1; then
		if check_source $SOURCE_MNT "ISO File ($MEDIA_DEV:`basename $ISO`)"; then
		    return 0
		fi
	        umount $SOURCE_MNT >/dev/null 2>&1
	    fi
	done
	for ISO in $MEDIA_MNT/VL*.iso; do
	    [ ! -f $ISO ] && continue
	    if (mount -r -o loop $ISO $SOURCE_MNT) >/dev/null 2>&1; then
		if check_source $SOURCE_MNT "ISO File ($MEDIA_DEV:`basename $ISO`)"; then
		    return 0
		fi
	        umount $SOURCE_MNT >/dev/null 2>&1
	    fi
	done
	umount $MEDIA_MNT > /dev/null 2>&1
    fi
    return 1
}

#########################################################################################
# SHOW information then FIND Install Media
# In the end of this process,
# Vector Linux media will be mounted on $SOURCE_MNT
# or FAILED then EXIT

find_media() {

    echo "hosted install"
    
    # Check if SOURCE has been mounted
    if [ -r $SETUP_CONF ]; then
       infobox "Found mounted media ..."
       sleep 2
       return 0
    fi

    # Not found, do hard disk search
    TITLE="VECTOR LINUX INSTALL"
    infobox "Finding Vector Linux install media ..."
    
    ## Find the media on the HD first
    ## So user's can easily overrides the CD
    probepart | grep " FAT" | cut -f 1 -d ' ' > /tmp/dev
    if [ $? = 0 ]; then
	for dev in `cat /tmp/dev`; do
	    search_media "Windows HD" "$dev" && return 0
	done
    fi

    probepart | grep -e " *Linux$" | cut -f 1 -d ' ' > /tmp/dev
    if [ $? = 0 ]; then
	for dev in `cat /tmp/dev`; do
	    search_media "Linux HD" "$dev" && return 0
	done
    fi

    ## try to find it on the CD
 drives=$(cat /proc/sys/dev/cdrom/info |grep  "drive name"|cut -f2 -d':'|xargs -n 2 2>/dev/null);
	for dev in $drives; do
	    search_media "CDROM" "/dev/$dev" iso9660 && return 0
	done
## if that doesnt work try probedisk
    probedisk | grep '|cdrom|' | cut -f 1 -d '|' > /tmp/dev
    if [ $? = 0 ]; then
	for dev in `cat /tmp/dev`; do
	    search_media "CDROM" "$dev" iso9660 && return 0
	done
    fi
    
    ## blind guess SCSI, maybe SATA
    for i in a b c d e f g h; do
	if ! probedisk | grep -q "/dev/sd$i"; then 
	    search_media "SCSI" /dev/sd$i iso9660 && return 0
	fi
    done
    ## more blind guess SCSI, maybe SATA
    for i in 0 1 2 3 4 5 6 7 8 9; do
      for j in sr sg; do
	if ! probedisk | grep -q "/dev/$j$i"; then 
	    search_media "SCSI" /dev/$j$i iso9660 && return 0
	fi
      done
    done

TEXT="\n
ERROR ERROR ! I could not find the install media on the hard disk
or the CDROM as well. Please check that you have either:\n
  - The Vector Linux CDROM in a CD drive.\n
  - A hard disk partition with Vector Linux ISO file.\n 
  - A hard disk partition with /veclinux directory.\n
If you really have that but I cannot get it, maybe you are using
an advanced technology my author could not afford.\n
Thank you, but we must abort at this time."

    $DCMD --backtitle "$BACKTITLE" --title "SOMETHING IS WRONG" \
    --msgbox "$TEXT" 17 68
    return 1
}

###############################################################
# Finding the media
# Possibly extending it to network install
menu_media()
{

while [ 0 ]; do
TITLE="VECTOR LINUX SETUP"
DIMENSION="20 72 4"
TEXT="\n
Welcome to the Vector Linux setup. This will be quick and easy.\n
Mostly, you only need the following keys during the process:\n
  [Enter]     : hit the <OK> or <Cancel> button\n 
  [Tab]       : select the buttons\n
  [Up]/[Down] : select the menu/list\n
  [Space]     : toggle the selection on the checklist\n
  [Esc]       : go back to the previous dialog\n
So first, change the keymap if you have a non-US keyboard,\n
then we can start:"

$DCMD --backtitle "$BACKTITLE" --title "$TITLE" --no-cancel \
--menu "$TEXT" $DIMENSION \
"KEYMAP" "change keymap to non US-layout" \
"START"  "find the media (HD/CDROM) and start the installation" \
"LILO"   "repair Linux loader (Vector must have been installed)" \
"EXIT"   "bailout from this setup to the Linux shell" \
2> $freply

status=$?
if [ $status != 0 ]; then
    umount /mnt/source >/dev/null 2>&1
    umount /mnt/media  >/dev/null 2>&1
    return $status
fi

reply=$(cat $freply)
dbug "reply=$reply"

case $reply in
  START)
     find_media && return 0
     ;;
  KEYMAP)
     $vdir/setup-keymap
     ;;
  LILO)
     $vdir/setup-lilo
     ;;
  REBOOT)
     reboot_exit;
     ;;
  EXIT)
     ok_exit
     ;;
esac

done
}

menu_readme() 
{
TITLE="VECTOR LINUX README"
if [ -f $README ]; then
    DIMENSION="22 76"
    $DCMD --title "$TITLE" --textbox $README $DIMENSION 
else
TEXT="\n
Congratulation for having this Vector Linux CD,
and thank you for choosing it.

More information can be found in the /doc directory.
For comments, suggestions, questions or anything,
please visit http://www.vectorlinux.com/forum.

Enjoy & happy Vectoring ...

------------------------
Robert & the Vectelopers"
DIMENSION="22 76"
$DCMD --title "$TITLE" --msgbox $TEXT $DIMENSION 

fi

}

menu_setup()
{
# Get settings if available
if [ -f $SETUP_CONF ]; then
   . $SETUP_CONF
fi
calculate_swap

while [ 0 ]; do

TITLE="VECTOR LINUX SETUP"
DIMENSION="20 76 5"
TEXT="\n
We are going to install $DISTRO $VERSION on your computer.
This distro requires the following hard disk partitions:\n
 - A swap for virtual memory and tmpfs : $HD_SWAP MB.\n
 - A root for system and programs      : $HD_ROOT_MIN-$HD_ROOT_MAX MB.\n
It is also recommended to create a separate home partition:\n
 - A home for your data                : $HD_HOME MB or more.\n
If you have prepared these partitions already, let's go straight
to the INSTALL menu. Otherwise choose RESIZE or FDISK menu."

$DCMD --backtitle "$BACKTITLE" --title "$TITLE" --no-cancel \
--menu "$TEXT" $DIMENSION \
"README" "Read the important notes for this release" \
"RESIZE" "claim some space from an ext2 or fat partition for Linux" \
"FDISK"  "launch cfdisk utility to make Linux partitions" \
"INSTALL" "bring it on ..., I can't wait" \
"EXIT"   "give me the Linux shell, trust me !" \
2> $freply

status=$?
[ ! $status = 0 ] && return $status

reply=$(cat $freply)

dbug "reply=$reply"

case $reply in
  README)
     menu_readme
     ;;    
  RESIZE)
     $vdir/setup-resize $HD_SWAP $HD_ROOT_MIN $HD_ROOT_MAX $HD_HOME
     status=$?
     ;;
  FDISK)
     $vdir/setup-fdisk $HD_SWAP $HD_ROOT_MIN $HD_ROOT_MAX $HD_HOME
     status=$?
     ;;
  INSTALL)
     setup-install
     status=$?
     ## Make sure everything is back to normal
     umount $HOME_MNT > /dev/null 2>&1
     umount $ROOT_MNT > /dev/null 2>&1
     ;;
  BACK)
     return 255
     ;;
  EXIT)
     ok_exit
     ;;
  REBOOT)
     reboot_exit
     ;;
esac
[ $status = 1 ] && error_exit
[ $status = 255 ] && clean_exit 0
done
}

###################################################################################
# MAIN 

## If we don't have a hard disk, FATAL error
if ! probedisk | grep -v 'cdrom' ; then
    $DCMD --backtitle "$BACKTITLE" --title "FATAL ERROR" --msgbox "\n
Excuse me, I cannot find any hard disk for installation.\n
Maybe you are using a new technology my author didn't have.\n
Sorry, but I'm outta here" 0 0
    error_exit  
fi

## make standard directories
mkdir -p $SOURCE_MNT
mkdir -p $MEDIA_MNT
mkdir -p $TARGET_MNT

if [ "$HOSTED" ]; then
    find_media && menu_setup
else
    while [ 0 ]; do
	wizard menu_media menu_setup
    done
fi
