#!/bin/bash

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

if [ $# -lt 2 ]; then
  echo "usage: $0 <knoppix-iso> <dest-dir>"
  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

kISO="$1"
kDEST="$2"
kTEMP=`mktemp -d`
kCLOOP=`mktemp -d`
kCLIMG="/KNOPPIX/KNOPPIX"

echo "$0 - inspired by http://www.ping.de/aktiv/live-cd/anleitung.html"
echo "This script takes a knoppix iso image and copys the compressed"
echo "files to the destination directory. After running this script"
echo "you'll have a complete master hierarchy under $kDEST."
echo "Temp is $kTEMP, cloop Temp is $kCLOOP"
echo "Press Enter to continue or Ctrl-C to abort"
read cnt

if [ ! -f "$kISO" ]; then
  echo "fatal: iso not readable!"
  exit 1
fi

if [ ! -d "$kDEST" ]; then
  echo -n "Directory $kDEST doesn't exist. Create? (Y/n) "
  read a
  if [ X$a = "Xy" -o X$a = "XY" ]; then
    mkdir -p "$kDEST"
  else
    echo "Abort."
    exit 2
  fi
fi

echo "Mounting knoppix iso..."
mount -t iso9660 -o loop "$kISO" "$kTEMP"

echo "Mounting knoppix cloop image..."
rmmod -f cloop 2>/dev/null     # no error when not loaded
modprobe cloop file="${kTEMP}${kCLIMG}" && mount -r -t iso9660 /dev/cloop0 $kCLOOP

echo "Populating destination directory..."
mkdir -p "$kDEST/knxsource/KNOPPIX"
mkdir -p "$kDEST/knxmaster/KNOPPIX"

echo "Copying clooped filesystem to Destination (get a coffee!)..."
sleep 3
cp -a "$kCLOOP"/* "$kDEST/knxsource/KNOPPIX"

echo "Copying additional files..."
cp "$kTEMP"/* "$kDEST/knxmaster/"
cp -a "$kTEMP/boot" "$kDEST/knxmaster/"

echo "Unmounting images..."
umount -l "$kCLOOP"
umount -l "$kTEMP"

echo "Copying your resolv.conf ans mounting proc..."
cp -a /etc/resolv.conf "$kDEST/knxsource/KNOPPIX/etc/"
mount -t proc none "$kDEST/knxsource/KNOPPIX/proc"

echo ""
echo "Done."
echo "No you may chroot into the source director by typing"
echo "chroot $kDEST/knxsource/KNOPPIX"

cat << EOT
Once inside the cd environment, it's possible install/remove packages or 
ajust the configuration. Remembering what debian can do for your 
live-cd, maybe you want to do some things :

  - Test your internet connection
  - Upgrade the cd                     apt-get update && apt-get upgrade
  - Install packets                    apt-get install <packet>
  - Remove packets                     apt-get remove <packet>
  ! Remove unused libraries            deborphan | xargs dpkg -r --purge
  ! Show what will purged              dpkg -l | grep ^rc
EOT
echo '  ! purge packets                      COLUMNS=100 dpkg --purge `dpkg -l | grep ^rc | awk ''{ print $2 }''`'
cat<<EOT
  ! Remove downloaded packets          apt-get clean
  
  - Use Aptitude for a nice GUI        apt-get install aptitude && aptitude

If you're done, don't forget to run knx-master.sh <dir> <targetiso> to
umount proc and master the iso. 
EOT

