############# knx-master.sh <dir> <targetiso>
# Author: sven@broeckling.de
# License: GPL

if [ $# -lt 2 ]; then
  echo "usage: $0 <dir> <target-iso>"
  echo "Takes a given KNOPPIX master dir and masters a bootable KNOPPIX"
  echo "iso. Use knx-extract.sh to get a complete master dir from a "
  echo "Downloaded KNOPPIX image."
  exit 1
fi

if [ `whoami` != root -o $UID -ne 0 ]; then
  echo "fatal: must be run as root - use sudo!"
  exit 1
fi

if [ ! -x /usr/bin/create_compressed_fs ]; then
  echo "cloop-utils has to be installed!"
  echo "Abort."
  exit 1
fi

kDIR="$1"
kISO="$2"
kCLIMG="/KNOPPIX/KNOPPIX"
kVOLID="Remastered Knoppix"
kPUBLISHER="Yet another Knoppix remasterer"

if [ ! -d "${kDIR}"/knxsource ]; then
  echo "The given master dir isn't a knoppix dir. It must contain"
  echo "two directories named knxsource and knxmaster"
  exit 2
fi

echo "$0 - inspired by http://www.ping.de/aktiv/live-cd/anleitung.html"
echo "Takes a given KNOPPIX master dir and masters a bootable KNOPPIX"
echo "iso. Use knx-extract.sh to get a complete master dir from a "
echo "Downloaded KNOPPIX image."
echo "Press Enter to continue or Ctrl-C to abort"
read cnt

echo "Unmounting proc..."
umount -l "$kDIR/knxsource/KNOPPIX/proc" 2>/dev/null # no error if fail

echo "Creating compressed knoppix image (get another coffee!)..."
mkisofs -quiet -R -U -V "$kVOLID" -publisher "$kPUBLISHER" -hide-rr-moved -cache-inodes -no-bak -pad "$kDIR"/knxsource/KNOPPIX | nice -5 /usr/bin/create_compressed_fs - 65536 > "$kDIR"/knxmaster/KNOPPIX/KNOPPIX 2>/dev/null
# quiet?

echo "Updating md5/sha1 sums..."
cd "$kDIR/knxmaster"
rm -f KNOPPIX/md5sums
find -type f -not -name md5sums -not -name sha1sums -not -name boot.cat -exec md5sum {} \; >> KNOPPIX/md5sums
rm -f KNOPPIX/sha1sums
find -type f -not -name md5sums -not -name sha1sums -not -name boot.cat -exec sha1sum {} \; >> KNOPPIX/sha1sums

cd - #popt?

echo "Creating iso image..."
mkisofs -quiet -pad -l -r -J -v -V "$kVOLID" -b boot/isolinux/isolinux.bin -c boot/isolinux/isolinux.cat -hide-rr-moved -no-emul-boot -boot-load-size 4 -boot-info-table -o "$kISO" "$kDIR"/knxmaster

echo "Done..."

