#!/bin/sh
#vl-pen-installer a script to write iso/CDROM contents to pendrive 
#
#
# Written by Uelsk8s for VectorLinux

vdir=/sbin
. $vdir/vasm-functions

check_root

TMP=/var/log/setup/tmp
mkdir -p $TMP


menu_vmklive()
{

DIALOG="Xdialog" 
TITLE="VL-usb-install"
ICON="/usr/share/icons/vl-ball.xpm"

## let the user know what this app does and ask them to confirm in case they clicked on it by accident

$DIALOG --icon $ICON --title $TITLE --yesno "This application will create a Bootable USB Device\nfrom your install ISO or mounted cdrom\n 
    \n Would you like to continue?" 600x400
    if [ $? != 1 ]; then
     echo "Starting the process"
    else  
     return 0
    fi

## Ask for the iso/cd name and pendrive path

$DIALOG --icon $ICON --title $TITLE --menu "You must choose if you want to use an iso image or a mounted install directory.\nAfter you choose you will need to browse to make your selection." 600x400 3 "ISO"  "Use an ISO image" "DIR"  "Use files inside of a DIR" 2> $freply
if [ $? != 0 ]; then
   clean_exit $?
fi

SELECT="`cat $freply`"
if [ "$SELECT" = "ISO" ];then
  $DIALOG --fselect *.iso 600x400 2> $freply
  CDNAME="`cat $freply`"
else
  $DIALOG --dselect / 600x400 2> $freply
  CDNAME="`cat $freply`"
fi
if [ "$CDNAME" = "" ]; then
  errorbox "you must enter a iso name or mounted cdrom path"
  sleep 10
  exit 1
fi
#$DIALOG --icon $ICON --title $TITLE --msgbox "Now you need to select the mounted usb device \nwhere the install files will be  copied to. \nIn the next box you will need to browse, \nand select the mountpoint for the device." 600x400
#$DIALOG --dselect / 600x400 2> $freply
#TARGET="`cat $freply`"
$DIALOG --icon $ICON --title $TITLE --inputbox "Enter the device that you would like to use. \nThere must be no blank spaces or strange characters. \nYou must give the full path i.e. /dev/sdb1" 600x400 2> $freply
TARGET="`cat $freply`"

if [ "$TARGET" = "" ]; then
  errorbox "you must enter a path"
  sleep 10
  exit 1
fi
#(
if [ ! -d $CDNAME ];then
   mount -o loop $CDNAME /mnt/loop
   CDNAME=/mnt/loop
fi
## this is added because vl-hot mounts the devices as non-executable
#test=`echo $TARGET|cut -d / -f3`
#if [ "$test" = "vl-hot" ];then
#  target=`mount |grep $TARGET|cut -d " " -f1`
#  echo $TARGET $target
#  sleep 2
#  umount -f $TARGET
#  mount $target /mnt/hd
#  TARGET=/mnt/hd
#fi 
umount $TARGET
mount $TARGET /mnt/hd
if [ "$?" = 0 ];then
 TARGET=/mnt/hd
else
  errorbox "There was an error mounting the device"
  sleep 10
  exit 1
fi

cp -ax $CDNAME/* $TARGET
umount $CDNAME

sleep 1
#echo "XXXX" ) | $DIALOG --icon $ICON --title $TITLE --no-buttons --infobox "Copying files to usb device now" 600x400 0


sleep 3


#(
cd $TARGET
ISOLINUX=`find . -name isolinux`
if [ ! -d $TARGET/boot/syslinux ];then
  cp -ax $VMKLIVEDIR/cd-root/boot/syslinux/* $ISOLINUX
  SYSLINUX=$ISOLINUX
else
  SYSLINUX=$TARGET/boot/syslinux
fi
TARGET1=""
MBR=""

# Find out which partition or disk are we using
MYMNT=`pwd`
echo $MYMNT
while [ "$MYMNT" != "" -a "$MYMNT" != "." -a "$MYMNT" != "/" ]; do
   TARGET1=$(egrep "[^[:space:]]+[[:space:]]+$MYMNT[[:space:]]+" /proc/mounts | cut -d " " -f 1)
   if [ "$TARGET1" != "" ]; then break; fi
   MYMNT=$(dirname "$MYMNT")
done
MBR=$(echo "$TARGET1" | sed -r "s/[0-9]+\$//g")
NUM=${TARGET:${#MBR}}
echo $MYMNT
echo $MBR
echo $TARGET1
echo $NUM
cp isolinux/isolinux.cfg isolinux/syslinux.cfg
rm -r syslinux
cp -ax isolinux/ syslinux/
cp isolinux/isolinux.cfg syslinux.cfg
sed -i s"|message.txt|isolinux/message.txt|"g syslinux.cfg
sed -i s"|kernel/sata|isolinux/kernel/sata|"g syslinux.cfg
sed -i s"|init.lz|isolinux/init.lz|"g syslinux.cfg
sed -i s"|initrd.lz|isolinux/initrd.lz|"g syslinux.cfg
# setup MBR if the device is not in superfloppy format
if [ "$MBR" != "$TARGET1" ]; then
   echo "Setting up MBR on $MBR..."
   $SYSLINUX/lilo -S /dev/null -M $MBR ext # this must be here to support -A for extended partitions
   echo "Activating partition $TARGET..."
   $SYSLINUX/lilo -S /dev/null -A $MBR $NUM
   echo "Updating MBR on $MBR..." # this must be here because LILO mbr is bad. mbr.bin is from syslinux
   cat $SYSLINUX/mbr.bin > $MBR
fi

echo "Setting up boot record for $TARGET..."
syslinux -d $SYSLINUX $TARGET1
sleep 4
#echo "XXXX" ) | $DIALOG --icon $ICON --title $TITLE --no-buttons --infobox "Configuring boot loader.\nPlease wait." 600x400 0

$DIALOG --icon $ICON --title $TITLE --msgbox "You're USB Device is now bootable\n\n  \n Click the OK button when you are ready to exit" 600x400

     return 0
}

############################
# MAIN MENU
#wizard menu_vmklive
menu_vmklive
