#!/bin/sh
#vliveinstall a script to install vectorlive to the hard disk
#
#
#
# Written By Uel Archuletta for VectorLinux
#
# Modified by Tony Brijeski for VectorLinux
# Modified by nightflier for VectorLinux 7.0

# run runonce script on next boot
cp /root/.vmklive/runonce* /var/log/setup/init

EXIT_DIR="${PWD}"
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="Vector-Linux-Live-Installer"
ICON="/usr/share/icons/vl-ball.xpm"

$DIALOG --icon $ICON --title $TITLE --yesno "Install the LiveCD to your hard drive.\n\n
This very basic installer requires proper manual interaction. \n
You need an understanding of Linux and computer hardware to do this safely. \n
If your hard drive has existing files, there is risk of data loss. \n
Please back up any data before proceeding. \n\n
Would you like to proceed now?" 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 "Scanning hard drives..." 600x400 0


## Step 2 - run gparted to setup partitions

$DIALOG --icon $ICON --title $TITLE --yesno "Partition hard drive. \n\n
You need a cleanly formatted target partition to install to. \n
If your drive is already prepared, click No to go to the next step.\n\n
Otherwise click Yes to launch the partitioning tool gparted.\n
Make sure to click the Apply button after specifying your drive layout.
" 600x400
    if [ $? != 1 ]; then
     FORMATWARNING=
     gparted
    else
     echo "Skipping partitioning step. Root partition will be re-formatted with existing type."
     DEFAULTFS=yes
     FORMATWARNING=", IT WILL BE FORMATTED!"
    fi

## 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 - configure some system settings.

$DIALOG --icon $ICON --title $TITLE --yesno "Create users and passwords. \n\n
For convenience, there are no passwords on the LiveCD.\n
Of course, this is not a good idea on an installed system.\n
Click Yes to create a user name and password for yourself.\n
Also assign a password to the root account.
" 600x400
    if [ $? != 1 ]; then
     vuser
    else
     echo "Living dangerously.. okay, you're the boss."
    fi

$DIALOG --title $TITLE --yesno "Configure time and date? \n\n
If your date and time is correct, you can skip this step. \n
Otherwise, you may configure it now. \n \n
Click Yes to configure, \n
Click No to skip." 600x400
    if [ $? != 1 ]; then
     vzoneset
     vdateset
    else
     echo "skipping time and date setup"
    fi

#$DIALOG --title $TITLE --yesno "Configure hardware? \n\n
#You now have the option to re-configure your hardware and system initialization. \n
#You can also do this later by selecting VASM from the system menu.   \n \n
#If all your system settings are the way you want them, you can skip this step. \n \n
#Click Yes to configure, \n
#Click No to skip." 600x400
#    if [ $? != 1 ]; then
#     /sbin/vhwconf
#     /sbin/vxconf
#     /sbin/valsaconf
     /sbin/vhwset
     /sbin/vsrvset
#    else
#     echo "skipping autosetup"
#    fi

$DIALOG --title $TITLE --yesno "Install Live Tools? \n\n
If you want to create your own LiveCD version later on, select Yes. \n
Otherwise, select No to reduce desktop clutter." 600x400
    if [ $? != 1 ]; then
     echo "Keeping Live Tools"
     LIVETOOLS=YES
    else
     echo "Removing Live Tools" 
     LIVETOOLS=NO
    fi

## 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"
     k="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`
         device[$k]="$(echo $i | sed s^/mnt/^/dev/^)"
         k=`expr $k + 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 \n
     If all you see is the placeholder,\n
     then your partitions are not mounted \n
     or do not contain a linux filesystem.\n
     " \
     600x400 4 \
     "placeholder" "Do not choose this" \
     ${part[0]} ${device[0]} \
     ${part[1]} ${device[1]} \
     ${part[2]} ${device[2]} \
     ${part[3]} ${device[3]} \
     ${part[4]} ${device[4]} \
     ${part[5]} ${device[5]} \
     ${part[6]} ${device[6]} \
     ${part[7]} ${device[7]} \
     ${part[8]} ${device[8]} \
     ${part[9]} ${device[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"
      k="0"
      for i in $partitions; do
        home[$j]="$i"
        j=`expr $j + 1`
        slice[$k]="$(echo $i | sed s^/mnt/^/dev/^)"
        k=`expr $k + 1`
      done
      $DIALOG --icon $ICON --title $TITLE --menubox "\n
      Which partition would you like to use as your home partition?\n \n
      If all you see is the placeholder,\n
      then we cannot find a suitable partition to use.\n
      If so, check that your partitions are mounted. \n
      " \
      600x400 4 \
      "placeholder" "Do not choose this" \
      ${home[0]} ${slice[0]} \
      ${home[1]} ${slice[1]} \
      ${home[2]} ${slice[2]} \
      ${home[3]} ${slice[3]} \
      ${home[4]} ${slice[4]} \
      ${home[5]} ${slice[5]} \
      ${home[6]} ${slice[6]} \
      ${home[7]} ${slice[7]} \
      ${home[8]} ${slice[8]} \
      ${home[9]} ${slice[9]} \
      2> $freply
      HOMEPART="`cat $freply`"

     if [ "$HOMEPART" == "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 [ "$HOMEPART" != "" ]; then
	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

### check for files on target
if [ ! -z "$(ls $TARGET | grep -v lost+found )" ]; then
$DIALOG --icon $ICON --title $TITLE --yesno "Installation target contains files! \n
This is not good. \n
Do you want to proceed anyway? \n \n
Click No to exit. \n
Click Yes to continue.
" 600x400
    if [ $? != 1 ]; then
     echo "Proceeding with install to unclean target"
    else
     exit 0
    fi
fi

if [ -z "$(echo $HOMEPART)" ]; then HOMEPART="same as above";fi

$DIALOG --icon $ICON --title $TITLE --yesno "Ready to install. \n \n
The target partition for system files is $(echo $TARGET | sed s^/mnt/^/dev/^)$FORMATWARNING \n
The target partition for /home is $(echo $HOMEPART | sed s^/mnt/^/dev/^) \n
Do you want to proceed? \n \n
Click No to exit. \n
Click Yes to continue.
" 600x400
    if [ $? != 1 ]; then
     echo "Here we go.. "
    else
     exit 0
    fi

# if gparted was skipped, re-format root partition with existing type, or reiserfs if none
if [ "${DEFAULTFS}x" = "yesx" ];then
  ZAPIT=$(mount | grep $TARGET | cut -d " " -f1)
  ZAPTYPE=$(mount | grep $TARGET | cut -d " " -f5)
  if [ -z "$ZAPTYPE" ];then ZAPTYPE=reiserfs;fi
  echo "formatting $ZAPIT with $ZAPTYPE"
    if [ $ZAPTYPE = ext2 ]||[ $ZAPTYPE = ext3 ]||[ $ZAPTYPE = ext4 ]||[ $ZAPTYPE = reiserfs ]||[ $ZAPTYPE = jfs ];then 
      umount $ZAPIT && echo y | mkfs.$ZAPTYPE $ZAPIT && mount $ZAPIT $TARGET
   	 if [ $ZAPTYPE = xfs ];then
           umount $ZAPIT && mkfs.xfs -f $ZAPIT && mount $ZAPIT $TARGET
           else
              echo "Unknown file system format, bailing.."
       fi
    fi
fi

if [ "$sephome" == "yes" ];then
    (sleep 2; echo "XXXX" ) | $DIALOG --icon $ICON --title $TITLE --no-buttons --infobox "Copying files to /home/" 600x400 0
    cp --preserve=all -R /home/* $HOMEPART/
fi

#(sleep 2; 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"
      if [ "$sephome" == "yes" ];then	rm -rf $TARGET/home/*;fi
      #/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" 640x400

   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

    
   tgtdev=`mount |egrep $TARGET | cut -d " " -f 1`
   
   rootfs=`mount | grep $tgtdev | cut -d " " -f5`   
   echo "$tgtdev / $rootfs	defaults	1 1" > $TARGET/etc/fstab
   if [ "$sephome" == "yes" ]; then
     homefs=`mount | grep $HOMEPART | cut -d " " -f5`
     HOMEDEV=$(echo $HOMEPART | sed s^/mnt/^/dev/^)
     echo "$HOMEDEV /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
tmpfs            /dev/shm         tmpfs        defaults         0   0

# CDROM, CDWRITER, DVD, FLOPPY
#/dev/cdrom       /mnt/cdrom       iso9660      defaults,noauto,ro,user    0 0
#/dev/cdrom1      /mnt/cdrom1      iso9660      defaults,noauto,ro,user    0 0
#/dev/cdwriter    /mnt/cdwriter    iso9660      defaults,noauto,rw,user    0 0
#/dev/dvd         /mnt/dvd         auto         defaults,noauto,ro,user    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.S && cp 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.M && cp 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.local && cp 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
   cd $TARGET/etc/rc.d/ && rm rc.K && cp rc.K.real rc.K

   ## Reset KDM to non-live defaults
   cd $TARGET/etc/kde/kdm && rm kdmrc && mv kdmrc.real kdmrc
   cd $TARGET/usr/share/apps/kdm/themes/std
   if [ -e std.xml.real ];then
     rm std.xml && mv std.xml.real std.xml
   fi

   # get off of $TARGET to allow clean umounts
   cd ${EXIT_DIR}

   ## Cleanup
   # get rid of wicd lock
   rm -f $TARGET/var/run/wicd/wicd.pid

   if [ "$LIVETOOLS" = "NO" ];then
   ## List of vl-live-tools files. Uncomment the ones to be removed
#	rm $TARGET/etc/X11/XF86Config-4-in
# 	rm $TARGET/etc/X11/xorg.conf-vesa
# 	rm $TARGET/etc/X11/xinit/xinitrc.jwm
# 	rm $TARGET/etc/rc.d/rc.K.live
# 	rm $TARGET/etc/rc.d/rc.M.live
# 	rm $TARGET/etc/rc.d/rc.S.live
# 	rm $TARGET/etc/rc.d/rc.local.live
# 	rm $TARGET/etc/rc.d/rc.K.real
# 	rm $TARGET/etc/rc.d/rc.M.real
# 	rm $TARGET/etc/rc.d/rc.S.real
# 	rm $TARGET/etc/rc.d/rc.local.real
 	rm $TARGET/etc/skel/Desktop/vmklive.desktop
 	rm $TARGET/etc/skel/Desktop/vliveinstall.desktop
 	rm $TARGET/home/vl/Desktop/vmklive.desktop
 	rm $TARGET/home/vl/Desktop/vliveinstall.desktop
 	rm -rf $TARGET/root/.vmklive
 	rm $TARGET/root/Desktop/vmklive.desktop
 	rm $TARGET/root/Desktop/vliveinstall.desktop
 	rm $TARGET/sbin/vl-usb-install
 	rm $TARGET/sbin/vliveinstall
 	rm $TARGET/sbin/vmklive
# 	rm $TARGET/sbin/VLXconf
# 	rm $TARGET/sbin/VLsetup
# 	rm $TARGET/sbin/alsaconf-quiet
# 	rm $TARGET/sbin/ddcprobe
# 	rm $TARGET/sbin/ddcxinfo
# 	rm $TARGET/sbin/ddcxinfo-vector
# 	rm $TARGET/sbin/modetest
# 	rm $TARGET/sbin/svgamodes
# 	rm $TARGET/sbin/vxconf-quiet
 	rm $TARGET/usr/share/applications/vl-usb.desktop
 	rm $TARGET/usr/share/applications/vliveinstall.desktop
 	rm $TARGET/usr/share/applications/vmklive.desktop
# 	rm $TARGET/usr/share/icons/vlball.xpm
# 	rm $TARGET/usr/share/icons/ROX-nuvola/apps/vector.png
	if [ "$sephome" == "yes" ];then
	  rm $HOMEPART/vl/Desktop/vliveinstall.desktop
	  rm $HOMEPART/vl/Desktop/vmklive.desktop
	fi
   fi


   HardDisk=$(fdisk -l | grep -e "Disk.*/dev" | cut -d " " -f 2 | cut -d \: -f 1)
   x=$(echo $HardDisk | wc -w)
   root=$(cat /proc/mounts |grep ^/dev |grep $TARGET |cut -d " " -f 1)
   if [ $x != 1 ]; then  
     j="0"
     for i in $HardDisk; do
       hd[$j]="$i"
       j=`expr $j + 1`
     done
   else
     hd[0]=$HardDisk
   fi
     $DIALOG --icon $ICON --title $TITLE --menubox "\n
     Setup boot loader? \n\n
     Where do you want to install the Linux Loader (lilo)? \n\n
     If you choose Exit or No,\n
     you will have to setup your boot manager manually." \
     600x400 4 \
     "Exit" "Do not install LILO" \
     ${hd[0]} ${hd[0]} \
     ${hd[1]} ${hd[1]} \
     ${hd[2]} ${hd[2]} \
     ${hd[3]} ${hd[3]} \
     ${device[0]} ${device[0]} \
     ${device[1]} ${device[1]} \
     ${device[2]} ${device[2]} \
     ${device[3]} ${device[3]} \
     ${device[4]} ${device[4]} \
     ${device[5]} ${device[5]} \
     ${device[6]} ${device[6]} \
     ${device[7]} ${device[7]} \
     ${device[8]} ${device[8]} \
     ${device[9]} ${device[9]} \
     "/dev/fd0" "Floppy" \
     2> $freply
     escape=$?
     result="$(cat $freply)"
     if [ $escape != 0 ];then
       result=Exit
     fi
     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 = Vector
       append=\"4 splash=silent\"
       read-only       
       " >$TARGET/etc/lilo.conf
       infobox "setting up lilo"
       mount -o bind /dev $TARGET/dev
       mount -o bind /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 "Reboot computer? \n\n
Installation is almost done. \n
The computer needs to restart to complete initialization and configuration. \n
The CD will eject during reboot to facilitate startup from the hard drive. \n\n
If there is a problem with the LILO boot loader, \n
you can start your system using the CD. \n
Select and edit as appropriate the entry for Boot VL installed... \n\n
Click No to keep using the LiveCD.\n
Click Yes to reboot now.
" 600x400
    if [ $? != 1 ]; then
       reboot
    fi


     return 0
}

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

