#!/bin/sh
# Chroot in SliTaz to hack.
#
. /lib/libtaz.sh

if [ -z "$1" ]; then
	echo "bitte zu bearbeitende Datei angeben..."
	exit
fi

QUELLE=`pwd`
ROOTDIR="/tmp/$1"
FILENAME=`echo $1 | cut -d. -f1`
echo "FILENAME= $FILENAME"
EXTENSION=`echo $1 | cut -d. -f2`

if [ -d "$ROOTDIR" ]; then
	echo "$ROOTDIR existiert bereits. In bisheriger Version weiterarbeiten ?"
	read ANTWORT
fi

if [ "$ANTWORT" != "j" ] || [ ! -d "$ROOTDIR" ]; then
	if [ -d "$ROOTDIR" ]; then
		echo "lösche alte Version..."
		rm -R "$ROOTDIR"
		status
	fi

	echo "erzeuge Verzeichnis neu..."
	mkdir -p "$ROOTDIR"
	status

	echo "Kopiere $1 von $QUELLE nach $ROOTDIR..."
	cp "$QUELLE/$1" "$ROOTDIR/"
	status

	echo "Packe image aus..."
	cd "$ROOTDIR"

	if [ "$EXTENSION" = "lzma" ]; then
		lzma -d -si $1
		cat "$FILENAME" | cpio -id
		status
		echo "loesche image..."
		rm "$ROOTDIR/$FILENAME"
		status
	else
		( zcat "$1" 2>/dev/null || gunzip d "$1" -so 2>/dev/null || cat "$1" ) | cpio -id
		status
		echo "loesche image..."
		rm "$ROOTDIR/$1"
		status
	fi
fi

cd "$ROOTDIR"
echo "mounte filesysteme ..."
mount -t proc proc "$ROOTDIR/proc"
mount -t sysfs sysfs "$ROOTDIR/sys"
mount -t devpts devpts "$ROOTDIR/dev/pts"
mount -t tmpfs shm "$ROOTDIR/dev/shm"
status

echo "Chrooting in $ROOTDIR, ggf. fuer Netzwerk 'udhcpc -i wlan0' eingeben..."
chroot "$ROOTDIR" /bin/sh --login


echo "kehre zurück und hänge Verzeichnisse wieder aus..."
umount "$ROOTDIR/dev/shm"
umount "$ROOTDIR/dev/pts"
umount "$ROOTDIR/sys"
umount "$ROOTDIR/proc"
status

echo "Räume auf... "

if [ -n "`ls """$ROOTDIR/var/cache/tazpkg/"""`" ]; then
	echo "Leere Paketcache..."
	cd "$ROOTDIR/var/cache/tazpkg/"
	rm -R *
	status
	cd "$ROOTDIR";
fi

if [ -e "$ROOTDIR/var/log/*" ]; then
	echo "Lösche Logdateien..."
	rm "$ROOTDIR/var/log/*"
	status;
fi

if [ -e "$ROOTDIR/lib/modules/*" ]; then
	echo "Lösche Kernelmodule..."
	rm -R "$ROOTDIR/lib/modules/*"
	status;
fi

if [ -e "$ROOTDIR/var/log/slitaz/*" ]; then
	rm "$ROOTDIR/var/log/slitaz/*"
	status;
fi

if [ -e "$ROOTDIR/tmp/*" ]; then
	echo "Lösche temporäre Dateien..."
	rm -R "$ROOTDIR/tmp/*"
	status;
fi

echo "Leere Befehlshistorie..."
echo > "$ROOTDIR/root/.ash_history"
status

cd "$ROOTDIR/var/lib/tazpkg"
if [ -n "`ls | grep -v -e installed.*$ -e mirror$`" ]; then
	echo "Lösche Paketdateien..."
	rm $(ls | grep -v -e installed.*$ -e mirror$)
	status;
fi


cd "$ROOTDIR"
echo "Verlasse $ROOTDIR chroot Umgebung... "
echo "packe rootfs im Wurzelverzeichnis neu ein..."

		find bin etc init sbin var dev lib lib64 root usr >/tmp/list

		for dir in home proc run sys tmp mnt media
		do
			echo $dir >>/tmp/list
		done
		
		if [ -d "home/slitaz" ]; then
			for dir in home/slitaz home/slitaz/cache home/slitaz/log home/slitaz/src home/slitaz/xml home/slitaz/packages
			do
				echo $dir >>/tmp/list
			done
		fi
		
		sed -i '/^\/var\/run\/.*pid$/d' /tmp/list

		echo "Creating rootfs.gz with lzma compression... "
		cat /tmp/list | cpio -o -H newc | lzma e -mt8 -si -so > /$1
		#~ cat /tmp/list | cpio -o -H newc | pxz --format=lzma -9 -v > /$1

dd if=/dev/zero bs=1 count=$(((4 - ($(stat -c %s /$1) % 4)) % 4)) >> /$1

echo "... fertig !!!"
