#!/bin/sh
#vliveinstall a script to install vlocitylive to the hard disk
#
#
#
# Written By Uel Archuletta for VLocityLinux
#
# Modified by Tony Brijeski for VLocityLinux
#

vdir=/sbin
. $vdir/vasm-functions

check_root

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

# VARIABLES
TP_X=${1:-/}
if [ "$TP_X" = "/" ]; then
    ROOT_DIR=""
else
    ROOT_DIR=$TP_X
fi

#find the livecd
livecd="`cat /proc/mounts |grep -v loop |grep "/mnt" |grep -v "/mnt/live" |grep -v "/mnt/_" |grep -v "/mnt/cdrom_" |cut -d " " -f 2 |grep -v "/proc" | egrep -v "^/dev|^/sys|^proc|^/\$"`"

menu_install()
{
DIALOG="Xdialog" 
TITLE="VLocity-Linux-Live-Installer"
ICON="/usr/share/icons/vl-ball.xpm"



$DIALOG --icon $ICON --title $TITLE --yesno "This application will install the livecd to your hard drive.\n \n Would you like to continue?" 600x400
    if [ $? != 1 ]; then
     echo "Starting the process"
    else
     return 0
    fi


## Step 1 - umount all mounted partitions

(mount | grep "/mnt" | awk '{print $3}' | xargs umount

#cleanup mtab as it sometimes gets corrupted by the above command

sed /mnt/d /etc/mtab >/etc/mtab.$$
mv /etc/mtab.$$ /etc/mtab
rm -f /etc/mtab.$$
sleep 5
echo "XXXX" ) | $DIALOG --icon $ICON --title $TITLE --no-buttons --infobox "Please wait...Starting partitioning tool" 600x400 0


## Step 2 - run gparted to setup partitions

gparted

## Step 3 - make sure all partitions have a proper /mnt/ entry and mount them

# mkdir for all partitions found
(cat /proc/partitions | grep -v loop | grep -v major | grep -v "^\$" | sed -r "s/^[0-9 ]+/\\/dev\\//" | grep "[0-9]" | awk -F "/" '{ print "/mnt/"$3}' | xargs mkdir

# mount all partitions found
cat /proc/partitions | grep -v loop | grep -v major | grep -v "^\$" | sed -r "s/^[0-9 ]+/\\/dev\\//" | grep "[0-9]" | awk -F "/" '{ print "/dev/"$3" /mnt/"$3}' | while read MOUNT; do mount $MOUNT; done
sleep 3
echo "XXXX" ) | $DIALOG --icon $ICON --title $TITLE --no-buttons --infobox "Please wait...Remounting partitions" 600x400 0
## Step 4 - running vuser to setup users and change root's password

vuser

## Step 5 - install the livecd to the harddrive

##check to see if more than 1 partition is available if only 1 use it
partitions="`mount |grep -v dos|grep -v fat|grep -v ntfs|egrep -o "/mnt/[a-z]+[0-9]+" | uniq`"
     j="0"
     for i in $partitions; do
       ## was trying to eliminate partitions without enought space I havent found a good way yet this doesnt always work
       #if [ "`df -h|grep $i|cut -d G -f3|cut -d . -f1`" -gt 2 ] 2>/dev/null; then
         part[$j]="$i"
         j=`expr $j + 1`
       #fi
     done
     $DIALOG --icon $ICON --title $TITLE --menubox "\n
     Which partition would you like to install the livecd to?\n
     This will be your root partition.\n
     If all you see is the placeholder it means your partitions arent \n
     mounted or dont contain a linux filesystem\n
     Choose which partition you would like to use" \
     600x400 4 \
     "placeholder" "Do not choose this" \
     ${part[0]} ${part[0]} \
     ${part[1]} ${part[1]} \
     ${part[2]} ${part[2]} \
     ${part[3]} ${part[3]} \
     ${part[4]} ${part[4]} \
     ${part[5]} ${part[5]} \
     ${part[6]} ${part[6]} \
     ${part[7]} ${part[7]} \
     ${part[8]} ${part[8]} \
     ${part[9]} ${part[9]} \
     2> $freply
     result=$?
     if [ $result != 0 ];then
       exit 0
     fi
     TARGET="`cat $freply`"
     if [ $TARGET == "placeholder" ];then
       (sleep 3; echo "XXXX") | $DIALOG --icon $ICON --title $TITLE --no-buttons --infobox "Sorry that wont work try again" 600x400 0
       exit 0
     fi
     dirs=`ls -1 / | egrep -v "^proc" | egrep -v "^mnt" | egrep -v "^boot"  | egrep -v "^sys"  | egrep -v "^tmp"`

    $DIALOG --icon $ICON --title $TITLE --yesno "Would you like to use a separate /home partition" 600x400
    if [ $? != 1 ]; then
      dir=`echo $dirs | egrep -v "^home"`
      partitions="`mount |grep -v dos|grep -v fat|grep -v ntfs|egrep -o "/mnt/[a-z]+[0-9]+" | uniq | grep -v $TARGET`"
      j="0"
      for i in $partitions; do
        home[$j]="$i"
        j=`expr $j + 1`
      done
      $DIALOG --icon $ICON --title $TITLE --menubox "\n
      Which partition would you like to use as your home Partition?\n
      If all you see is the placeholder then we cannot\n
      find a partition to use, maybe your partitions are not mounted?\n
      Choose which partition you would like to use" \
      600x400 4 \
      "placeholder" "Do not choose this" \
      ${home[0]} ${home[0]} \
      ${home[1]} ${home[1]} \
      ${home[2]} ${home[2]} \
      ${home[3]} ${home[3]} \
      ${home[4]} ${home[4]} \
      ${home[5]} ${home[5]} \
      ${home[6]} ${home[6]} \
      ${home[7]} ${home[7]} \
      ${home[8]} ${home[8]} \
      ${home[9]} ${home[9]} \
      2> $freply
      HOME="`cat $freply`"
     if [ $HOME == "placeholder" ];then
       (sleep 3; echo "XXXX" ) | $DIALOG --icon $ICON --title $TITLE --no-buttons --infobox "Sorry that wont work try again" 600x400 0
       exit 0
     fi
      if [ "$HOME" != "" ]; then
        cp --preserve=all -R /home/* $HOME/
	sephome="yes"
      else
       $DIALOG --icon $ICON --title $TITLE --msgbox "Sorry copying to /home failed. You will have to do it manually" 600x400
      fi
    else
      dir=`echo $dirs`
    fi

(sleep 3; echo "XXXX" ) | $DIALOG --icon $ICON --title $TITLE --no-buttons --infobox "Please wait...Installing" 600x400 0

     #x=1
     ##size should be the total installed size returned from df it is different between soho and standard
     # get the install size for the progress bar - this is created during vmklive
     . /root/.vmklive/installsize
     (for i in $dir; do
       cp --preserve=all -R /$i $TARGET/
       #x=`expr $x + 10`
      done) | /sbin/dfgaugeX $TARGET $size "LiveCD"
      #/sbin/dfgauge $TARGET $size $i
      #progress 100    "done"
      mkdir -p $TARGET/{tmp,boot,mnt,sys,proc}
      mkdir -p $TARGET/mnt/{cdrom,cdwriter,dvd,dvdwriter,floppy,hd,linux,loop,memory,pendrive,tmp,vl-hot,win,zip}

(sleep 3; echo "XXXX" ) | $DIALOG --icon $ICON --title $TITLE --no-buttons --infobox "Done copying on to initializing"

   dev=`mount |egrep $TARGET | cut -d " " -f 1|cut -c 6-9`
   DEV=`ls -A1d /sys/block/*/dev /sys/block/*/*/dev 2>/dev/null | grep $dev`
   NOD=`head -n 1 $DEV | tr ':' ' '`
   cd $TARGET/
   mknod dev/$dev b $NOD

    
   device=`mount |egrep $TARGET | cut -d " " -f 1`
   
   rootfs=`mount | grep $device | cut -d " " -f5`   
   echo "$device / $rootfs	defaults	1 1" > $TARGET/etc/fstab
   if [ $sephome == "yes" ]; then
     homefs=`mount | grep $HOME | cut -d " " -f5`
     echo "$HOME /home $homefs	defaults	1 2" >> $TARGET/etc/fstab
   fi
   swap=`fdisk -l|grep swap|cut -d " " -f1`
   if [ $swap ];then
    echo "$swap       none   swap   sw   0  0" >>$TARGET/etc/fstab
   fi
   echo "
   devpts           /dev/pts         devpts       gid=5,mode=620   0   0
   proc             /proc            proc         defaults         0   0
   /dev/fd0         /mnt/floppy      vfat,msdos  noauto,users,suid,dev,exec 0 0" >> $TARGET/etc/fstab
   
   rm $TARGET/etc/mtab
   cp /boot/vmlinuz $TARGET/boot
   cp /boot/initrd.real $TARGET/boot/initrd
   cp --preserve -R /dev/null $TARGET/dev
   cp --preserve -R /dev/console $TARGET/dev
   cp /etc/lilo.conf $TARGET/etc/lilo.conf

   cd $TARGET/etc/rc.d/ && rm rc.S && ln -s rc.S.real rc.S
   cd $TARGET/etc/rc.d/ && rm rc.M && ln -s rc.M.real rc.M
   #cd $TARGET/etc/rc.d/ && rm rc.local && ln -s rc.local.real rc.local
   #cd $TARGET/etc/rc.d/ && rm rc.X && ln -s rc.X.real rc.X
   cd $TARGET/etc/rc.d/ && rm rc.K && ln -s rc.K.real rc.K

   ##these are not needed for standard only for SOHO
   #cd $TARGET/opt/kde/share/config/kdm && rm kdmrc && mv kdmrc.bak kdmrc
   cd $TARGET/opt/kde/share/apps/kdm/themes/GlassLight 
   if [ -e GlassLight.xml.bak ];then
     rm GlassLight.xml && mv GlassLight.xml.bak GlassLight.xml
   fi

   HardDisk=`fdisk -l | grep Disk | cut -d " " -f 2 | cut -c 1-8`
   #x=`echo $HardDisk | wc -w`
   #if [ $x != 1 ]; then  
   root=$(cat /proc/mounts |grep ^/dev |grep $TARGET |cut -d " " -f 1)
     j="0"
     for i in $HardDisk; do
       hd[$j]="$i"
       j=`expr $j + 1`
     done
     $DIALOG --icon $ICON --title $TITLE --menubox "\n
     LILO (Linux Loader) install lilo \n
     we need to know where you would like lilo to be written \n
     if you choose exit you will have to setup your boot manager manually \n
     Choose where you would like to install lilo" \
     600x400 4 \
     ${hd[0]} ${hd[0]} \
     ${hd[1]} ${hd[1]} \
     ${hd[2]} ${hd[2]} \
     ${hd[3]} ${hd[3]} \
     ${part[0]} ${part[0]} \
     ${part[1]} ${part[1]} \
     ${part[2]} ${part[2]} \
     ${part[3]} ${part[3]} \
     ${part[4]} ${part[4]} \
     ${part[5]} ${part[5]} \
     ${part[6]} ${part[6]} \
     ${part[7]} ${part[7]} \
     ${part[8]} ${part[8]} \
     ${part[9]} ${part[9]} \
     "/dev/fd0" "Floppy" \
     2> $freply

     result="`cat $freply`"
     if [ $result == "Exit" ]; then
        (sleep 3; echo "XXXX" ) | $DIALOG --icon $ICON --title $TITLE --no-buttons --infobox "Remember to setup your boot manager" 600x400 0
     else
       LILO_TARGET=$result
     #else
       #LILO_TARGET="$HardDisk"
     #fi
     ##this only did the mbr of the disk where the target resides
     ##not good if I want to install to hdb2 and have lilo on hda
     #dev=$(cat /proc/mounts |grep ^/dev |grep $TARGET |cut -c 1-8)
        cp -r $TARGET/root/.grub   $TARGET/boot/grub  

      if [ -e $TARGET/root/.bitmap ];then
        cp -r $TARGET/root/.bitmap $TARGET/boot/bitmap
        BIT="bitmap=/boot/bitmap/boot.bmp"
      fi

       echo "
       boot = $LILO_TARGET
       prompt
       timeout = 20
       $BIT
       change-rules
       reset
       vga = 788   
       image = /boot/vmlinuz
       initrd= /boot/initrd
       root = $root
       label = VLocity
       append=\"4 splash=silent\"
       read-only       
       " >$TARGET/etc/lilo.conf
       infobox "setting up lilo"
       mount -o bind /dev $TARGET/dev
       mount -t proc proc $TARGET/proc
       #mount --move /proc $TARGET/proc
       /usr/bin/ms-sys --mbrzero $LILO_TARGET 2>/dev/null
       sync
       chroot $TARGET lilo   
       #exit
       umount $TARGET/dev
       umount $TARGET/proc
       #mount -t proc proc /proc
     fi

   SETUP_DIR="$TARGET/var/log/setup"
   INIT_DIR="$SETUP_DIR/init"
   mkdir -p $INIT_DIR
   chmod 700 $INIT_DIR
       echo "
       #!/bin/sh
       vdir=/sbin
       . $vdir/vasm-functions
       #/bin/grep ^Driver /etc/X11/xorg.conf | grep "nv"
       #if [ $? == 0 ]; then
       # if [ -e /etc/nvidia*.tlz ];then
       #   echo installing package
       #   installpkg /etc/nvidia*.tlz 1>/dev/null #2>/dev/null
       # fi
       # if [ -e /lib/modules/`uname -r`/kernel/drivers/video/nvidia.ko ]; then
       # NV=`/bin/grep ^Driver /etc/X11/xorg.conf | grep "nv"|cut -d \" -f 2`
       #   if [ "$NV" == "nv" ];then
       #     sed s'/nv/nvidia/g' /etc/X11/xorg.conf >/etc/X11/xorg.conf.new
       #     mv /etc/X11/xorg.conf.new /etc/X11/xorg.conf
       #   fi
       # fi
       #else
       # echo $?
       #fi
       " > $INIT_DIR/config-stage2
       chmod a+x $INIT_DIR/config-stage2
     

#     msgbox "all done you can reboot now and try it out" Success 

## Step 6 - umount and ask to reboot

mount | grep "/dev" | awk '{print $1}' | xargs umount

   $DIALOG --icon $ICON --title $TITLE --yesno "Installation finished \n \n Do you want to reboot now?" 600x400
    if [ $? != 1 ]; then
       reboot
    fi


     return 0
}

############################
# MAIN MENU
wizard menu_install

