#!/bin/sh

## last update: 2011-03-24

##################################
### ---------- settings ----------
##################################

home_dir=/home/xuser/
upload_dir=/home/xuser/upload/
txtfiles_dir=/home/xuser/upload/txtfiles/
revs_32bit_8bit_depth_dir=/home/xuser/revs/32bit/8bit_depth/
revs_32bit_10bit_depth_dir=/home/xuser/revs/32bit/10bit_depth/

pthreads_dir=/home/xuser/pthreads/
gpac_dir=/home/xuser/gpac/
ffmpeg_dir=/home/xuser/libav/
ffmpegsource_dir=/home/xuser/ffmpegsource/

x264_32bit_8bit_depth_dir=/home/xuser/x264_32bit_8bit_depth/
x264_32bit_10bit_depth_dir=/home/xuser/x264_32bit_10bit_depth/

msys_local_include_dir=/f/msys/1.0/local/include/
msys_local_lib_dir=/f/msys/1.0/local/lib/

mingw_include_dir=/f/MinGW/include/
mingw_lib_dir=/f/MinGW/lib/

## compile x264, "yes" or "no"
use_x264="yes"

## update libraries for x264, "yes" or "no"
use_pthreads="yes"
use_gpac="yes"
use_ffmpeg_ffmpegsource="yes"

## use remote ftp, "yes" or "no"
use_remote_ftp="yes"

#date format YYYY-MM-DD hh:mm:ss
dateformat="%Y-%m-%d %T"

##################################
### ------ end of settings -------
##################################


	## checking for needed dirs and files

	if [ ! -d $home_dir ]; then mkdir -p $home_dir;
	fi

	if [ ! -d $txtfiles_dir ]; then mkdir -p $txtfiles_dir;
	fi

	if [ ! -d $revs_32bit_8bit_depth_dir ]; then mkdir -p $revs_32bit_8bit_depth_dir;
	fi

	if [ ! -d $revs_32bit_10bit_depth_dir ]; then mkdir -p $revs_32bit_10bit_depth_dir;
	fi

	if [ ! -f $txtfiles_dir/rev.txt ]; then echo "0" >$txtfiles_dir/rev.txt;
	fi

	if [ ! -d $x264_32bit_8bit_depth_dir ]; then cd $home_dir; git clone git://git.videolan.org/x264.git $x264_32bit_8bit_depth_dir;
	fi

	if [ ! -d $x264_32bit_10bit_depth_dir ]; then cd $home_dir; git clone git://git.videolan.org/x264.git $x264_32bit_10bit_depth_dir;
	fi


## update 32bit 10bit-dept version first
cd $x264_32bit_10bit_depth_dir;

## get last line from output `git pull`
check_32bit_10bit_depth=`git pull --rebase 2>&1`;

## change to x264 dir, working dir, start in first working dir (32bit 8bit_depth)
cd $x264_32bit_8bit_depth_dir;

## get last line from output `git pull`
check_32bit_8bit_depth=`git pull --rebase 2>&1`;


## get revision number
## mingw/msys wc creates a leading space, therefor sed -e 's/^ *//' is used
rev_u=`git rev-list HEAD | wc -l | sed -e 's/^ *//'`;
rev=`head -1 "$txtfiles_dir"/rev.txt`;

	## check if revision number does not match with contents of rev.txt
	if [ "$rev_u" != "$rev" ]; then

	## make revision dirs
	mkdir -p $revs_32bit_8bit_depth_dir/revision$rev_u
	mkdir -p $revs_32bit_10bit_depth_dir/revision$rev_u

	## create latest changelog
	git log | perl -e '$/="\ncommit"; $i=@t=<>; for(@t){s/\n/ r$i\n/m; $i--; print}' >$txtfiles_dir/changelog.txt;


##################################
### ---------- pthreads ----------
##################################

	## check if use_pthreads is "yes" or "no"
	if [ "$use_pthreads" = "yes" ]; then

		if [ ! -d $pthreads_dir ]; then cd $home_dir; cvs -d :pserver:anoncvs@sources.redhat.com:/cvs/pthreads-win32 checkout pthreads;

		cd $pthreads_dir;

		make clean GC-static;

			if [ -f libpthreadGC2.a ]; then
			rm -rf $msys_local_include_dir/pthread.h;
			rm -rf $msys_local_include_dir/sched.h;
			rm -rf $msys_local_include_dir/semaphore.h;
			rm -rf $msys_local_lib_dir/libpthreadGC2.a;

			rm -rf $mingw_include_dir/pthread.h;
			rm -rf $mingw_include_dir/sched.h;
			rm -rf $mingw_include_dir/semaphore.h;
			rm -rf $mingw_lib_dir/libpthreadGC2.a;

			cp -r pthread.h $msys_local_include_dir;
			cp -r sched.h $msys_local_include_dir;
			cp -r semaphore.h $msys_local_include_dir;
			cp -r libpthreadGC2.a $msys_local_lib_dir;

			cp -r pthread.h $mingw_include_dir;
			cp -r sched.h $mingw_include_dir;
			cp -r semaphore.h $mingw_include_dir;
			cp -r libpthreadGC2.a $mingw_lib_dir;

			datefinal=`date +"$dateformat"`;
			echo -e $datefinal CVS: new pthreads compile, manually check date!'\r' | tee -a $txtfiles_dir/eventlog.txt;
			fi
		fi

	cd $pthreads_dir;

	pthreadscheck=`cvs up 2>&1`;

		if echo $pthreadscheck | grep -q "[PU] "; then

		make clean GC-static;

			if [ -f libpthreadGC2.a ]; then
			rm -rf $msys_local_include_dir/pthread.h;
			rm -rf $msys_local_include_dir/sched.h;
			rm -rf $msys_local_include_dir/semaphore.h;
			rm -rf $msys_local_lib_dir/libpthreadGC2.a;

			rm -rf $mingw_include_dir/pthread.h;
			rm -rf $mingw_include_dir/sched.h;
			rm -rf $mingw_include_dir/semaphore.h;
			rm -rf $mingw_lib_dir/libpthreadGC2.a;

			cp -r pthread.h $msys_local_include_dir;
			cp -r sched.h $msys_local_include_dir;
			cp -r semaphore.h $msys_local_include_dir;
			cp -r libpthreadGC2.a $msys_local_lib_dir;

			cp -r pthread.h $mingw_include_dir;
			cp -r sched.h $mingw_include_dir;
			cp -r semaphore.h $mingw_include_dir;
			cp -r libpthreadGC2.a $mingw_lib_dir;

			datefinal=`date +"$dateformat"`;
			echo -e $datefinal CVS: new pthreads compile, manually check date!'\r' | tee -a $txtfiles_dir/eventlog.txt;
			fi
		fi
	fi

##################################
### ------ end of pthreads -------
##################################


##################################
### ------------ gpac ------------
##################################

	## check if use_gpac is "yes" or "no"
	if [ "$use_gpac" = "yes" ]; then

		if [ ! -d $gpac_dir ]; then cd $home_dir; svn co https://gpac.svn.sourceforge.net/svnroot/gpac/trunk/gpac gpac;
		fi

		if [ ! -f $txtfiles_dir/gpacrev32bit.txt ]; then echo "0" >$txtfiles_dir/gpacrev32bit.txt;
		fi

		cd $gpac_dir;

		gpaccheck=`svn up | tail -1`;

		if [ `echo $gpaccheck | awk '{print $1}'` = "At" ]; then
		gpacrev_u=`echo $gpaccheck | cut -c13-20 | tr -d .`
		fi

		if [ `echo $gpaccheck | awk '{print $1}'` = "Updated" ]; then
		gpacrev_u=`echo $gpaccheck | cut -c21-28 | tr -d .`
		fi

		gpacrev=`head -1 $txtfiles_dir/gpacrev32bit.txt`;

		## check if revision number does not match with contents of gpacrev32bit.txt
		if [ "$gpacrev_u" != "$gpacrev" ]; then

		configure;
		make clean;
		make lib;

			if [ -f bin/gcc/libgpac_static.a ]; then
			rm -rf $msys_local_include_dir/gpac;
			rm -rf $msys_local_lib_dir/libgpac_static.a;

			rm -rf $mingw_include_dir/gpac;
			rm -rf $mingw_lib_dir/libgpac_static.a;

			cp -r include/gpac/ $msys_local_include_dir;
			cp -r bin/gcc/libgpac_static.a $msys_local_lib_dir;

			cp -r include/gpac/ $mingw_include_dir;
			cp -r bin/gcc/libgpac_static.a $mingw_lib_dir;

			## store latest revision in gpacrev32bit.txt.
			echo -n "$gpacrev_u" >$txtfiles_dir/gpacrev32bit.txt;

			datefinal=`date +"$dateformat"`;
			echo -e $datefinal SVN: gpac revision $gpacrev_u'\r' | tee -a $txtfiles_dir/eventlog.txt;
			fi
		fi
	fi

##################################
### -------- end of gpac ---------
##################################


##################################
### ----------- ffmpeg -----------
##################################

	## check if use_ffmpeg_ffmpegsource is "yes" or "no"
	if [ "$use_ffmpeg_ffmpegsource" = "yes" ]; then

		if [ ! -d $ffmpeg_dir ]; then cd $home_dir; git clone git://git.libav.org/libav.git libav;
		fi

		if [ ! -f $txtfiles_dir/ffmpegrev32bit.txt ]; then echo "0" >$txtfiles_dir/ffmpegrev32bit.txt;
		fi

	cd $ffmpeg_dir;

	## get last line from output `git pull`
	ffmpegcheck=`git pull --rebase 2>&1`;

	## get revision number
	ffmpegrev_u=`git rev-list HEAD | wc -l | sed -e 's/^ *//'`;
	ffmpegrev=`head -1 $txtfiles_dir/ffmpegrev32bit.txt`;

		## check if revision number does not match with contents of ffmpegrev32bit.txt
		if [ "$ffmpegrev_u" != "$ffmpegrev" ]; then

		configure --enable-gpl --enable-postproc --enable-memalign-hack --cpu=i686 --disable-devices --disable-filters --enable-runtime-cpudetect --disable-encoders --disable-muxers --disable-network --disable-decoder=aac,ac3,adpcm_*,alac,als,ape,atrac?,cook,dca,dsicinaudio,dxa,eac3,flac,interplay_dpcm,mlp,mp1,mp2,mp3,mp3*,mpc?,pcm_*,qcelp,ra_*,sipr,truehd,truespeech,tta,vorbis,wavpack,wma*,twinvq --disable-demuxer=aac,ac3,pcm_*,ape,amr,ass,au,avs,dts,eac3,flac,mp3,mpc,mpc8,truehd,tta,w64,wav,wv --disable-parser=aac,ac3,dca,mlp,mpegaudio --extra-cflags=-U__STRICT_ANSI__ --disable-pthreads;
		make clean;
		make;

			if [ -f libavcodec/libavcodec.a -a -f libavdevice/libavdevice.a -a -f libavfilter/libavfilter.a -a -f libavformat/libavformat.a -a -f libavutil/libavutil.a -a -f libpostproc/libpostproc.a -a -f libswscale/libswscale.a ]; then
			rm -rf $msys_local_include_dir/libavcodec;
			rm -rf $msys_local_include_dir/libavdevice;
			rm -rf $msys_local_include_dir/libavfilter;
			rm -rf $msys_local_include_dir/libavformat;
			rm -rf $msys_local_include_dir/libavutil;
			rm -rf $msys_local_include_dir/libpostproc;
			rm -rf $msys_local_include_dir/libswscale;
			rm -rf $msys_local_lib_dir/libavcodec.a;
			rm -rf $msys_local_lib_dir/libavdevice.a;
			rm -rf $msys_local_lib_dir/libavfilter.a;
			rm -rf $msys_local_lib_dir/libavformat.a;
			rm -rf $msys_local_lib_dir/libavutil.a;
			rm -rf $msys_local_lib_dir/libpostproc.a;
			rm -rf $msys_local_lib_dir/libswscale.a;

			rm -rf $mingw_include_dir/libavcodec;
			rm -rf $mingw_include_dir/libavdevice;
			rm -rf $mingw_include_dir/libavfilter;
			rm -rf $mingw_include_dir/libavformat;
			rm -rf $mingw_include_dir/libavutil;
			rm -rf $mingw_include_dir/libpostproc;
			rm -rf $mingw_include_dir/libswscale;
			rm -rf $mingw_lib_dir/libavcodec.a;
			rm -rf $mingw_lib_dir/libavdevice.a;
			rm -rf $mingw_lib_dir/libavfilter.a;
			rm -rf $mingw_lib_dir/libavformat.a;
			rm -rf $mingw_lib_dir/libavutil.a;
			rm -rf $mingw_lib_dir/libpostproc.a;
			rm -rf $mingw_lib_dir/libswscale.a;

			## make install, installs to the local dir, mingw is weird, so also copy to mingw dir.
			## too much work to manually copy the needed .h files.
			make install;

			cp -r $msys_local_include_dir/libavcodec $mingw_include_dir;
			cp -r $msys_local_include_dir/libavdevice $mingw_include_dir;
			cp -r $msys_local_include_dir/libavfilter $mingw_include_dir;
			cp -r $msys_local_include_dir/libavformat $mingw_include_dir;
			cp -r $msys_local_include_dir/libavutil $mingw_include_dir;
			cp -r $msys_local_include_dir/libpostproc $mingw_include_dir;
			cp -r $msys_local_include_dir/libswscale $mingw_include_dir;
			cp -r $msys_local_lib_dir/libavcodec.a $mingw_lib_dir;
			cp -r $msys_local_lib_dir/libavdevice.a $mingw_lib_dir;
			cp -r $msys_local_lib_dir/libavfilter.a $mingw_lib_dir;
			cp -r $msys_local_lib_dir/libavformat.a $mingw_lib_dir;
			cp -r $msys_local_lib_dir/libavutil.a $mingw_lib_dir;
			cp -r $msys_local_lib_dir/libpostproc.a $mingw_lib_dir;
			cp -r $msys_local_lib_dir/libswscale.a $mingw_lib_dir;

			## store latest revision in ffmpegrev32bit.txt.
			echo -n "$ffmpegrev_u" >$txtfiles_dir/ffmpegrev32bit.txt;

			ffmpegupdated="yes"

			datefinal=`date +"$dateformat"`;
			echo -e $datefinal GIT: libav revision $ffmpegrev_u'\r' | tee -a $txtfiles_dir/eventlog.txt;
			fi

##################################
### ------- end of ffmpeg --------
##################################
#
# ffmpegsource will always update when ffmpeg is updated
#
##################################
### -------- ffmpegsource --------
##################################

		 	if [ ! -d $ffmpegsource_dir ]; then cd $home_dir; svn checkout http://ffmpegsource.googlecode.com/svn/trunk/ ffmpegsource;
			fi

			if [ ! -f $txtfiles_dir/ffmpegsourcerev32bit.txt ]; then echo "0" >$txtfiles_dir/ffmpegsourcerev32bit.txt;
			fi

			cd $ffmpegsource_dir;

			ffmpegsourcecheck=`svn up | tail -1`;

			if [ `echo $ffmpegsourcecheck | awk '{print $1}'` = "At" ]; then
			ffmpegsourcerev_u=`echo $ffmpegsourcecheck | cut -c13-20 | tr -d .`
			fi

			if [ `echo $ffmpegsourcecheck | awk '{print $1}'` = "Updated" ]; then
			ffmpegsourcerev_u=`echo $ffmpegsourcecheck | cut -c21-28 | tr -d .`
			fi

			ffmpegsourcerev=`head -1 $txtfiles_dir/ffmpegsourcerev32bit.txt`;

			## check if revision number does not match with contents of ffmpegsourcerev32bit.txt OR if ffmpeg is updated
			if [ "$ffmpegsourcerev_u" != "$ffmpegsourcerev" ] || [ "$ffmpegupdated" = "yes" ]; then

			configure;
			make clean;
			make;

				if [ -f src/core/.libs/libFFMS2.a ]; then
				rm -rf $msys_local_include_dir/ffms.h;
				rm -rf $msys_local_lib_dir/libffms2.la;
				rm -rf $msys_local_lib_dir/libFFMS2.a;

				rm -rf $mingw_include_dir/ffms.h;
				rm -rf $mingw_lib_dir/libffms2.la;
				rm -rf $mingw_lib_dir/libFFMS2.a;

				cp -r include/ffms.h $msys_local_include_dir;
				cp -r src/core/.libs/libffms2.la $msys_local_lib_dir;
				cp -r src/core/.libs/libFFMS2.a $msys_local_lib_dir;

				cp -r include/ffms.h $mingw_include_dir;
				cp -r src/core/.libs/libffms2.la $mingw_lib_dir;
				cp -r src/core/.libs/libFFMS2.a $mingw_lib_dir;

				## store latest revision in ffmpegsourcerev32bit.txt.
				echo -n "$ffmpegsourcerev_u" >$txtfiles_dir/ffmpegsourcerev32bit.txt;

				datefinal=`date +"$dateformat"`;
				echo -e $datefinal SVN: ffmpegsource revision $ffmpegsourcerev_u'\r' | tee -a $txtfiles_dir/eventlog.txt;
				fi
			fi
		fi
	fi

##################################
### ---- end of ffmpegsource -----
##################################


##################################
### ------------ x264 ------------
##################################

	## check if use_x264 is "yes" or "no"
	if [ "$use_x264" = "yes" ]; then


	## compile latest x264 revision, 32bit 8bit_depth output
	cd $x264_32bit_8bit_depth_dir;

	## rerun configure (needed to be sure of version using git and not to set --bit-depth=8)
	configure --bit-depth=8 --enable-strip;

	## cleanup files from previous compile
	make clean;

	## compile x264.exe
	make fprofiled VIDS="../1280x720p50_fprofiled.yuv";

		## check if x264.exe is created, else stop the script.
		if [ ! -f x264.exe ]; then 

		datefinal=`date +"$dateformat"`;
		echo -e $datefinal GIT: x264 32bit 8bit_depth output revision $rev_u failed to compile'\r' | tee -a $txtfiles_dir/eventlog.txt;

		## store latest revision in rev.txt.
		echo -n "$rev_u" >$txtfiles_dir/rev.txt;
		## stop the script
		exit 0;
		fi

	## create md5 checksum for x264.exe and copy to revision dir
        x264_32bit_8bit_depth_md5=`md5sum x264.exe | awk '{print $1}'`;
	echo -n $x264_32bit_8bit_depth_md5 >$revs_32bit_8bit_depth_dir/revision$rev_u/x264.md5;
	cp x264.exe $revs_32bit_8bit_depth_dir/revision$rev_u/;

	datefinal=`date +"$dateformat"`;
	echo -e $datefinal GIT: x264 32bit 8bit_depth output revision $rev_u md5: $x264_32bit_8bit_depth_md5'\r' | tee -a $txtfiles_dir/eventlog.txt;


	## compile latest x264 revision, 32bit 10bit_depth output
	cd $x264_32bit_10bit_depth_dir;

	## rerun configure (needed to be sure of version using git, and not to set --bit-depth=10)
	configure --bit-depth=10 --enable-strip;

	## cleanup files from previous compile
	make clean;

	## compile x264.exe
	make fprofiled VIDS="../1280x720p50_fprofiled.yuv";


		## check if x264.exe is created, else stop the script.
		if [ ! -f x264.exe ]; then 

		datefinal=`date +"$dateformat"`;
		echo -e $datefinal GIT: x264 32bit 10bit_depth output revision $rev_u failed to compile'\r' | tee -a $txtfiles_dir/eventlog.txt;

		## store latest revision in rev.txt.
		echo -n "$rev_u" >$txtfiles_dir/rev.txt;
		## stop the script
		exit 0;
		fi

	## create md5 checksum for x264.exe and copy to revision dir
        x264_32bit_10bit_depth_md5=`md5sum x264.exe | awk '{print $1}'`;
	echo -n $x264_32bit_10bit_depth_md5 >$revs_32bit_10bit_depth_dir/revision$rev_u/x264.md5;
	cp x264.exe $revs_32bit_10bit_depth_dir/revision$rev_u/;

	datefinal=`date +"$dateformat"`;
	echo -e $datefinal GIT: x264 32bit 10bit_depth output revision $rev_u md5: $x264_32bit_10bit_depth_md5'\r' | tee -a $txtfiles_dir/eventlog.txt;
	fi

##################################
### -------- end of x264 ---------
##################################

	## store latest revision in rev.txt.
	echo -n "$rev_u" >$txtfiles_dir/rev.txt;

		## upload to remote ftp
		## check if use_remote_ftp is "yes" or "no"
		if [ "$use_remote_ftp" = "yes" ]; then
		cd $upload_dir;
		upload_update_32bit_8bit_depth.sh $rev_u;
		upload_update_32bit_10bit_depth.sh $rev_u;
		upload_http_changelog_txt.sh;
		upload_http_eventlog_txt.sh;
		upload_http_date_txt.sh;
		upload_http_rev_32bit_txt.sh;

		fi

	datefinal=`date +"$dateformat"`;
	echo $datefinal COMPILE: the script has finished, current x264 32bit revision: $rev_u;

	## stop the script
	exit 0;
	fi

	## check if revision number does match with contents of rev.txt
	if [ "$rev_u" = "$rev" ]; then

	datefinal=`date +"$dateformat"`;
	echo $datefinal COMPILE: no new updates, current 32bit revision: $rev_u;
	## stop the script
	exit 0;
	fi
