################################################## ################################################## #####
format()
{
umount -l /dev/${target} >/dev/null 2>&1
echo When in cfdisk do NOT make ${targetpath} bootable, otherwise a re-boot will be required.
echo
if [ "${1}" = "F" -o "${1}" = "f" ]; then
read -p "Partition drive prior to format? (Y)es,(N)o or (C)ancel " NEW_MASTER
case ${NEW_MASTER} in
[Yy]) cfdisk;; #echo>//dev/null
[Nn]) ;;
*) echo; echo "Test Cancelled"; exit;;
esac
fi

unset NEW_MASTER
echo Formatting /dev/${target}, please wait...
mkfs.ext3 /dev/${target}
#mount -t ext3 -rw /dev/${target} ${targetpath}
mount /dev/${target} ${targetpath}
}
################################################## ################################################## #####
delknx()
{
if [ -d ${targetpath}/knx ]; then
echo "Please wait deleting old build"
rm -rf ${targetpath}/knx/
fi
}
################################################## ################################################## #####
update()
{
echo "Current target path is ${targetpath}"
echo "Enter new path or just press return"
read temp1
if [ -n "${temp1}" ]; then
targetpath=${temp1}
mount ${targetpath}
fi
unset temp1

echo "Current external file path is ${extfilepath} - Do NOT enter the /media/"
echo "Enter new path or just press return"
read temp1
if [ -n "${temp1}" ]; then
extfilepath=${temp1}
mount /media/${extfilepath}
fi
unset temp1

}
################################################## ######################################
chroot2()
{

#Return back from CHROOT environment

# Should help if home directories nor saved in re-mastered versions
# rsync -a ${targetpath}/knx/source/KNOPPIX/home/knoppix/ ${targetpath}/knx/source/KNOPPIX/etc/skel/
# chown root:root -R ${targetpath}/knx/source/KNOPPIX/etc/skel/
# rm -rf ${targetpath}/knx/source/KNOPPIX/home/knoppix

clear
echo Now install1.sh active again
echo
echo Prompt should be back to 'root!tty1:/#'
echo Only run after install3.sh and exit to CHROOT
echo

# Put startx back to orig again by re-instating text init 0 etc.
sed 's@echo "/sbin/init 0"@/sbin/init 0@' -i ${targetpath}/knx/source/KNOPPIX/etc/init.d/knoppix-startx
rm -f ${targetpath}/knx/source/KNOPPIX/etc/X11/xorg.conf
rm -f ${targetpath}/knx/source/KNOPPIX/install2.sh
rm -f ${targetpath}/knx/source/KNOPPIX/install3.sh
rm -f ${targetpath}/knx/source/KNOPPIX/dpkg.deb
# read -p "pause"

if [ "${1}" = "2" ]; then
#Remove some files
\rm ${targetpath}/knx/master/KNOPPIX/index*.html 2>/dev/null
\rm ${targetpath}/knx/master/KNOPPIX/KNOPPIX-FAQ-*.txt 2>/dev/null
\rm ${targetpath}/knx/master/boot/isolinux/german.kbd 2>/dev/null

# Update kde splash screen
\cp /media/${extfilepath}/EafBuildImages/splash_top.png ${targetpath}/knx/source/KNOPPIX/usr/share/apps/ksplash/Themes/Knoppix
\cp /media/${extfilepath}/EafBuildImages/splash_top.png ${targetpath}/knx/source/KNOPPIX/usr/share/apps/ksplash/Themes/Default
fi

#Blanket umount
umount -l /media/${extfilepath} >/dev/null 2>&1
umount -l /media/fd0 >/dev/null 2>&1
umount -l /share1 >/dev/null 2>&1

apt-get clean

rm -rf ${targetpath}/knx/source/KNOPPIX/.rr_moved

clear
echo
echo Making the big compressed KNOPPIX ISO 9660 filesystem - used by the cloop driver:

# Get documented error with above, try -m -B
mkisofs -R -U -V "Knoppixfilesystem" -publisher "Knoppix" -input-charset ISO-8859-15 -allow-leading-dots -hide-rr-moved -cache-inodes -no-bak -pad ${targetpath}/knx/source/KNOPPIX | nice -5 /usr/bin/create_compressed_fs - 131072 > ${targetpath}/knx/master/KNOPPIX/KNOPPIX
# NOTE: can add --best for higher compression ie: /usr/bin/create_compressed_fs --best - 65536
cd ${targetpath}/knx/master

echo
echo Updating the md5 hashes of the files included in the ISO, used for integrity checking
echo

find -type f -not -name md5sums -not -name boot.cat -not -name isolinux.bin -exec md5sum '{}' \; > KNOPPIX/md5sums

#v6 required?
#find -type f -not -name sha1sums -not -name boot.cat -not -name isolinux.bin \
#-exec sha1sum '{}' \; > KNOPPIX/sha1sums

echo
echo "Pleae wait making the iso image"

#mkisofs -pad -l -r -J -v -V "KNOPPIX" -no-emul-boot -boot-load-size 4 \
# -boot-info-table -b boot/isolinux/isolinux.bin -c boot/isolinux/boot.cat \
# -hide-rr-moved -o ${targetpath}/knx/knoppix.iso ${targetpath}/knx/master


genisoimage -pad -l -r -J -v -V "KNOPPIX" -no-emul-boot -boot-load-size 4 -boot-info-table -b boot/isolinux/isolinux.bin -c boot/isolinux/boot.cat -hide-rr-moved -o ${targetpath}/knx/knoppix.iso ${targetpath}/knx/master

echo
echo Size of finished iso is:
ls -lh ${targetpath}/knx/knoppix.iso
echo
echo Script finished,the ISO is stored in ${targetpath}/knx/knoppix.iso
read -p "Press any key to end"
}

################################################## ################################################## #####
#Main body of script
################################################## ################################################## #####
targetpath=/media/sda4
target=sda4
extfilepath=sda3
TMP=${targetpath}/knx/tmp

swapon -s | grep "/dev" >/dev/null 2>&1
if [ "${?}" -gt "0" ]; then
mkswap /dev/sda1
swapon /dev/sda1
fi

for i in ${target} ${extfilepath}; do
mount | grep "/dev/${i}" 1>/dev/null 2>&1
if [ "${?}" -eq 0 ]; then
echo "/dev/${i} is already mounted"
else
if [ ! -d /media/${i} ];then
sudo mkdir /media/${i} >/dev/null
fi
echo "Mounting /dev/${i} /media/${i}"
mount /dev/${i} /media/${i}
fi
done

mainmenu