#!/bin/bash
test -n "$1" -a -n "$2" || echo -e 'SYNTAX: '$0' <-t|-p> <protocol_filename.xml>\nWARNING must run with data as WorkingDir\n\n' 
test -n "$1" -a -n "$2" || exit 1
TYPE=$1
PROTOCOL=$2
test -x "./autodate.pl" || exit 1
test -d "../protocols" || exit 1
echo d
test -e ./Makefile || exit 1
echo c
if [ -e /var/www/telematikhtu-backup/autogenerated/$PROTOCOL ]; then
	if [ -e ../protocols/$PROTOCOL ]; then
		if [ -z "$(diff --brief /var/www/telematikhtu-backup/autogenerated/$PROTOCOL ../protocols/$PROTOCOL)" ]; then
			echo "No changes since protocol has last been added"
			exit 0
		fi
	fi
	cp -vi /var/www/telematikhtu-backup/autogenerated/$PROTOCOL ../protocols/ || exit 1
else
	if [ -e ../protocols/$PROTOCOL ]; then
		read -p "Overwrite existing $PROTOCOL [y/N]? " CpAnswer
		if [ "$CpAnswer" == "y" ]; then
			scp engelbart.htu.tugraz.at:/var/www/telematikhtu-backup/autogenerated/$PROTOCOL ../protocols/ || exit 1
		fi
	else
		scp engelbart.htu.tugraz.at:/var/www/telematikhtu-backup/autogenerated/$PROTOCOL ../protocols/ || exit 1
	fi
fi
test -e "../protocols/$PROTOCOL" || exit 1
cvs add ../protocols/$PROTOCOL
./autodate.pl $TYPE $PROTOCOL 
echo Protokoll $PROTOCOL added sucessfully

make || exit 1 

(cd ../; cvs commit) || exit 1

read -p "Upload to HTU and EyeCatch now ? [y/N]" UpAnswer
if [ "$UpAnswer" = "y" ] ; then
	if [ "$USER" = "bernhard" ]; then
		make rsyncintern-xro
	else
		make rsyncintern
	fi
	make upload
fi
echo Protokoll $PROTOCOL uploaded sucessfully
exit 0
