#!groovy

//===========================================================================================================
// Main compilation routines
//===========================================================================================================
def push_build() {
	//Don't use the build_stage function which outputs the compiler
	stage 'Push'

		status_prefix = 'Push'

		def out_dir = pwd tmp: true
		sh "mkdir -p ${out_dir}"

		//parse git logs to find what changed
		sh "git remote > ${out_dir}/GIT_REMOTE"
		git_remote = readFile("${out_dir}/GIT_REMOTE")
		remoteDoLangExists = git_remote.contains("DoLang")

		if( !remoteDoLangExists ) {
			sh 'git remote add DoLang git@gitlab.do-lang.org:internal/cfa-cc.git'
		}

		sh "git push DoLang ${gitRefNewValue}:master"
}

//===========================================================================================================
// Main loop of the compilation
//===========================================================================================================
node ('master'){
	try {
		//Prevent the build from exceeding 30 minutes
		timeout(60) {

			//Wrap build to add timestamp to command line
			wrap([$class: 'TimestamperBuildWrapper']) {

				stage 'Build'

					parallel (
						x64: { node ('master') {
							build job: 'Cforall/master', 					\
								parameters: [						\
									[$class: 'BooleanParameterValue', 		\
									  name: 'isFullBuild', 				\
									  value: true], 					\
									[$class: 'StringParameterValue', 		\
									  name: 'buildArchitecture', 			\
									  value: '64-bit']				\
								]
						}},
						x32: { node ('master') {
							build job: 'Cforall/master', 					\
								parameters: [						\
									[$class: 'BooleanParameterValue', 		\
									  name: 'isFullBuild', 				\
									  value: true], 					\
									[$class: 'StringParameterValue', 		\
									  name: 'buildArchitecture', 			\
									  value: '32-bit']				\
								]
						}}
					)

				//Push latest changes to do-lang repo
				//push_build()
			}
		}
	}

	//If an exception is caught we need to change the status and remember to
	//attach the build log to the email
	catch (Exception caughtError) {
		//rethrow error later
		err = caughtError

		//Store the result of the build log
		currentBuild.result = "${status_prefix} FAILURE".trim()

		//Send email to notify the failure
		//promote_email(currentBuild.result)
	}

	finally {
		//Must re-throw exception to propagate error
		if (err) {
			throw err
		}
	}
}

//===========================================================================================================
//Routine responsible of sending the email notification once the build is completed
//===========================================================================================================

//Email notification on a full build failure
def promote_email(String status) {
	//Since tokenizer doesn't work, figure stuff out from the environnement variables and command line
	//Configurations for email format
	def email_subject = "[cforall git][PROMOTE - FAILURE]"
	def email_body = """This is an automated email from the Jenkins build machine. It was
generated because of a git hooks/post-receive script following
a ref change was pushed to the repository containing
the project "UNNAMED PROJECT".

Check console output at ${env.BUILD_URL} to view the results.

- Status --------------------------------------------------------------

PROMOTE FAILURE - ${status}
"""

	def email_to = "pabuhr@uwaterloo.ca, rschlunt@uwaterloo.ca, a3moss@uwaterloo.ca, tdelisle@uwaterloo.ca, brice.dobry@huawei.com"

	//send email notification
	emailext body: email_body, subject: email_subject, to: email_to, attachLog: true
}
