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

    options {
        buildDiscarder(logRotator(numToKeepStr: '25', daysToKeepStr: '30'))
        gitLabConnection('GitLabConnectionJenkins')
        copyArtifactPermission('*');

    }
    parameters {
        booleanParam(name: 'IS_RELEASE_CANDIDATE', defaultValue: false, description: 'Is it a Release Candidate?')
        string(name: 'RELEASE_CANDIDATE_NAME', defaultValue: '', description: 'i.e: RC2, RC3 (only needed if IS_RELEASE_CANDIDATE is true)')
        choice(name: 'ARCHITECTURE', choices: ['64', '32/64'], description: 'To build either for 64 bit or both 32 and 64 bit')
        string(name: 'MEGACMD_BRANCH', defaultValue: "${env.BRANCH_NAME}", description: 'Which version of MEGAcmd should we build? Default is current branch.')
        string(name: 'SDK_BRANCH', defaultValue: ' ', description: 'Optionally, define a custom SDK branch.')
    }
    environment {
        MEGACMD_BRANCH = "${params.MEGACMD_BRANCH}"
        SDK_BRANCH = "${params.SDK_BRANCH}"
    }

    stages {
        stage('Clean previous runs'){
            steps{
                deleteDir()
            }
        }

        stage('Checkout windows'){
            steps {
                checkout([
                    $class: 'GitSCM',
                    branches: [[name: "${env.MEGACMD_BRANCH}"]],
                    userRemoteConfigs: [[ url: "${env.GIT_URL_MEGACMD}", credentialsId: "12492eb8-0278-4402-98f0-4412abfb65c1" ]],
                    extensions: [
                        [$class: "UserIdentity",name: "jenkins", email: "jenkins@jenkins"]
                        ]
                ])
                script {
                    windows_sources_workspace = WORKSPACE
                    def branchToCheckout = ''

                    if (env.SDK_BRANCH && env.SDK_BRANCH != ' ') {
                        branchToCheckout = env.SDK_BRANCH
                        shouldCloneSDK = true
                    } else if (env.MEGACMD_BRANCH in ['develop', 'master']) {
                        branchToCheckout = "develop"
                        shouldCloneSDK = true
                    } else {
                        def status = sh(script: "git submodule status", returnStdout: true).trim()
                        branchToCheckout = status.tokenize(' ')[0].substring(1)
                    }
                    dir('sdk') {
                        sh "echo Cloning SDK branch ${branchToCheckout}"
                        checkout([
                            $class: 'GitSCM',
                            branches: [[name: branchToCheckout]],
                            userRemoteConfigs: [[url: "${env.GIT_URL_SDK}", credentialsId: "12492eb8-0278-4402-98f0-4412abfb65c1"]],
                            extensions: [
                                [$class: "UserIdentity", name: "jenkins", email: "jenkins@jenkins"]
                            ]
                        ])
                    }
                }
            }
        }
        stage ('Build Windows installers'){
            environment{
                MEGA_VCPKGPATH  = "${windows_sources_workspace}\\..\\vcpkg_cmdins"
                MEGA_WIN_KITVER = "10.0.19041.0"
            }
            steps {
                dir(windows_sources_workspace + '\\build'){
                    bat """
                        @echo on
                        mkdir tmp
                        set _MSPDBSRV_ENDPOINT_= ${BUILD_TAG}
                        set TMP=${windows_sources_workspace}\\build\\tmp
                        set TEMP=${windows_sources_workspace}\\build\\tmp
                        set TMPDIR=${windows_sources_workspace}\\build\\tmp
                        full_build_process.cmd ${params.ARCHITECTURE} nosign 2
                    """
                }
            }
        }
        stage ('Upload Installers') {
            when {
                beforeAgent true
                anyOf {
                    expression { params.IS_RELEASE_CANDIDATE == true}
                }
            }
            environment{
                JF_PATH  = "${windows_sources_workspace}\\.."
            }
            steps {
                dir(windows_sources_workspace + '\\build'){
                    script {
                        def windows_artifactory_upload = { String ART_UPLOAD_PATH, String buildDir, String arch ->

                            withCredentials([string(credentialsId: 'MEGACMD_ARTIFACTORY_TOKEN', variable: 'MEGACMD_ARTIFACTORY_TOKEN')]) {
                                powershell """
                                    ${JF_PATH}\\jf rt del --url ${REPO_URL} --access-token $MEGACMD_ARTIFACTORY_TOKEN ${ART_UPLOAD_PATH}
                                    ${JF_PATH}\\jf rt upload --url ${REPO_URL} --access-token $MEGACMD_ARTIFACTORY_TOKEN MEGAcmdSetup${arch}_unsigned.exe ${ART_UPLOAD_PATH}
                                    cd ${buildDir}
                                    ${JF_PATH}\\jf rt upload --url ${REPO_URL} --access-token $MEGACMD_ARTIFACTORY_TOKEN legacy.pdb ${ART_UPLOAD_PATH}
                                    ${JF_PATH}\\jf rt upload --url ${REPO_URL} --access-token $MEGACMD_ARTIFACTORY_TOKEN LMegacmdClient.pdb ${ART_UPLOAD_PATH}
                                    ${JF_PATH}\\jf rt upload --url ${REPO_URL} --access-token $MEGACMD_ARTIFACTORY_TOKEN LMegacmdServer.pdb ${ART_UPLOAD_PATH}
                                    ${JF_PATH}\\jf rt upload --url ${REPO_URL} --access-token $MEGACMD_ARTIFACTORY_TOKEN MegacmdServer.pdb ${ART_UPLOAD_PATH}
                                    ${JF_PATH}\\jf rt upload --url ${REPO_URL} --access-token $MEGACMD_ARTIFACTORY_TOKEN Mega.pdb ${ART_UPLOAD_PATH}
                                    ${JF_PATH}\\jf rt upload --url ${REPO_URL} --access-token $MEGACMD_ARTIFACTORY_TOKEN MEGAclient.exe ${ART_UPLOAD_PATH}
                                    ${JF_PATH}\\jf rt upload --url ${REPO_URL} --access-token $MEGACMD_ARTIFACTORY_TOKEN MEGAcmdServer.exe ${ART_UPLOAD_PATH}
                                    ${JF_PATH}\\jf rt upload --url ${REPO_URL} --access-token $MEGACMD_ARTIFACTORY_TOKEN MEGAcmdShell.exe ${ART_UPLOAD_PATH}
                                    ${JF_PATH}\\jf rt upload --url ${REPO_URL} --access-token $MEGACMD_ARTIFACTORY_TOKEN MEGAcmdUpdater.exe ${ART_UPLOAD_PATH}
                                """
                            }
                        }
                        MEGACMD_VERSION = getVersionFromHeader("../src/megacmdversion.h")
                        sh "echo $MEGACMD_VERSION > version.txt"
                        if (params.ARCHITECTURE == "64") {
                            windows_artifactory_upload("MEGAcmd-releases/$MEGACMD_VERSION/$RELEASE_CANDIDATE_NAME/windows/", "built64", "64")
                        } else if (params.ARCHITECTURE == "32/64") {
                            windows_artifactory_upload("MEGAcmd-releases/$MEGACMD_VERSION/$RELEASE_CANDIDATE_NAME/windows/built64/", "built64", "64")
                            windows_artifactory_upload("MEGAcmd-releases/$MEGACMD_VERSION/$RELEASE_CANDIDATE_NAME/windows/built32/", "built32", "32")
                        }
                    }
                    archiveArtifacts artifacts: 'version.txt', onlyIfSuccessful: true
                }
            }
            post {
                success {
                    script {
                        echo "Installers successfully uploaded. URL: [${env.REPO_URL}/MEGAcmd-releases/$MEGACMD_VERSION/$RELEASE_CANDIDATE_NAME/windows]"
                    }
                }
            }
        }
    }
    post {
        always {
            deleteDir()
        }
    }
}

def getVersionFromHeader(String versionFilePath) {
    return sh(script: """
        awk  '/#define MEGACMD_MAJOR_VERSION/ { MAJOR=\$3 }; \
              /#define MEGACMD_MINOR_VERSION/ { MINOR=\$3 }; \
              /#define MEGACMD_MICRO_VERSION/ { MICRO=\$3 }; \
              END { print MAJOR"."MINOR"."MICRO }' \
              $versionFilePath
        """
        , returnStdout: true).trim()
}

