target_root=$1

DEVKIT_DIR=$(dirname $(dirname `readlink -f "$0"`))
DEVKIT_NAME='debian-squeeze'

setup_account_file()
{
	local target=$target_root/etc/$1
	local master=$DEVKIT_DIR/$DEVKIT_NAME/share/base-passwd/$1.master

	if [ ! -e $target ]; then
		touch $target
	fi

	for i in $(seq $(cat $master | wc -l)); do
		local line=$(head -n $i $master | tail -n 1)
		local name=$(echo $line | cut -d: -f1)

		if ! grep ^$name $target > /dev/null ; then
			echo "$line" >> $target
		fi
	done
}

mkdir -p $target_root/etc

setup_account_file "passwd"
setup_account_file "group"

mkdir -p $target_root/var/tmp

mkdir -p $target_root/var/lib/dpkg/updates
mkdir -p $target_root/var/lib/dpkg/info
touch $target_root/var/lib/dpkg/lock
touch $target_root/var/lib/dpkg/status
touch $target_root/var/lib/dpkg/available
touch $target_root/var/lib/dpkg/diversions

mkdir -p $target_root/var/lib/apt/lists/partial
mkdir -p $target_root/var/cache/apt/archives/partial
touch $target_root/var/lib/apt/lists/lock
touch $target_root/var/cache/apt/archives/lock

mkdir -p $target_root/etc/apt/apt.conf.d
mkdir -p $target_root/etc/apt/preferences.d
if [ ! -e $target_root/etc/apt/sources.list ]; then
	cp $DEVKIT_DIR/$DEVKIT_NAME/etc/apt/sources.list $target_root/etc/apt/
fi


mkdir -p $target_root/etc/rcS.d
for i in $(seq 0 1 6) ; do
	mkdir -p $target_root/etc/rc$i.d
done

mkdir -p $target_root/var/log
mkdir -p $target_root/var/backups

mkdir -p $target_root/etc/alternatives
mkdir -p $target_root/var/lib/dpkg/alternatives

# work-arounds for something needed from the target

mkdir -p $target_root/usr/share

# autotools-dev

mkdir -p $target_root/usr/share/misc
cp $DEVKIT_DIR/$DEVKIT_NAME/share/misc/config.guess \
	$target_root/usr/share/misc/
cp $DEVKIT_DIR/$DEVKIT_NAME/share/misc/config.sub \
	$target_root/usr/share/misc/

# build-essential

cp -r $DEVKIT_DIR/$DEVKIT_NAME/share/build-essential $target_root/usr/share/

