#!/bin/sh
# Must be root to run this
set -e
DEBVERSION=sid
PRISTINE=/var/local/cache/pristine/$DEBVERSION
if [ ! -e $PRISTINE ] ; then
echo "Debian pristine image not found, try running create-source-image"
echo $PRISTINE
exit -1
fi
TOPDIR=`pwd`
mkdir -p $TOPDIR/build/env
BACKING=$TOPDIR/build/env/backing
MOUNT=$TOPDIR/build/env/mount
if [ -e $MOUNT ] ; then
echo "Removing old filesystem"
# FIXME: evil hack!
chroot $MOUNT /etc/init.d/ssh stop || true
umount $MOUNT/proc || true
umount $MOUNT || true
rm -rf $MOUNT
fi
if [ -e $BACKING ] ; then
echo "Copying deb files into cache"
cp $BACKING/var/cache/apt/archives/* build/aptcache/$DEBVERSION || true
echo "Deleting old filesystem backing store"
rm -rf $BACKING
fi
mkdir $BACKING $MOUNT
touch $BACKING/please-trash-this-system
mkdir -p $BACKING/etc
echo "pristine" > $BACKING/etc/debian_chroot
cp -v policy-rc.d $BACKING/etc/policy-rc.d
mkdir -p $BACKING/var/cache/apt/archives
echo "Copying deb files out of cache"
cp build/aptcache/$DEBVERSION/* $BACKING/var/cache/apt/archives || true
cp -av action $BACKING
#hg -R ../mercurial-server archive -r default $BACKING/action/mercurial-server
( cd ../mercurial-server && \
hg st -mac0n | cpio -p -0 -d $BACKING/action/mercurial-server )
#cp ../mercurial-server/build/debian/mercurial-server_1.0-1_all.deb $BACKING/action
unionfs-fuse -o cow -o allow_other,suid,dev $BACKING=RW:$PRISTINE=RO $MOUNT
#mount --bind /dev "$MOUNT/dev"
#mount --bind /dev/pts "$MOUNT/dev/pts"
mount -t proc proc $MOUNT/proc
chroot $MOUNT ./action/go
#cat $BACKING/action/results
#rm -rf build/env