#!/bin/bash -x

. ~/.bash_profile

set -e

PATH=/usr/local/bin:$PATH
QF=~/src/qf
MINGW=~/src/mingw/mingw-cross-env-2.18/usr/i686-pc-mingw32
WEBDEST=~/public_html/qf-files
export QFSMP=-j4

cd $QF/quakeforge
git pull
./bootstrap
#begin linux
	cd linux.o
	rm -f quakeforge-*.tar.gz
	../configure -C --without-clients --with-servers=master
	make $QFSMP
	make install
	#begin rpm
		cd RPM
		make rpm
		cd ..
	#end rpm
	#begin deb
		DIR=`basename quakeforge-*.tar.gz .tar.gz`
		cd debian
		rm -rf $DIR
		tar zxvf ../${DIR}.tar.gz
		cd $DIR
		dpkg-buildpackage -rfakeroot -us -uc
		cd ..
		#rm -rf $DIR
		cd ..
	#end deb
	cd ..
#end linux
#begin win32
	cd qf-win32.o
	qf_win32=`pwd`/qf-win32
	rm -rf qf-win32 qf-win32*.zip
	cp ../tools/cross/cross-configure.sh .
	./cross-configure.sh --disable-debug --disable-shared --program-prefix= 
	../tools/cross/cross-make.sh $QFSMP
	../tools/cross/cross-make.sh \
		prefix=${qf_win32} \
		exec_prefix=${qf_win32} \
		pkgdatadir=${qf_win32} \
		man1dir=${qf_win32_dir} \
		install
	$MINGW/bin/strip --strip-unneeded qf-win32/bin/*.exe
	python ../tools/build_scripts/qf-win32.py
	for p in {client-{sdl,sdl32,sgl,wgl},devel,server,tools}; do
		cp qf-win32-$p.zip $WEBDEST/qf-win32-$p.zip-
		mv $WEBDEST/qf-win32-$p.zip- $WEBDEST/qf-win32-$p.zip
	done
	touch $WEBDEST/qf-win32-*.zip
	#zip -r9 qf-win32.zip qf-win32
	#cp qf-win32.zip /project/website/htdocs/files/qf-win32.zip-
	#mv /project/website/htdocs/files/qf-win32.zip- /project/website/htdocs/files/qf-win32.zip
	cd ..
#end win32
cd $QF/game-source
git pull
for f in `find . -name 'Makefile'`; do make -C `dirname $f` clean all; done
cd ..
zip -r9 game-source.zip game-source
tar zcf game-source.tar.gz game-source
tar jcf game-source.tar.bz2 game-source
if cmp game-source.zip $WEBDEST/game-source.zip; then
	echo no change
else
	cp game-source.* $WEBDEST
fi
#cd $QF/quakeforge/linux.o/doc
#make doc
