#!/bin/sh

if [ ! -d "$1" ] ; then
	echo "Usage: $0 root-dir tar-file [ tar-file [ ... ] ]"
	exit 1
fi

d=$1 ; shift

for x
do
	if ! [ -f "$x" ] ; then
		echo "No such package: $x!"
		continue
	fi
	echo "Installing package $x. Please wait ..."
	tar xIf $x -C $d --same-owner --same-permissions
done

if [ $d != / ] ; then
	echo "export CHROOT_GOAL=$d"  > $d/tmp/$$.tmp
	echo "cd / ; . /etc/profile" >> $d/tmp/$$.tmp
	echo 'for x in /etc/setup.d/* ; do $x update ; done' >> $d/tmp/$$.tmp
	echo "umount /dev /proc"     >> $d/tmp/$$.tmp
	cd $d ; chroot . bin/sh tmp/$$.tmp ; rm -f /tmp/$$.tmp
else
	cd / ; for x in /etc/setup.d/* ; do $x update ; done
fi

exit 0
