#!groovy

//===========================================================================================================
// Main loop of the compilation
//===========================================================================================================

node {
	def err = null

	final scmVars = checkout scm
	final commitId = scmVars.GIT_COMMIT

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

			stage('Build') {

				parallel (
					gcc_08_x86_new: { trigger_build( 'gcc-8',   'x86' ) },
					gcc_07_x86_new: { trigger_build( 'gcc-7',   'x86' ) },
					gcc_06_x86_new: { trigger_build( 'gcc-6',   'x86' ) },
					gcc_10_x64_new: { trigger_build( 'gcc-10',  'x64' ) },
					gcc_09_x64_new: { trigger_build( 'gcc-9',   'x64' ) },
					gcc_08_x64_new: { trigger_build( 'gcc-8',   'x64' ) },
					gcc_07_x64_new: { trigger_build( 'gcc-7',   'x64' ) },
					gcc_06_x64_new: { trigger_build( 'gcc-6',   'x64' ) },
					clang_x64_new:  { trigger_build( 'clang',   'x64' ) },
				)
			}

			stage('Package') {
				trigger_dist( commitId, currentBuild.number.toString() )
			}

			stage('Promote') {
				trigger_prom()
			}
		}

		promote_email(true)
	}

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

		//rethrow error later
		err = caughtError

		//Store the result of the build log
		currentBuild.result = 'FAILURE'

		//Send email to notify the failure
		promote_email(false)
	}

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

def trigger_build(String cc, String arch) {
	// Randomly delay the builds by a random amount to avoid hitting the SC server to hard
	sleep(time: 5 * Math.random(), unit:"MINUTES")

	// Run the build
	// Don't propagate, it doesn't play nice with our email setup
	def result = build job: 'Cforall/master', 		\
		parameters: [						\
			[$class: 'StringParameterValue', 		\
			  name: 'Compiler', 				\
			  value: cc],					\
			[$class: 'StringParameterValue', 		\
			  name: 'Architecture', 			\
			  value: arch],					\
			[$class: 'BooleanParameterValue', 		\
			  name: 'NewAST', 				\
			  value: true], 					\
			[$class: 'BooleanParameterValue', 		\
			  name: 'RunAllTests', 				\
			  value: true], 					\
			[$class: 'BooleanParameterValue', 		\
			  name: 'RunBenchmark', 			\
			  value: true], 					\
			[$class: 'BooleanParameterValue', 		\
			  name: 'BuildDocumentation', 		\
			  value: true], 					\
			[$class: 'BooleanParameterValue', 		\
			  name: 'Publish', 				\
			  value: true], 					\
			[$class: 'BooleanParameterValue', 		\
			  name: 'Silent', 				\
			  value: true], 					\
		],								\
		propagate: false

	echo(result.result)

	if(result.result != 'SUCCESS') {
		sh("wget -q -O - https://cforall.uwaterloo.ca/jenkins/job/Cforall/job/master/${result.number}/consoleText")
		error(result.result)
	}
}

def trigger_dist(String commitId, String buildNum) {
	def result = build job: 'Cforall_Distribute_Ref',	\
		parameters: [						\
			string(name: 'GitRef', value: commitId),	\
			string(name: 'Build' , value: buildNum)	\
		],								\
		propagate: false

	echo(result.result)

	if(result.result != 'SUCCESS') {
		sh("wget -q -O - https://cforall.uwaterloo.ca/jenkins/job/Cforall_Distribute_Ref/${result.number}/consoleText")
		error(result.result)
	}
}

def trigger_prom() {
	def result = build job: 'Cforall_Promote_Ref', propagate: false

	echo(result.result)

	if(result.result != 'SUCCESS') {
		sh("wget -q -O - https://cforall.uwaterloo.ca/jenkins/job/Cforall_Promote_Ref/${result.number}/consoleText")
		error(result.result)
	}
}

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

//Email notification on a full build failure
def promote_email(boolean success) {
	node {
		echo('notifying users')

		def result = success ? "PROMOTE - SUCCESS" : "PROMOTE - FAILURE"

		//Since tokenizer doesn't work, figure stuff out from the environnement variables and command line
		//Configurations for email format
		def email_subject = "[cforall git][${result}]"
		def email_body = """<p>This is an automated email from the Jenkins build machine. It was
	generated following the result of the C\u2200 nightly build.</p>

	<p>Check console output at ${env.BUILD_URL} to view the results.</p>

	<p>- Status --------------------------------------------------------------</p>

	<p>${result}</p>

	<p>- Logs ----------------------------------------------------------------</p>
	"""

		def email_to = "cforall@lists.uwaterloo.ca"

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