#!/bin/bash
#modified bt4 customisation script
#published without any warranty but with best wishes
#usage: $0 [/path/to/filesystem.squashfs]

# constants
squashfs=${1:-filesystem.squashfs}

# functions
die() {
	echo "[!] $@"
	exit 1
}

warn(){
	echo "[?] $@"
}

clear
echo "##############################################################"
echo "[*] BackTrack 5 Final customisation script"
echo "[*] Setting up the build environment..."

services="inetutils-inetd tinyproxy iodined knockd openvpn atftpd ntop nstxd nstxcd apache2 sendmail atd dhcp3-server winbind miredo miredo-server pcscd wicd wacom cups bluetooth binfmt-support mysql"

mkdir -p extract-cd/casper
mkdir -p squashfs
mount -t squashfs -o loop $squashfs squashfs || die "mounting squashfs failed!"
mkdir -p edit
echo "[*] Copying over files, please wait ... "

cp -a squashfs/* edit/ || die "file copying failed!"

cp /etc/resolv.conf edit/etc/
cp /etc/hosts edit/etc/
cp /etc/fstab edit/etc/
cp /etc/mtab edit/etc/

mount --bind /dev/ edit/dev
mount -t proc /proc edit/proc

echo "##############################################################"
echo "[*] Entering livecd. "
echo "##############################################################"
echo "[*] Now you can modify the LiveCD. At minimum, we recommend :"
echo "[*] apt-get update && apt-get upgrade & apt-get clean"
echo "##############################################################"
echo "[*] If you are running a large update, you might need to stop"
echo "[*] services like crond, udev, cups, etc in the chroot"
echo "[*] before exiting your chroot environment."
echo "##############################################################"
echo "[*] Once you have finished your modifications, type \"exit\""
echo "##############################################################"

chroot edit

echo "[*] Exited the build environemnt, unmounting images."

rm -rf edit/etc/mtab
rm -rf edit/etc/fstab

umount edit/dev
umount edit/proc
umount squashfs
umount mnt

test -f extract-cd/casper/filesystem.manifest && chmod +w extract-cd/casper/filesystem.manifest

echo "[*] Building manifest"
chroot edit dpkg-query -W --showformat='${Package} ${Version}\n' > extract-cd/casper/filesystem.manifest || die "manifect building failed"

for service in $services;do
chroot edit update-rc.d -f $service remove || warn "Something went wrong when tried to disable service $service"
done

if [ ! -f extract-cd/casper/filesystem.manifest-desktop ]
then
echo "[*] Creating extract-cd/casper/filesystem.manifest-desktop"
cp -v extract-cd/casper/filesystem.manifest{,-desktop} || warn "Could not copy file!"
fi

REMOVE='ubiquity casper live-initramfs user-setup discover xresprobe os-prober libdebian-installer4'
for i in $REMOVE
do
sed -i "/${i}/d" extract-cd/casper/filesystem.manifest-desktop || warn "Could not disable installation of some packages. Maybe they are already disabled."
done

rm -rf extract-cd/casper/filesystem.squashfs
echo "[*] Building squashfs image..."

mksquashfs edit extract-cd/casper/filesystem.squashfs || die "squashfs building failed"

rm -f extract-cd/md5sum.txt

(cd extract-cd && find . -type f -print0 | xargs -0 md5sum > md5sum.txt) || die "md5sum building failed"

echo "[*] Updated files are in ./extract-cd/"
echo "[*] Replace them all on your USB-drive if you want to apply changes."
echo "##############################################################"