#!/bin/sh

if [ -z "$TMPDIR" ]; then
	TMPDIR="/data/space/build"
fi

if [ -z "OUTPUT" ]; then
	OUTPUT="/data/space/packages"
fi

if [ -z "LOGFILE" ]; then
	LOGFILE="/data/space/buildlog"
fi

if [ ! -d $TMPDIR ]; then
	echo "$TMPDIR is not a directory"
	exit 1
fi

if [ ! -d $OUTPUT ]; then
	echo "$OUTPUT is not a directory"
	exit 1
fi

echo "TMP: $TMPDIR"
echo "OUT: $OUTPUT"
echo "LOG: $LOGFILE"


DIR=`dirname $0`

if [ -f "$LOGFILE" ]; then
	echo "Erase old logfile"
fi

for pkg in `find $DIR -maxdepth 1 -type d`; do
	if [ -x "$pkg/build" ]; then
		echo -n "Build package $pkg ..."
		( cd $pkg && TMPDIR=$TMPDIR sh build && mv -f output/*.deb output/*.dsc $OUTPUT ) 2>>$LOGFILE 1>>$LOGFILE
		echo "build finish"
	fi

done
