#!groovy

import groovy.transform.Field

// For skipping stages
import org.jenkinsci.plugins.pipeline.modeldefinition.Utils

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

node('master') {
	// Globals
	BuildDir  = pwd tmp: true
	SrcDir    = pwd tmp: false
	Settings  = null

	// Local variables
	def err = null
	def log_needed = false

	currentBuild.result = "SUCCESS"

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

			build_id = prepare_build()

			node('x64') {
				BuildDir  = pwd tmp: true
				SrcDir    = pwd tmp: false

				Tools.clean()

				// checkout()
			}

			// Update the build directories when exiting the node
			BuildDir  = pwd tmp: true
			SrcDir    = pwd tmp: false
		}
	}

	//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

		// echo err.toString()

		// //An error has occured, the build log is relevent
		// log_needed = true

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

	finally {
		// //Send email with final results if this is not a full build
		// email(log_needed)

		// echo 'Distribution Completed'

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

//===========================================================================================================
// Main compilation routines
//===========================================================================================================


//Compilation script is done here but environnement set-up and error handling is done in main loop
// def checkout() {
// 	build_stage('Checkout', true) {
// 		//checkout the source code and clean the repo
// 		final scmVars = checkout scm
// 		Settings.GitNewRef = scmVars.GIT_COMMIT
// 		Settings.GitOldRef = scmVars.GIT_PREVIOUS_COMMIT

// 		echo GitLogMessage()
// 	}
// }

//===========================================================================================================
// Helper classes/variables/routines
//===========================================================================================================
def prepare_build() {
	// prepare the properties
	properties ([ 													\
		buildDiscarder(logRotator(										\
			artifactDaysToKeepStr: '',									\
			artifactNumToKeepStr: '',									\
			daysToKeepStr: '730',										\
			numToKeepStr: '1000'										\
		)),														\
		[$class: 'ParametersDefinitionProperty', 								\
			parameterDefinitions: [ 									\
				[$class: 'StringParameterDefinition',						\
					description: 'The build to put in the version',				\
					name: 'Build',									\
					defaultValue: '',  								\
				],												\
			],
		]])

	// It's unfortunate but it looks like we need to checkout the entire repo just to get
	// - the pretty git printer
	// - Jenkins.tools
	checkout scm

	Tools = load "Jenkins/tools.groovy"

	currentBuild.description = "Distributing Binaries"

	if (!params.Build) {
		params.Build = Jenkins.instance.getItem('Cforall Full Build').lastSuccessfulBuild.number
	}

	echo "Distributing build ${params.Build}"

	return params.Build
}

