pipeline {
    agent { label 'windows && amd64' }

    options { 
        buildDiscarder(logRotator(numToKeepStr: '135', daysToKeepStr: '21'))
        gitLabConnection('GitLabConnectionJenkins')
    }
    

    environment {
        BUILD_OPTIONS = ''
        TESTS_PARALLEL = ''
        APIURL_TO_TEST = ''
    }

    stages {
        stage('clean previous runs and update gitlab commit status'){
            steps{
                deleteDir()
                updateGitlabCommitStatus(name: 'Build & test windows', state: 'running')
            }
        }
        stage('Get parameters'){
            parallel{
                stage('Get build and run paramters'){
                    steps {
                        script{
                            BUILD_OPTIONS = sh(script: 'echo "$gitlabTriggerPhrase" | grep BUILD_OPTIONS | awk -F "BUILD_OPTIONS="  \'{print \$2}\' | cut -d"\\"" -f2 || :', returnStdout: true).trim()
                            TESTS_PARALLEL = sh(script: 'echo "$gitlabTriggerPhrase" | grep "\\-\\-sequence" >/dev/null 2>&1 && echo "" || echo "--INSTANCES:10"', returnStdout: true).trim()
                            println BUILD_OPTIONS
                            println TESTS_PARALLEL
                        }
                    }
                    post{
                        always {
                            script{                        
                                if (currentBuild.currentResult == 'FAILURE'){
                                    addGitLabMRComment(comment: ":red_circle: ${env.JOB_NAME} FAILURE when getting the additional build parameters :worried:<br/>Build results: [Jenkins [${env.JOB_NAME} ${env.BUILD_DISPLAY_NAME}]](${env.RUN_DISPLAY_URL})<br/>Commit: ${env.GIT_COMMIT}" )
                                }
                            }
                        }
                    }
                }

                stage('Get API URL'){
                    steps {
                        script{
                            APIURL_TO_TEST = sh(script: 'echo "$gitlabMergeRequestDescription" | grep USE_APIURL_TO_TEST | awk -F "USE_APIURL_TO_TEST="  \'{print \$2}\' | cut -d" " -f1', returnStdout: true).trim()
                            println APIURL_TO_TEST
                            if (APIURL_TO_TEST == ""){
                                APIURL_TO_TEST = "https://g.api.mega.co.nz/"
                                echo "APIURL_TO_TEST was not found on description so ${APIURL_TO_TEST} will be used by default"
                            }
                            echo "APIURL_TO_TEST will be ${APIURL_TO_TEST}"
                        }
                    }
                    post{
                        always {
                            script{                        
                                if (currentBuild.currentResult == 'FAILURE'){
                                    addGitLabMRComment(comment: ":red_circle: ${env.JOB_NAME} FAILURE when getting the SDK branch :worried:<br/>Build results: [Jenkins [${env.JOB_NAME} ${env.BUILD_DISPLAY_NAME}]](${env.RUN_DISPLAY_URL})<br/>Commit: ${env.GIT_COMMIT}" )
                                }
                            }
                        }
                    }
                }

            }
        }
        stage('Checkout Windows'){
            steps {
                checkout([
                    $class: 'GitSCM', 
                    branches: [[name: "${env.gitlabSourceBranch}"]],
                    userRemoteConfigs: [[ url: "${env.GIT_URL_SDK}", credentialsId: "12492eb8-0278-4402-98f0-4412abfb65c1" ]],
                    extensions: [
                        [$class: "UserIdentity",name: "jenkins", email: "jenkins@jenkins"],
                        [$class: 'PreBuildMerge', options: [fastForwardMode: 'FF', mergeRemote: "origin", mergeStrategy: 'DEFAULT', mergeTarget: "${env.gitlabTargetBranch}"]]
                        ]
                ])
                script{
                    windows_sources_workspace = WORKSPACE           
                }
            }
        }
        stage('Build Windows'){
            environment{
                VCPKGPATH  = "${windows_sources_workspace}\\..\\..\\3rdparty"
            }
            options{
                timeout(time: 150, unit: 'MINUTES')
            }
            steps{
                dir(windows_sources_workspace){
                    //Build SDK
                    sh "echo Building SDK"  
                    sh "mkdir contrib\\\\cmake\\\\build_dir\\\\"
                    sh "cmake -DENABLE_CHAT=1 -DCMAKE_VERBOSE_MAKEFILE=1 -DMega3rdPartyDir='${VCPKGPATH}' ${BUILD_OPTIONS} -DCMAKE_GENERATOR_PLATFORM=x64 -S '${windows_sources_workspace}'\\\\contrib\\\\cmake\\\\   -B '${windows_sources_workspace}'\\\\contrib\\\\cmake\\\\build_dir\\\\"
                    sh "cmake --build '${windows_sources_workspace}'\\\\contrib\\\\cmake\\\\build_dir\\\\ --config Debug --target Mega -j 1"
                    sh "cmake --build '${windows_sources_workspace}'\\\\contrib\\\\cmake\\\\build_dir\\\\ --config Debug --target megacli -j 1"
                    sh "cmake --build '${windows_sources_workspace}'\\\\contrib\\\\cmake\\\\build_dir\\\\ --config Debug --target megasimplesync -j 1"
                    sh "cmake --build '${windows_sources_workspace}'\\\\contrib\\\\cmake\\\\build_dir\\\\ --config Debug --target test_integration -j 1"
                    sh "cmake --build '${windows_sources_workspace}'\\\\contrib\\\\cmake\\\\build_dir\\\\ --config Debug --target test_unit -j 1"

                    //Package megacli and megasimplesync to be archived
                    sh "echo Packaging megacli and megasimplesync to be archived"
                    sh "mkdir examples-${BUILD_ID}"    
                    sh "cp contrib\\\\cmake\\\\build_dir\\\\Debug\\\\megacli.exe examples-${BUILD_ID}"
                    sh "cp contrib\\\\cmake\\\\build_dir\\\\Debug\\\\megasimplesync.exe examples-${BUILD_ID}"
                    sh "ls ..\\\\3rdparty\\\\vcpkg\\\\installed\\\\x64-windows-mega"
                    bat "copy ..\\\\3rdparty\\\\vcpkg\\\\installed\\\\x64-windows-mega\\\\debug\\\\bin\\\\*.dll examples-${BUILD_ID} || :"
                    bat "powershell Compress-Archive -Path .\\\\examples-${BUILD_ID}\\\\* -DestinationPath .\\\\examples-${BUILD_ID}.zip || :"
                }
            }
        }

        stage('Run Windows tests'){
            options{
                timeout(time: 250, unit: 'MINUTES')
            }
            environment {
                PATH = "${windows_sources_workspace}\\\\..\\\\..\\\\3rdparty\\\\vcpkg\\\\installed\\\\x64-windows-mega\\\\debug\\\\bin;${env.PATH}"
                MEGA_PWD = credentials('MEGA_PWD_DEFAULT')
                MEGA_PWD_AUX = credentials('MEGA_PWD_DEFAULT')
                MEGA_PWD_AUX2 = credentials('MEGA_PWD_DEFAULT')
                MEGA_REAL_PWD=credentials('MEGA_REAL_PWD_TEST')
            }
            steps{
                lock(label: 'SDK_Concurrent_Test_Accounts', variable: 'ACCOUNTS_COMBINATION', quantity: 1, resource: null){
                    dir("${windows_sources_workspace}") {
                        script{
                            env.MEGA_EMAIL = "${env.ACCOUNTS_COMBINATION}"
                            echo "${env.ACCOUNTS_COMBINATION}"
                        }
                        sh "echo Running tests"
                        bat """
                        contrib\\\\cmake\\\\build_dir\\\\Debug\\\\test_unit.exe
                        if %ERRORLEVEL% NEQ 0 exit 1
                        contrib\\\\cmake\\\\build_dir\\\\Debug\\\\test_integration.exe --CI --USERAGENT:${env.USER_AGENT_TESTS} --APIURL:${APIURL_TO_TEST} ${TESTS_PARALLEL}
                        if %ERRORLEVEL% NEQ 0 set ERROR_VAL=1
                        gzip -c test_integration.log > test_integration_${BUILD_ID}.log.gz
                        rm test_integration.log
                        exit %ERROR_VAL%
                        """
                    }
                }
            }
        }              
    }
            
    post{
        always {
            archiveArtifacts artifacts: 'test_integration*log*, examples-*.zip', fingerprint: true
            script{
                if (currentBuild.currentResult == 'SUCCESS'){
                    addGitLabMRComment(comment: ":white_check_mark: ${currentBuild.projectName} <b>Windows</b> SUCCEEDED :muscle:<br/>Build results: [Jenkins [${currentBuild.displayName}]](${currentBuild.absoluteUrl})<br/>Commit: ${env.GIT_COMMIT}" )
                    updateGitlabCommitStatus(name: 'Build & test windows', state: 'success')
                }                                
                if (currentBuild.currentResult == 'FAILURE'){
                    addGitLabMRComment(comment: ":red_circle: ${currentBuild.projectName} <b>Windows</b> FAILURE  :worried:<br/>Build results: [Jenkins [${currentBuild.displayName}]](${currentBuild.absoluteUrl})<br/>Commit: ${env.GIT_COMMIT}" )
                    updateGitlabCommitStatus(name: 'Build & test windows', state: 'failed')
                }
                if (currentBuild.currentResult == 'ABORTED'){
                    addGitLabMRComment(comment: ":interrobang: ${currentBuild.projectName} <b>Windows</b> ABORTED  :confused:<br/>Build results: [Jenkins [${currentBuild.displayName}]](${currentBuild.absoluteUrl})<br/>Commit: ${env.GIT_COMMIT}" )
                    updateGitlabCommitStatus(name: 'Build & test windows', state: 'canceled')
                }                                
                if (currentBuild.currentResult == 'UNSTABLE'){
                    addGitLabMRComment(comment: ":interrobang: ${currentBuild.projectName} <b>Windows</b> UNSTABLE  :confused:<br/>Build results: [Jenkins [${currentBuild.displayName}]](${currentBuild.absoluteUrl})<br/>Commit: ${env.GIT_COMMIT}" )
                    updateGitlabCommitStatus(name: 'Build & test windows', state: 'failed')
                }
            }
        }
    }
}
