#!/bin/sh
# checkout a backing tree
# Copyright (C) 2014 - 2018 FARGOS Development, LLC
# $Id$
# Has dependency on svnGetRevisionDate and svnGetAttribution

# Local convention
# TODO:  svn-aware, enhance to support git usage

# default to using first element of BT_ROOT_PATH
btRoot=`pathedit --space ${BT_ROOT_PATH:-/opt/backingTrees} | awk '{ print $1; }'`
SB_BUILD_ARCH=`buildArch`
targetArch=`buildArch`

dateId=`date +%y%m%d`
bldPrefix="bld${dateId}"


bldLabel=""
reuseFlag=""
doCheckout=1
doGenMakefiles=1
doBuild=1
parallel=""
useBranch=""

repositoryType="svn"

checkoutCmd="sbsvnget"
postCheckoutCmd="svnGetRevisionDate --set"
SVN_BRANCHES_PATH="/svn/branches"
export SVN_BRANCHES_PATH

while test $# -gt 0
do
	case "${1}" in
	-h | -help | --help)
		printf "usage: %s [{--svn | --git}] [--reuse] [--btroot btRoot] [--ta targetArchitecture] [--bt backingTree] [--nocheckout | {--releasebranch branchName}] [--nogenmake] [--nobuild]\n" "${0}" >&2
		printf "  --btroot = specify alternate backing tree root, default is \"%s\"\n" "${btRoot}" >&2
		printf "  --ta = target architecture, default is \"native\"\n" >&2
		printf "  --bt = specify backing tree name to be used rather than auto-generate one\n" >&2
		printf "  --nocheckout = don't checkout source\n" >&2
		printf "  --releasebranch name - checkout specified release image\n" >&2
		printf "  --nogenmake = don't use createNewMakefiles to generate sb.Makefile\n" >&2
		printf "  --nobuild = don't use buildEverything to compile source tree\n" >&2
		exit 1
		;;
	--svn)
		repositoryType="svn"
		checkoutCmd="sbsvnget"
		postCheckoutCmd="svnGetRevisionDate --set"
		;;
	--git)
		repositoryType="git"
		checkoutCmd="sbgitget"
		postCheckoutCmd="gitGetRevisionDate --set"
		;;
	-bt | --bt)
		bldName="${2}"
		shift
		;;
	-btroot | --btroot)
		btRoot="${2}"
		shift
		;;
	-ta | --ta)
		targetArch="${2}"
		shift
		;;
	-j)
		parallel="-j ${2}"
		shift
		;;
	-noco | -nocheckout | --nocheckout)
		doCheckout=0
		;;
	-releasebranch | --releasebranch)
		useBranch="${2}"
		shift
		;;
	-nobuild | --nobuild)
		doBuild=0
		;;
	-nogenmake | --nogenmake)
		doGenMakefiles=0
		;;
	-reuse | --reuse)
		reuseFlag="--reuse"
		;;
	--blame)
		postCheckoutCmd="svnGetAttribution"
		doBuild=0
		doGenMakefiles=0
		;;
	*)
		printf "%s: unrecognized argument: \"%s\"\n" "${0}" "${1}" >&2
		exit 1
		;;
	esac
	shift
done

if test -n "${useBranch}"
then
	case "${useBranch}" in
	[0-9]*)	# a date?
		useBranch="release_${useBranch}"
		;;
	esac
	export SVN_PATH="${SVN_BRANCHES_PATH}/${useBranch}"
	echo Using SVN_PATH ${SVN_PATH}
fi

BT_ROOT_PATH="${btRoot}"; export BT_ROOT_PATH
SB_TARGET_ARCH="${targetArch}"

if test -z "${bldName}"
then
	for suffix in a b c d e f g h i j k l m n o p
	do
		if test ! -e "${btRoot}/${bldPrefix}${suffix}"
		then
			bldLabel="${suffix}"
			break
		fi
	done
	if test -z "${bldLabel}"
	then
		printf "%s: cannot generate unique build label for %s\n" ${0}" ${bldPrefix} >&2
		exit 1
	fi
	bldName="${bldPrefix}${bldLabel}"
fi

mkbt ${reuseFlag} -copy -btroot "${btRoot}" -bt ${bldName} -ta ${targetArch}

if test ! -e ${btRoot}/${bldName}/src
then
	printf "%s: did not create %s backing tree directory successfully\n" "${0}" ${bldName} >&2
	exit 1
fi

# generate RC file for backing tree
mkBTrcFile --btroot "${btRoot}" --ta "${SB_TARGET_ARCH}" -bt "${bldName}"

# get sandbox rc file for backing tree
tempSB=${btRoot}/${bldName}/obj/sandbox_${SB_TARGET_ARCH}.rc

if test false # don't do
then
ST=${btRoot}/${bldName}
STS=${ST}/src
STI=${ST}/include
STO=${ST}/obj/${SB_TARGET_ARCH}
STL=${ST}/${SB_TARGET_ARCH}/lib
STB=${ST}/${SB_TARGET_ARCH}/bin
SB_NAME=${bldName}
# export sandbox environment variables
for envName in SB_NAME SB_BUILD_ARCH SB_TARGET_ARCH ST BT_PATH STS STO STL STI STB
do
	export ${envName}
done
fi # end don't do

cd ${STS}

if test ${doCheckout} -eq 1
then
	alwaysCheckout=`sbBackingTreeRootFilter`
	for dir in ${alwaysCheckout}
	do
		workon -rc "${tempSB}" -sb ${bldName} ${checkoutCmd} ${dir}
	done
	# touch all C/C++ headers and source to match revision time
	find ${STS} '(' -name '*.[hc]' -o -name '*.[hc]pp' ')' -exec ${postCheckoutCmd} '{}' ';'
fi

if test ${doGenMakefiles} -eq 1
then
	# create sandbox-specific makefiles
	workon -rc ${tempSB} -sb ${bldName} createNewMakefiles -all -force # -noconfig
	rc=$?
	if test ${rc} -ne 0
	then
		printf "%s: could not create sb.Makefile files under \"%s\"\n" "${0}" "${ST}" >&2
		exit 1
	fi
fi

rc=0
if test ${doBuild} -eq 1
then
	# touch lastFileDelete timestamp
	touch "${btRoot}/${bldName}/obj/${SB_TARGET_ARCH}/lastFileDelete.timestamp"
	echo Doing workon -rc ${tempSB} -sb ${bldName} buildEverything ${parallel}
	export SB_ALT_CONFIG=Test # force use of internal config
	/usr/bin/nice workon -rc ${tempSB} -sb ${bldName} buildEverything ${parallel}
	rc=$?
fi
exit ${rc}
