#!/bin/bash

FOLDER=$(mktemp -d)

cd /home/christoph/openanno/svn/trunk

svn update 2>${FOLDER}/svn_update_2 >${FOLDER}/svn_update_1

REVISION=$(svn info | grep 'Revision:' | awk '{print $2}')

OLDREVISION=$(cat /home/christoph/openanno/revision)

[ $REVISION = $OLDREVISION ] && exit

echo $REVISION > /home/christoph/openanno/revision

sed -i -e "s:svn${OLDREVISION}:svn${REVISION}:g" setup.py

python setup.py sdist 2>${FOLDER}/sdist_2 >${FOLDER}/sdist_1

TARBALL=$(ls dist)

#python setup.py bdist_wininst -d /srv/www/horizons/snapshots  2>${FOLDER}/bdist_w_2 >${FOLDER}/bdist_w_1

#python setup.py bdist_rpm -d /srv/www/horizons/snapshots/ 2>${FOLDER}/bdist_r_2 >${FOLDER}/bdist_r_1


cp dist/$TARBALL /srv/www/horizons/snapshots/

mv dist/$TARBALL /home/christoph/openanno

#mv dist/*  /srv/www/horizons/snapshots/

cd /home/christoph/openanno/git

git-import-orig -u 2009.0+svn$REVISION ../$TARBALL 2>${FOLDER}/git-i-o_2 >${FOLDER}/git-i-o_1

dpkg-buildpackage -uc -us -i  2>${FOLDER}/dpkg-bp_2 >${FOLDER}/dpkg-bp_1

git commit -m"changelogentry for new revision" debian/changelog 2>/dev/null >/dev/null

fakeroot debian/rules clean 2>${FOLDER}/clean_2 >${FOLDER}/clean_1

reprepro -b /srv/www/cnemesis/debs include sid ../*${REVISION}*.changes 2>${FOLDER}/reprepro_2 >${FOLDER}/reprepro_1

LOG="Building Unknown Horizons Revision $REVISION"
for i in svn_update sdist bdist_w bdist_r git-i-o dpkg-bp reprepro
do \
  LOG="${LOG}

=====${i}=====
$(cat ${FOLDER}/${i}_1 ${FOLDER}/${i}_2)"
done

echo $LOG > /home/christoph/openanno/building_revision_$REVISION

echo "From: christoph.auto@coders-nemesis.eu
To: christoph@coders-nemesis.eu
Subject: Building Unknown Horizons ${REVISION}

${LOG}" | /usr/sbin/sendmail '' 'christoph@coders-nemesis.eu'


