#!/bin/bash

if test $# = 0; then
    echo "RepBranches: You have to specify the target list in
	the format \"{package} {EN}{ForWork}{ForLTS}{ForLTSServ}{OmitSourceTree}\"
oscadaRepBranches \"SrcTar 1110\" \"SrcRPM 1010\" \"Debian_10_32 1000\" ...
 Environments:
  OSCADA_SVNServ - SubVersion server of the OpenSCADA project, \"oscada.org\" by default;
  OSCADA_Check   - sign of the checking mode, that is building only the last SVN revision whether for the Work or LTS branch,
    the not checking mode - force by default."
    exit -1
fi

program=openscada
prefix=/data/oscada
BinDir=$prefix/bin
BldDir=$prefix/build
BldLock=$BldDir/lock
build=$BinDir/oscadaBuild
ResulsDir=/data/share_res/ftp

test -f $BldLock && kill -0 "$(cat $BldLock)" 2> /dev/null && echo "Building is going now!" && exit 0;
echo $$ > $BldLock

if test "x$OSCADA_SVNServ" = "x"; then OSCADA_SVNServ=oscada.org; fi
if test "x$OSCADA_Check" != "x1"; then OSCADA_Check=0; fi

export LANG=en_US.UTF-8

logFile="/data/oscada/build/log/$(date +%FT%T).log"
exec > >(tee $logFile) 2>&1

#Moving the command line targets to TargetsList
iTarg=0
while test $# -gt 0; do
    TargetsList[iTarg]=$1
    shift
    iTarg=$(($iTarg + 1))
done
echo "RepBranches: Building the targets - ${TargetsList[*]}"

#********************************************************
#* Checking for the Work branch				*
#********************************************************
nBuild=0

SrcWorkVer=`svn info svn://$OSCADA_SVNServ/trunk/OpenSCADA | sed -n "/Last Changed Rev:[ ]*\([^ ]*\).*/s//\1/p"`
SrcWorkSt=`svn log svn://$OSCADA_SVNServ/trunk/OpenSCADA -l1 | sed -n "4p" | sed -n "/[^ ]* [^ ]* (\(.*\))/s//\1/p"`
if test "x$SrcWorkVer" != "x" && test "x$SrcWorkSt" = "xstable" -o $OSCADA_Check = 0; then
    echo "RepBranches: Last revision $SrcWorkVer of the Work branch."
    SrcWorkDir=$BldDir/${program}_1
    # Processing the target list
    iTarg=0
    while test "x${TargetsList[iTarg]}" != "x"; do
	targ=`echo ${TargetsList[iTarg]} | sed -n "/^\([^ ]*\).*$/s//\1/p"`
	en=`echo ${TargetsList[iTarg]} | sed -n "/^[^ ]* \(.\).*$/s//\1/p"`
	enWork=`echo ${TargetsList[iTarg]} | sed -n "/^[^ ]* .\(.\).*$/s//\1/p"`
	omitSrc=`echo ${TargetsList[iTarg]} | sed -n "/[^ ]* ....\(.\).*$/s//\1/p"`
	BldTargStamp=$BldDir/${targ}_Work.stamp
	#if test ! -f $BldTargStamp; then echo "0" > $BldTargStamp; fi
	if test $en = 1 -a $enWork = 1 && test $OSCADA_Check = 0 -o "$(cat $BldTargStamp 2> /dev/null)" != $SrcWorkVer; then
	    echo "RepBranches: Performing the Work $SrcWorkVer building for $targ."
	    if test ! -d $SrcWorkDir -a "x$omitSrc" != "x1"; then
		svn co svn://$OSCADA_SVNServ/trunk/OpenSCADA $SrcWorkDir
		if test $? = 0; then echo "RepBranches: The sources '$SrcWorkDir' from 'svn://$OSCADA_SVNServ/trunk/OpenSCADA' successfully downloaded for building."
		else echo "RepBranches: Error $? of obtaining the sources '$SrcWorkDir' from 'svn://$OSCADA_SVNServ/trunk/OpenSCADA'."; exit $?; fi
	    fi
	    export OSCADA_OptTarget=`echo $targ |sed -n "/.*:\(.*\)/s//\1/p"`
	    export OSCADA_ResTarget=$ResulsDir/OpenSCADA/Work
	    isRep=`echo $targ | sed -n "/.*\(rep\).*/s//\1/p"`
	    if test -n "$isRep"; then OSCADA_ResTarget=$ResulsDir; fi
	    $build $SrcWorkDir 1+r$SrcWorkVer `echo $targ | sed -n "/^\([^:]*\).*$/s//\1/p"`
	    if test $? = 0; then echo $SrcWorkVer > $BldTargStamp;
	    else echo "RepBranches: Error $? of building Work $SrcWorkVer for $targ."; fi

	    nBuild=$(($nBuild + 1))
	fi
	iTarg=$(($iTarg + 1))
    done

    if test -d $SrcWorkDir; then rm -R $SrcWorkDir; fi
fi

#********************************************************
#* Current LTS version checking				*
#********************************************************
CurLTSSVNTag=openscada_0.9
SrcLTSRev=`svn info svn://$OSCADA_SVNServ/tags/$CurLTSSVNTag | sed -n "/Last Changed Rev:[ ]*\([^ ]*\).*/s//\1/p"`
if test "x$SrcLTSRev" != "x"; then
    SrcLTSVer=`svn log svn://$OSCADA_SVNServ/tags/$CurLTSSVNTag@$SrcLTSRev -l1 | sed -n "4p" | sed -n "/[^ ]* [^ ]* \(.*\)/s//\1/p"`
    if test "x$SrcLTSVer" != "x"; then
	SrcLTSDir=$BldDir/${program}_$SrcLTSVer
	LTSVerServ=`echo $SrcLTSVer | sed -n "/[^.]*.[^.]*.[^.]*.\(.*\)/s//\1/p"`
	echo "RepBranches: Last revision $SrcLTSRev of the LTS branch $SrcLTSVer."
	# Processing the target list
	iTarg=0
	while test "x${TargetsList[iTarg]}" != "x"; do
	    targ=`echo ${TargetsList[iTarg]} | sed -n "/^\([^ ]*\).*$/s//\1/p"`
	    en=`echo ${TargetsList[iTarg]} | sed -n "/^[^ ]* \(.\).*$/s//\1/p"`
	    enLTS=`echo ${TargetsList[iTarg]} | sed -n "/^[^ ]* ..\(.\).*$/s//\1/p"`
	    enLTSServ=`echo ${TargetsList[iTarg]} | sed -n "/[^ ]* ...\(.\).*$/s//\1/p"`
	    omitSrc=`echo ${TargetsList[iTarg]} | sed -n "/[^ ]* ....\(.\).*$/s//\1/p"`
	    BldTargStamp=$BldDir/${targ}_LTS.stamp
	    #if test ! -f $BldTargStamp; then echo "0" > $BldTargStamp; fi
	    if test $en = 1 -a $enLTS = 1 && test "x$LTSVerServ" = "x" -o $enLTSServ = 1 && test $OSCADA_Check = 0 -o "$(cat $BldTargStamp 2> /dev/null)" != $SrcLTSVer; then
		echo "RepBranches: Performing the LTS $SrcLTSVer building for $targ."
		if test ! -d $SrcLTSDir -a "x$omitSrc" != "x1"; then
		    svn co svn://$OSCADA_SVNServ/tags/$CurLTSSVNTag $SrcLTSDir
		    if test $? = 0; then echo "RepBranches: The sources '$SrcLTSDir' from 'svn://$OSCADA_SVNServ/tags/$CurLTSSVNTag' successfully downloaded for building."
		    else echo "RepBranches: Error $? of obtaining the sources '$SrcLTSDir' from 'svn://$OSCADA_SVNServ/tags/$CurLTSSVNTag'."; exit $?; fi
		fi
		export OSCADA_OptTarget=`echo $targ |sed -n "/.*:\(.*\)/s//\1/p"`
		export OSCADA_ResTarget=$ResulsDir/OpenSCADA/LTS
		isRep=`echo $targ | sed -n "/.*\(rep\).*/s//\1/p"`
		if test -n "$isRep"; then OSCADA_ResTarget=$ResulsDir; fi
		$build $SrcLTSDir $SrcLTSVer `echo $targ | sed -n "/^\([^:]*\).*$/s//\1/p"`
		if test $? = 0; then echo $SrcLTSVer > $BldTargStamp;
		else echo "RepBranches: Error $? of building LTS $SrcLTSVer for $targ."; fi

		nBuild=$(($nBuild + 1))
	    fi
	    iTarg=$(($iTarg + 1))
	done

	if test -d $SrcLTSDir; then rm -R $SrcLTSDir; fi
    fi
else
    echo "RepBranches: The SubVersion server '$OSCADA_SVNServ' or the tag '$CurLTSSVNTag' unaccessible now! Pass the building."
fi

if test "x$logFile" != "x"; then
    if test -s $logFile -a $nBuild != 0; then gzip $logFile
    else rm $logFile; fi
fi

rm $BldLock