#!/bin/sh
#vmklive a script to remaster your system into a livecd
#
#
#
#
# Written by Tony Brijeski for VLocityLinux
#
#

vdir=/sbin
. $vdir/vasm-functions

check_root

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


menu_vmklive()
{

DIALOG="Xdialog" 
TITLE="VLocity-Linux-Remaster"
ICON="/usr/share/icons/vl-ball.xpm"
VMKLIVEDIR="/root/.vmklive"

## 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 livecd/dvd\nfrom your installed system\n 
    \n Would you like to continue?" 600x400
    if [ $? != 1 ]; then
     echo "Starting the process"
    else  
     return 0
    fi

## Ask for the livecd name and isofile name for the build

$DIALOG --icon $ICON --title $TITLE --inputbox "Enter the name of the Live CD/DVD. \nThere must be no blank spaces or strange characters. \nThe default name is vlocity if you leave it blank" 600x400 2> $freply
LIVECDNAME="`cat $freply`"

if [ "$LIVECDNAME" = "" ]; then
LIVECDNAME="vlocity"
fi
 
$DIALOG --icon $ICON --title $TITLE --inputbox "Enter the name of the iso file you want it to make. \nDo not add the .iso at the end as it will automatically be added \nThe default name is customvllive if you leave it blank" 600x400 2> $freply
ISONAME="`cat $freply`"

if [ "$ISONAME" = "" ]; then
ISONAME="customvllive"
fi  


sleep 2

(cat $VMKLIVEDIR/.config | while read LINE ;do
TEST1="`echo $LINE | grep LIVECDNAME`"
TEST2="`echo $LINE | grep ISONAME`"
if [ "$TEST1" != "" ]; then
FILE="LIVECDNAME=\"$LIVECDNAME\""
elif [ "$TEST2" != "" ]; then
FILE="ISONAME=\"$ISONAME\""
else
FILE="$LINE"
fi

echo $FILE >> $VMKLIVEDIR/.config.$$
done

mv $VMKLIVEDIR/.config.$$ $VMKLIVEDIR/.config
rm -f $VMKLIVEDIR/.config.$$
sleep 3
echo "XXXX" ) | $DIALOG --icon $ICON --title $TITLE --no-buttons --infobox "Saving your options to the config file now" 600x400 0


sleep 3


(HOMEDIRSIZE=`du -s /home | awk '{print $1}'`
BINDIRSIZE=`du -s /bin | awk '{print $1}'`
DEVDIRSIZE=`du -s /dev | awk '{print $1}'`
ETCDIRSIZE=`du -s /etc | awk '{print $1}'`
LIBDIRSIZE=`du -s /lib | awk '{print $1}'`
OPTDIRSIZE=`du -s /opt | awk '{print $1}'`
ROOTDIRSIZE=`du -s /root | awk '{print $1}'`
SBINDIRSIZE=`du -s /sbin | awk '{print $1}'`
USRDIRSIZE=`du -s /usr | awk '{print $1}'`
VARDIRSIZE=`du -s /var | awk '{print $1}'`
TOTALDIRSIZE=`expr $HOMEDIRSIZE + $BINDIRSIZE + $DEVDIRSIZE + $ETCDIRSIZE + $LIBDIRSIZE + $OPTDIRSIZE + $ROOTDIRSIZE + $SBINDIRSIZE + $USRDIRSIZE + $VARDIRSIZE`
echo "size=\"$TOTALDIRSIZE\"" > $VMKLIVEDIR/installsize
sleep 3
echo "XXXX" ) | $DIALOG --icon $ICON --title $TITLE --no-buttons --infobox "Calculating the size of the install.\nPlease wait." 600x400 0

(ln -sf /etc/rc.d/rc.S.live /etc/rc.d/rc.S
ln -sf /etc/rc.d/rc.K.live /etc/rc.d/rc.K
ln -sf /etc/rc.d/rc.M.live /etc/rc.d/rc.M
ln -sf /etc/rc.d/rc.local.live /etc/rc.d/rc.local
mv /etc/fstab $VMKLIVEDIR
mv /etc/mtab $VMKLIVEDIR
mv /etc/X11/xorg.conf $VMKLIVEDIR
ln -sf /etc/X11/xorg.conf-vesa /etc/X11/xorg.conf

cp $VMKLIVEDIR/vmklive /sbin
cp $VMKLIVEDIR/vliveinstall /sbin
echo $VMKLIVEDR
cp $VMKLIVEDIR/*.desktop /root/Desktop/
cp $VMKLIVEDIR/*.desktop /usr/share/applications/
cp $VMKLIVEDIR/*.desktop /etc/skel/Desktop/
ls /home | grep -v backupsys | grep -v ftp | grep -v tmp >$VMKLIVEDIR/hometmp.$$
cat $VMKLIVEDIR/hometmp.$$ | awk '{print " $VMKLIVEDIR/*.desktop /home/"$1 }' | xargs cp
rm $VMKLIVEDIR/hometmp.$$

sleep 2

xterm -e $VMKLIVEDIR/VL-build
sleep 3
echo "XXXX" ) | $DIALOG --icon $ICON --title $TITLE --no-buttons --infobox "Creating the LiveCD/DVD.\n\nThis can take a while so please be patient" 600x400 0


ln -sf /etc/rc.d/rc.S.real /etc/rc.d/rc.S
ln -sf /etc/rc.d/rc.K.real /etc/rc.d/rc.K
ln -sf /etc/rc.d/rc.M.real /etc/rc.d/rc.M
ln -sf /etc/rc.d/rc.local.real /etc/rc.d/rc.local
mv $VMKLIVEDIR/fstab /etc/
mv $VMKLIVEDIR/mtab /etc/
mv $VMKLIVEDIR/xorg.conf /etc/X11/


$DIALOG --icon $ICON --title $TITLE --yesno "You're $ISONAME.iso and $ISONAME.md5sum are ready.\n\n They can be found in the /tmp folder. \n Click on any button when you are ready to exit" 600x400


sleep 2

     return 0
}

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