#!/bin/bash


#get kernel params, may be overridden on the command line
HDISOPART=`cat /proc/cmdline  | perl -ni -e 'if (/gxisopart=(\S+)/) { print $1; }'`
grep gxlivehd /proc/cmdline > /dev/null 2>&1
if [ $? -eq 0 ] ; then
	LIVEHD=1
fi


######################################################################################
#parse command line arguments
until [ -z "$1" ]
do
  CURARG=$1
  case "$CURARG" in
    "--upgrade-1004")        UPGRADE1004=1  ;;
    "--live-hd")        LIVEHD=1  ;;
    "--net-iso="* )        NETISO=${CURARG#*=} ;;
    "--net-iso"   ) shift; NETISO=$1 ;;
    "--hd-iso-part="* )        HDISOPART=${CURARG#*=} ;;
    "--hd-iso-part"   ) shift; HDISOPART=$1 ;;
    "--hd-iso-file="* )        HDISOFILE=${CURARG#*=} ;;
    "--hd-iso-file"   ) shift; HDISOFILE=$1 ;;
  esac
  shift
done

#disable LIVEHD protection, repartition drive past HDISOPART since 2017 has different partitioning scheme vs. 2013
unset LIVEHD

#handle undefined variables
#for booleans, undefined or "" is the only boolean false, test using: if [ $MYFLAG ]
#if [ -z "$MYFILE" ] ; then MYFILE="default" ;fi

#checks
if [ -n "$HDISOPART" ] ; then
if [ "$HDISOPART" -eq 1 ] ; then 
	echo HDISOPART is 1
elif [ "$HDISOPART" -eq 2 ] ; then
	echo HDISOPART is 2
else
	echo 'FATAL ERROR: --hd-iso-part must be 1 or 2'
	exit 1
fi
fi
######################################################################################




ROOTFS=/mnt/gxinstall/rootfs/
ISOLOC=/mnt/gxinstall/rescue/

echo -n "$0 starting at "
date

#Script to repartition the hard drive for Groovix usage
# functions taken from code Written by Luis J Salazar for the Drake Mass Upgrade Project

#Set vars
#Declare the minimum size of the disk drive for the Groovix system
MINSIZE=14

function auto_network_interfaces_carry_over() {

	#abort if ip= was specified on command line
	cat /proc/cmdline  | grep ' ip=' 
	if [ $? -eq 0 ] ; then
		echo 'not attempting to use existing /etc/network/interfaces from hard drive ( specified ip= )'
		return 1
	fi

	cat /proc/cmdline  | grep 'gxinstall=auto'
	if [ $? -eq 0 ] ; then
		echo 'attempting to use existing /etc/network/interfaces from hard drive'

		if [ -e /gxinstallfiles/etc/network/interfaces ] ; then
			echo '/gxinstallfiles/etc/network/interfaces already exists, using that'
			return 0
		fi

		#try to find it
		mkdir -p /mnt/gxinstall/tmpmnt
		mkdir -p /gxinstallfiles/etc/network/
		# 2017 and 2013 root/etc partition 3 is /, 1004 partition 1 is /
		#mount ${GXHD}3 /mnt/gxinstall/tmpmnt || mount ${GXHD}1 /mnt/gxinstall/tmpmnt
		mount ${GXHD}3 /mnt/gxinstall/tmpmnt 
			cp /mnt/gxinstall/tmpmnt/etc/network/interfaces /gxinstallfiles/etc/network/interfaces.from-hard-drive-pre-install
		#umount ${GXHD}1
		umount ${GXHD}3

		if [ -e /gxinstallfiles/etc/network/interfaces.from-hard-drive-pre-install ] ; then
			echo 'found an old /etc/network/interfaces file on the hard drive'
		else
			echo 'could not find an /etc/network/interfaces file to use'
			return 1
		fi

		egrep 'static|groovix-preserve-custom-etc-network-interfaces|wireless|wpa|ssid' /gxinstallfiles/etc/network/interfaces.from-hard-drive-pre-install
		if [ $? -eq 0 ] ; then
			echo 'interfaces file contains static or groovix-preserve-custom-etc-network-interfaces or wireless, preserving it '
			cp /gxinstallfiles/etc/network/interfaces.from-hard-drive-pre-install /gxinstallfiles/etc/network/interfaces
		else
			echo 'interfaces file does NOT contain static or groovix-preserve-custom-etc-network-interfaces or wireless, NOT preserving it '
			return 1
		fi

	else
		echo 'not attempting to use existing /etc/network/interfaces from hard drive (not gxinstall=auto)'
	fi


}

function check_disk_size() {

DISKSIZE=`fdisk -l $GXHD | grep Disk | grep dev | cut -d " " -f 3  | awk -F '.' '{ print $1; exit; }'`
if [ "$DISKSIZE" -lt "$MINSIZE" ];
  then

echo "The disk size of this computer is too small.  Your disk is size $DISKSIZE Gigs.  The Groovix installer requires a minimum of $MINSIZE Gigs.  This installer will exit at this time.  No changes were made to the disk."
  exit 1
else

echo "Your disk is $DISKSIZE Gigs.  This is big enough for the install."

fi
}

function which_hd() {
	GXHDKP=`cat /proc/cmdline  | perl -ni -e 'if (/gxihd=(\S+)/) { print $1; }'`
	if [ -n "$GXHDKP" ] ; then
		#if specified on kernel command line, use gxihd= value
       		GXHD=$GXHDKP
	else
		#otherwise use the first hard drive we find

		#TODO: should exclude drive we're running from 
		# only if installing from usb to real hard drive
		#  install from rescue part to rest of hd is ok

		# if running rescue we're on part 2, usb we're on part 1:
		mount | grep "/cdrom "
		CURUSB=$(mount | grep "1 on /cdrom " | cut -f 1 -d" " | cut -c 1-8)
                if [[ -z "$CURUSB" ]] ; then
			GXHD=`groovix-list-devices disk | head -n 1`
		else
                	echo NOT USING CURUSB which is $CURUSB
			GXHD=`groovix-list-devices disk | grep -v "$CURUSB" | head -n 1`
		fi
	fi

	# deal with MMC "hard drives" that use a p in the partition names, Intel NUCs, etc.
	echo "$GXHD" | grep mmcblk
	if [ $? -eq 0 ] ; then
		ln -s $GXHD ${GXHD}p
		GXHD=${GXHD}p
	fi

	if [ -n "$GXHD" ] ; then
		GXHDN=`basename $GXHD`
		echo "using $GXHD hard drive"
		echo "current partitioning scheme:"
		fdisk -l $GXHD
	else
		echo "no hard drive found, exiting"
		exit 1
	fi


}

#Once we get here, there's no going back.
function groovix_repartition() {

#echo "Using dd to wipe sda"
#dd if=/dev/zero of=/dev/sda bs=512 count=1


echo
echo turning off swap
echo swapoff -a
swapoff -a
echo

#unmount any mounted partitions on the target - maybe we are installing to a usb drive that the gui auto-mounted
echo umount $GXHD*
umount $GXHD*


# MINSIZE is already checked

#left over for var should be at the VERY LEAST, about 8GB using these raw numbers, could turn out to be much less

#2018-01-11 reducing swap sizes, having a huge swap doesn't necessarily help, system will just bog down with hard drive activity

#16GB would be very tight but should be possible without too much installed in /usr, might have to give up SRCD partition and use network iso download re-install method
if [ "$DISKSIZE" -lt 29 ] ; then
	SWAP_SIZE=256M
	SRCD_SIZE=3000M
	ROOT_SIZE=8500M
	#USR_SIZE=6000M
	TMP_SIZE=512M
	HOME_SIZE=512M
	# var size is always the remainder, ~ 2GB for 16GB drive
elif [ "$DISKSIZE" -lt 55 ] ; then
	SWAP_SIZE=512M
	SRCD_SIZE=6G
	ROOT_SIZE=12G
	#USR_SIZE=10G
	TMP_SIZE=1G
	HOME_SIZE=2G
	# var size is always the remainder, ~ 6GB for 30GB drive
elif [ "$DISKSIZE" -lt 75 ] ; then
	SWAP_SIZE=1G
	SRCD_SIZE=8G
	ROOT_SIZE=15G
	#USR_SIZE=10G
	TMP_SIZE=5G
	HOME_SIZE=10G
	# home not big enough for virtual machines
	# var size is always the remainder, ~ 10GB for 55GB drive
elif [ "$DISKSIZE" -lt 115 ] ; then
	SWAP_SIZE=2G
	SRCD_SIZE=10G
	ROOT_SIZE=20G
	#USR_SIZE=15G
	TMP_SIZE=5G
	HOME_SIZE=20G
	# home big enough for virtual machines
	# var size is always the remainder, ~ 10GB for 75GB drive
elif [ "$DISKSIZE" -lt 240 ] ; then
        SWAP_SIZE=4G
        SRCD_SIZE=10G
        ROOT_SIZE=30G
        #USR_SIZE=20G
        TMP_SIZE=10G
        HOME_SIZE=30G
	# home plenty big enough for virtual machines
        # var size is always the remainder, ~ 15GB for 115GB drive
else
	SWAP_SIZE=8G
	SRCD_SIZE=20G
	ROOT_SIZE=60G
	#USR_SIZE=30G
	TMP_SIZE=20G
	HOME_SIZE=60G
	# home big enough for several virtual machines
	# var size is always the remainder, ~ 60GB for 240GB drive
fi

# if we're running live off of files on the hard drive, don't touch the partitioning at all
#  fdisk gives warning about "Re-reading the partition table failed with error 16" and 
#    running partprobe doesn't make them go away like it normally does when the hard drive is completely unmounted
if [ -z "$LIVEHD" ] ; then

	echo
	echo "##############################################################"
	echo "here is your partition table pre fdisk"
	fdisk -l
	echo "##############################################################"
	echo
	echo "Using fdisk to partion drive"
	sleep 5

if [ -z "$HDISOPART" ] ; then

	# new partition table and make first partition

	#get rid of GPT
	fdisk -l $GXHD 2>&1 | grep "GUID Partition Table"
	if [ $? -eq 0 ] ; then
		echo
		echo
		echo
		echo "Groovix detected GPT on $GXHD, trying to remove it"

		# gdisk -l sometimes stops and asks whether to use MBR or GPT !!
		#echo "current GPT: gdisk -l $GXHD"
		#gdisk -l $GXHD
		echo parted --script $GXHD print
		parted --script $GXHD print

		# scripted mode doesn't work due to it thinking drive is in use and no Ignore error option
		#parted -- $GXHD mklabel msdos Ignore Yes Ignore quit
		echo parted --script --  $GXHD mklabel msdos
		parted --script --  $GXHD mklabel msdos
		if [ $? -ne 0 ] ; then
			echo sgdisk --zap $GXHD
			sgdisk --zap $GXHD
		fi
		fdisk -l $GXHD 2>&1 | grep "GUID Partition Table"
		if [ $? -eq 0 ] ; then
			echo
			echo
			echo
			echo "####################################################################"
			echo "####      ATTENTION: MANUAL INTERVENTION REQUIRED!!!!!!!    ########"
			echo "####          \"Ignore\" all errors, answer \"Yes\"!            ########"
			echo "####################################################################"
			echo
			
			parted -- $GXHD mklabel msdos 
		else
			echo "####################################################################"
			echo "#######         GPT HAS BEEN ERASED SUCCESFULLY!           #########"
			echo "####################################################################"
		fi
		partprobe
		partprobe -s

		# gdisk -l sometimes stops and asks whether to use MBR or GPT !!
		#echo "after erasing GPT: gdisk -l $GXHD"
		#gdisk -l $GXHD
		echo "after erasing GPT: parted --script $GXHD print"
		parted --script $GXHD print
	fi
	
	fdisk $GXHD <<EOF
o
n
p


+$SWAP_SIZE
w
EOF
	partprobe
	partprobe -s

else
	# only delete down to the partition number that holds our iso
	for T in $(fdisk -l | egrep "^/dev/" | cut -f 1 -d" " | perl -pi -e 's/.*(\d+).*/$1/' | tac) ; do
		if [ "$T" -eq $HDISOPART ] ; then
			break
		fi
		echo deleting partition $T

		# to get out of the delete automatically if the partition doesn't exist, we could delete partition 4, which it accepts even if it doesn't exist!
		fdisk $GXHD <<EOF
d
$T
w
EOF
partprobe
partprobe -s

	done


	# now if any of those partitions left are set bootable, toggle them
	for T in $(fdisk -l $GXHD | egrep "^/dev/.*\s+\*\s+" | cut -f 1 -d" " | perl -pi -e 's/.*(\d+).*/$1/' | tac) ; do
	
		fdisk $GXHD <<EOF
a
$T
w
EOF
partprobe
partprobe -s

	done

fi 
# end if HDISOPART


fi
# end if LIVEHD

#echo
#echo "##############################################################"
#echo fdisk up to partition 1:
#fdisk -l
#echo "##############################################################"
#echo



if [ -z "$LIVEHD" ] ; then
#add on second partition unless we are using it and didn't delete it
if [ "$HDISOPART" != "2" ] ; then

	fdisk $GXHD <<EOF
n
p


+$SRCD_SIZE
w
EOF
partprobe
partprobe -s

fi
fi

#echo
#echo "##############################################################"
#echo fdisk up to partition 2:
#fdisk -l
#echo "##############################################################"
#echo

if [ -z "$LIVEHD" ] ; then

	#make the rest of the paritions
	fdisk $GXHD <<EOF
n
p


+$ROOT_SIZE
n
e



n

+$TMP_SIZE
n

+$HOME_SIZE
n




a
3
t
1
82
w
EOF

	partprobe 
	partprobe -s

	sleep 1
	echo debug
	ls -l $GXHD*
	partprobe -s
	sleep 1
	ls -l $GXHD*
	echo
	echo "##############################################################"
	echo "here is your partition table post fdisk:"
	fdisk -l
	echo " and as reported by cat /proc/partitions :"
	cat /proc/partitions
	sleep 5
	echo "##############################################################"
	echo
	
	echo
	echo "$GXHD drive is now repartitioned"

else

	echo
	echo "$GXHD drive has NOT been repartitioned because we're running live from the hd"

fi

sleep 5

}

function warn_delay() {
	echo
	echo
	echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! "	
	echo " !!!! WARNING WARNING WARNING !!!!!!!!!!!!!!!!!!!!!!!!!!! "
	echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! "	
	echo
	echo "This install procedure will reformat the first disk drive in this computer."
	echo "All data on $GXHD will be LOST forever."
	echo 
	echo "Hard drive erasure will take place in 30 seconds, hit Enter to proceed immediately."
	echo
	echo "TO CANCEL the install, hold in the power button to turn off the computer NOW!!!!!!!!"

	TMOUT=30
        read delay

	echo "Installation proceeding"

}


############################################################################
############################################################################
#This is the beginning of the install script
############################################################################
############################################################################

#disable console blanking 
setterm -blank 0 -powersave off

#which hard drive to use?
which_hd

echo
echo "#############################################################"
echo "# Using hard drive $GXHD with base name $GXHDN "
echo "#############################################################"
echo
sleep 5

#Then we give the warning
warn_delay

#Then we check disk size
check_disk_size

# try to carry over network info for static ips
auto_network_interfaces_carry_over

#then do it
groovix_repartition

#############################################################
# sync filesystems

#unset TMOUT
#echo DEBUG hit enter to continue
#read a

mkdir /mnt/gxinstall/
 
if [ "$HDISOPART" = 1 ] ; then
	echo "using partition 1, not activating swap yet"
	ISOLOC=/mnt/gxinstall/part1
	mkdir /mnt/gxinstall/part1
	mount ${GXHD}1 /mnt/gxinstall/part1
else
	#get swap ready first in case we need it
	mkswap ${GXHD}1
	swapon ${GXHD}1
fi

if [  "$HDISOPART" != 2 ] ; then
	umount ${GXHD}2
	set -e
	mkfs.ext4 -F ${GXHD}2
	set +e
fi


#for T in 3 5 6 7 8 ; do
for T in 3 5 6 7 ; do
	umount ${GXHD}$T
	set -e
	mkfs.ext4 -F ${GXHD}$T
	set +e
done

echo "Getting ready to roll out the filesystem."


#for T in rescue usr tmp home var rootfs ; do
for T in rescue tmp home var rootfs ; do
	mkdir /mnt/gxinstall/$T
done

if [  "$HDISOPART" != 2 ] ; then
	set -e
	mount ${GXHD}2 /mnt/gxinstall/rescue
	set +e
fi

set -e
mount ${GXHD}3 /mnt/gxinstall/rootfs
#mount ${GXHD}5 /mnt/gxinstall/usr
mount ${GXHD}5 /mnt/gxinstall/tmp
mount ${GXHD}6 /mnt/gxinstall/home
mount ${GXHD}7 /mnt/gxinstall/var
set +e

echo
echo after all partitions mounted df shows:
df
echo


#if [ $? -ne 0 ] ; then
#	echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! "	
#	echo " !!!!!!!!  FATAL ERROR: PAUSING INSTALLATION   !!!!!!!!!! "	
#	echo " !!!!!!!!  Hit Enter to continue anyways       !!!!!!!!!! "	
#	echo " !!!!!!!!  Hit ctrl-alt-F2 for debug terminal  !!!!!!!!!! "	
#	echo " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! "	
#	unset TMOUT
#	read GXCONTINUE
#fi

if [ -n "$NETISO" ] ; then 
	# if we are doing an installing using an .iso image over the network, grab it now
	# should usually have enough space on /mnt/gxinstall/rescue (5G minimum for .iso and the extraction) 
	# on 16GB hard drives, rescue is only 3GB
	# if necessary we could destructively erase enough stuff to make enough room in another partition
	# otherwise need to use alternate scheme where we erase /rescue, get tar file of iso struct without .squashfs
	# put iso struct into place, then download .squashfs right into place
	wget $NETISO -O $ISOLOC/groovix-image.iso
	HDISOFILE=groovix-image.iso
fi

#shared net and hd iso installation setup
if [ -n "$NETISO" ] || [ -n "$HDISOPART" ]  ; then 
	# if we're booting an .iso from grub, then HDISOPART is set but HDISOFILE is not, and /rofs is already mounted
	if [ -n "$HDISOFILE" ] ; then
		umount /cdrom 2>/dev/null
		mv /cdrom /cdrom.old 2>/dev/null
		mkdir /cdrom
		mount -o loop $ISOLOC/$HDISOFILE /cdrom
		umount /rofs 2>/dev/null
		mv /rofs /rofs.old 2>/dev/null
		mkdir /rofs
		mount -o loop /cdrom/casper/filesystem.squashfs /rofs
	fi

fi



#unset TMOUT
#echo DEBUG hit enter to continue
#read a


#die on any errors in this section
set -e

#to be able to boot live off hard drive
# if this doesn't work we can always create an .iso at this point from contents of /cdrom since 
# booting a .iso is well supported and documented by grub2
# But we wouldn't have way to control which option we're booting into though  - maybe need to modify some files 
#  before creating the .iso
	
if [  "$HDISOPART" != 2 ] ; then
	#if we're already running from the live copy in partition 2 we can't sync to ourself
	rsync -av /cdrom/ /mnt/gxinstall/rescue/
fi


#rsync -av --exclude=/var --exclude=/home --exclude=/tmp --exclude=/usr --exclude=/rescue /rofs/ /mnt/gxinstall/rootfs/
rsync -av --exclude=/var --exclude=/home --exclude=/tmp  --exclude=/rescue /rofs/ /mnt/gxinstall/rootfs/

#note that /rescue will not exist on the root partition - it really isn't needed , we only boot to it through grub, 
# don't need to clutter up file system by showing it

#rsync -av /rofs/usr/ /mnt/gxinstall/usr/
rsync -av /rofs/tmp/ /mnt/gxinstall/tmp/
rsync -av /rofs/home/ /mnt/gxinstall/home/
rsync -av /rofs/var/ /mnt/gxinstall/var/

#allow errors again
set +e

echo
echo after all partitions data copied df shows:
df
echo

##############################################
#prepare for chroot
##############################################


#mount partitions
#for T in usr var home tmp ; do
for T in var home tmp ; do
	mkdir $ROOTFS/$T
	mount -o bind /mnt/gxinstall/$T/ $ROOTFS/$T/
done

########################################################
# populate fstab
rm -rf $ROOTFS/etc/fstab
cp /usr/share/groovix-installer/templates/fstab $ROOTFS/etc/fstab
sed -i "s/GXHDN/$GXHDN/g" $ROOTFS/etc/fstab

# switch to UUIDs
cp $ROOTFS/etc/fstab $ROOTFS/etc/fstab.non-uuid
#for T in /dev/disk/by-uuid/* ; do UUID=$(basename $T ); DNAME=$(basename $(readlink $T)); sed -i "s/\/dev\/$DNAME/UUID=$UUID/" $ROOTFS/etc/fstab ; done
#/dev/disk/by-uuid not getting updated, specifically on /dev/mmcblk devices, use blkid instead
ls -lart /dev/disk/by-uuid
for T in $(cat $ROOTFS/etc/fstab  | cut -f 1 | egrep "^\/dev/" | cut -f 3 -d/) ; do UUID=$(blkid -p -s UUID -o value /dev/$T ); sed -i "s/^\/dev\/$T/UUID=$UUID/" $ROOTFS/etc/fstab ; done
grep UUID $ROOTFS/etc/fstab



# populate mtab , needed for grub-install 
rm -rf $ROOTFS/etc/mtab
cp /usr/share/groovix-installer/templates/mtab $ROOTFS/etc/mtab
sed -i "s/GXHDN/$GXHDN/g" $ROOTFS/etc/mtab
#grub gives errors (not fatal) if this doesn't exist
mkdir $ROOTFS/run/lock
chmod 777 $ROOTFS/run/lock

########################################################

#mount other things
for T in dev dev/pts proc sys ; do
	mount -o bind /$T $ROOTFS/$T
done

####################################################################
# install grub
####################################################################

chroot $ROOTFS grub-install $GXHD
chroot $ROOTFS update-grub $GXHD


#End of grub install

####################################################################
# copy files from live to hd 
####################################################################
# copy build.version file to know which iso this was installed from
date +%Y-%m-%d.%H:%M > $ROOTFS/var/lib/groovix/installer/install.timestamp

#backup network config that was already in the filesystem
cp $ROOTFS/etc/network/interfaces $ROOTFS/etc/network/interfaces.pre-groovix-install



# copy network config in from live environment  - maybe set up by gnc or initrd (including from /proc/cmdline)
cp /etc/network/interfaces $ROOTFS/etc/network/interfaces.from-live
cp /etc/network/interfaces $ROOTFS/etc/network/interfaces

#if this file exists then it was explicitly placed there to be copied into new system
cp /gxinstallfiles/etc/network/interfaces $ROOTFS/etc/network/interfaces.from-gxinstallfiles
cp /gxinstallfiles/etc/network/interfaces $ROOTFS/etc/network/interfaces 

# grab any other custom files we wanted to copy in
cp -arvp /gxinstallnewstruct/* $ROOTFS/



#don't get all these other interfaces defined in 2017 live, could come in very handy for wireless or later additions
cp $ROOTFS/etc/network/interfaces $ROOTFS/etc/network/interfaces.pre-groovix-installer-fix

IFFILE=$ROOTFS/etc/network/interfaces
        #for i in eth0 eth1 eth2 ath0 wlan0; do
        for i in eth0 eth1 eth2 wlan0; do
                echo "checking for $i in $IFFILE"
                egrep "^\s*iface $i" $IFFILE
                if [ $? -ne 0 ] ; then
                        echo "  adding $i ..."
                        echo 
                        cat >> "$IFFILE" <<EOF

#added by groovix installer
auto $i
iface $i inet dhcp

EOF
                fi
        done



echo
echo
echo "##############################################################"
echo "Live /etc/network/interfaces looks like this:"
echo "##############################################################"
cat /etc/network/interfaces
echo "##############################################################"
echo
echo
echo

echo
echo "##############################################################"
echo "Final (installed) $IFFILE looks like this:"
echo "##############################################################"
cat $IFFILE
echo "##############################################################"
echo
echo
echo


# copy groovix.conf from live environment.   
# must get parameters that were set on command line and applied
# by groovix startup 03-apply-kernel-parameters
cp $ROOTFS/etc/groovix/groovix.conf $ROOTFS/etc/groovix/groovix.conf.pre-groovix-install
echo diff /etc/groovix/groovix.conf $ROOTFS/etc/groovix/groovix.conf
diff /etc/groovix/groovix.conf $ROOTFS/etc/groovix/groovix.conf
cp /etc/groovix/groovix.conf $ROOTFS/etc/groovix/groovix.conf

#copy kernel cmdline parameters from install in case we need them later
cp /proc/cmdline $ROOTFS/var/lib/groovix/installer/cmdline


####################################################################
# sync logs
####################################################################

#won't be complete since we're still writing to it from this process
#try to flush buffer
#doesn't work anyways and just hides relevant messages in some cases
#echo "flushing tee buffer "
#for T in `seq 1 256` ; do
#	echo "$T"
#done
#echo "done flushing tee buffer "

cp /var/log/groovix/installer.log  $ROOTFS/var/log/groovix/installer.log



# unmount everything

if [ -n "$NETISO" ] ; then 
	echo 'there are no --net-iso clean up steps required at this time'
	# clean up if we did a --net-iso install
	## no - these can stay mounted
	#umount /rofs
	#umount /cdrom
	## this can stay here - might come in handy later
	#rm $ISOLOC/groovix-image.iso
fi



for T in dev/pts dev proc sys ; do
	umount $ROOTFS/$T
done

#for T in usr var home tmp ; do
for T in var home tmp ; do
	umount $ROOTFS/$T/
done

if [  "$HDISOPART" != 2 ] ; then
	#didn't mount it so don't umount it
	#will see errors here if NETISO since /rofs and /cdrom are still mounted
	umount /mnt/gxinstall/rescue
fi
#for T in rootfs usr tmp home var ; do
for T in rootfs tmp home var ; do
	umount /mnt/gxinstall/$T 
done

if [ "$HDISOPART" = 1 ] ; then
	echo "unmounting partition 1 and turning into swap now"
	#get swap ready first in case we need it
	umount /rofs
	umount /cdrom
	umount ${GXHD}1
	mkswap ${GXHD}1
	swapon ${GXHD}1
fi


echo -n "$0 completed at "
date

echo current ip addresses:
ifconfig | grep "inet addr"
echo

echo done installing to hard drive $GXHD with base name $GXHDN
echo
echo waiting 60 seconds before completing so you can review the messages above, hit enter to skip delay

unset TMOUT
TMOUT=60
read skipdelay

true
