#!groovy

node ('master') {

	user_email = ''
	test_list = ''

	prepare_build()

	try {

		stage('Checkout') {
			checkout scm

			sh 'git clean -xdf'

			sh 'git reset --hard'
		}

		stage('Building x86') {
			regen_tests('i386')
		}

		stage('Building x64') {
			regen_tests('x86_64')
		}

		stage('Building arm64') {
			regen_tests('arm64')
		}

		stage('Patching') {
			make_patch()
		}

		stage('Email') {
			email()
		}
	}
	catch (Exception caughtError) {
		email_error()

		throw caughtError
	}
}

def prepare_build() {
	properties ([ 													\
		[$class: 'ParametersDefinitionProperty', 								\
			parameterDefinitions: [ 									\
				[$class: 'StringParameterDefinition',						\
					description: 'Who required the test',					\
					name: 'pEmail',									\
				],												\
				[$class: 'StringParameterDefinition',						\
					description: 'Space separated list of tests to run',			\
					name: 'pTests',									\
				],												\
			],
		]])

	user_email = pEmail;
	test_list  = pTests;

	echo "User ${user_email} requested regenerating tests : ${ test_list }"
}

def regen_tests( String arch ) {

	def install_dir = pwd tmp: true

	//Configure the conpilation (Output is not relevant)
	//Use the current directory as the installation target so nothing
	//escapes the sandbox
	//Also specify the compiler by hand
	sh "./configure CXX=clang++ CC=gcc-6 --host=${arch} --enable-silent-rules --quiet"

	//Compile the project
	sh 'make -j 8 --no-print-directory'

	//Regenerate the desired tests
	sh "src/tests/test.py --regenerate-expected ${test_list}"

	//Clean everything from the last build
	sh 'make maintainer-clean > /dev/null'
}

def make_patch() {

	def target_dir = pwd tmp: true

	//Add every file so new files appear in the diff
	sh 'git add .'

	//Create a patch file
	sh "git diff --cached --binary > ${target_dir}/result.patch"
}

def email() {

	def target_dir = pwd tmp: true

	dir( target_dir ) {

		def email_subject = "[cforall dashboard][TEST REGEN# ${env.BUILD_NUMBER}] - Result"
		def email_body = """This is an automated email from the Jenkins build machine. It was
generated https://cforall.uwaterloo.ca:8082/dashboard.html.

Please apply the required changes using the following method :
    - copy result.patch to your cforall repository of choice.
    - call 'git apply [patch file name]' (WARNING: the changes may conflict with any current changes to the test expected results).
    - validate that the changes are correct.
    - commit the changes as usual."""

		//send email notification
		emailext body: email_body, subject: email_subject, to: user_email, attachmentsPattern: "**/*.patch"
	}
}

def email_error() {

	def email_subject = "[cforall dashboard][TEST REGEN# ${env.BUILD_NUMBER}] - FAILURE"
	def email_body = """This is an automated email from the Jenkins build machine. It was
generated https://cforall.uwaterloo.ca:8082/dashboard.html.

Test generation encoutered an error please see attached logs

-----------------------------------------------------------------------"""

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