pipeline {
  agent { label 'master' }

  options {
    disableConcurrentBuilds()
    /* Prevent Jenkins jobs from running forever */
    timeout(time: 45, unit: 'MINUTES')
    /* Limit builds retained */
    buildDiscarder(logRotator(
      numToKeepStr: '10',
      daysToKeepStr: '30',
      artifactNumToKeepStr: '10',
    ))
  }


  stages {
    stage('Prep') {
      steps { script {
        println "Current JOB: ${env.JOB_NAME}"
        /* load common lib */
        cmn = load('ci/common.groovy')
        /* just for a shorter access */
        btype = cmn.getBuildType() 
      } }
    }
    stage('Build') {
      parallel {
        stage('MacOS') {
        when { expression { btype != 'release' } }
        steps { script {
          osx = cmn.buildBranch('status-react/combined/desktop-macos')
        } } }
        stage('Linux') {
        when { expression { btype != 'release' } }
        steps { script {
          nix = cmn.buildBranch('status-react/combined/desktop-linux')
        } } }
        stage('Windows') {
        when { expression { btype != 'release' } }
        steps { script {
          win = cmn.buildBranch('status-react/combined/desktop-windows')
        } } }
        stage('iOS') { steps { script {
          ios = cmn.buildBranch('status-react/combined/mobile-ios')
        } } }
        stage('Android') { steps { script {
          dro = cmn.buildBranch('status-react/combined/mobile-android')
        } } }
        stage('Android e2e') { steps { script {
          e2e = cmn.buildBranch('status-react/combined/mobile-android', 'e2e')
        } } }
      }
    }
    stage('Archive') {
      steps { script {
        sh('rm -f pkg/*')

        if (btype != 'release') {
          cmn.copyArts('status-react/combined/desktop-macos', osx.number)
          cmn.copyArts('status-react/combined/desktop-linux', nix.number)
          cmn.copyArts('status-react/combined/desktop-windows', win.number)
        }
        cmn.copyArts('status-react/combined/mobile-ios', ios.number)
        cmn.copyArts('status-react/combined/mobile-android', dro.number)
        cmn.copyArts('status-react/combined/mobile-android', e2e.number)
        cmn.copyArts('status-react/combined/mobile-ios', ios.number)
        archiveArtifacts('pkg/*')
      } }
    }
    stage('Upload') {
      steps { script {
        e2eUrl = cmn.uploadArtifact(cmn.pkgFind('e2e.apk'))
        apkUrl = cmn.uploadArtifact(cmn.pkgFind("${btype}.apk"))
        if (btype != 'release') {
          dmgUrl = cmn.uploadArtifact(cmn.pkgFind('dmg'))
          appUrl = cmn.uploadArtifact(cmn.pkgFind('AppImage'))
          exeUrl = cmn.uploadArtifact(cmn.pkgFind('exe'))
        } else {
          dmgUrl = null
          appUrl = null
          exeUrl = null
        }
        /* special case for iOS Diawi links */
        ipaUrl = ios.getBuildVariables().get('DIAWI_URL')
        /* add URLs to the build description */
        cmn.setBuildDesc(
          Apk: apkUrl, e2e: e2eUrl, iOS: ipaUrl, App: appUrl, Mac: dmgUrl, Win: exeUrl,
        )
        /* Create latest.json with newest nightly URLs */
        if (btype == 'nightly') {
          cmn.updateLatestNightlies(
            APK: apkUrl, IOS: ipaUrl, APP: appUrl, MAC: dmgUrl, WIN: exeUrl
          )
        }
      } }
    }
    stage('Notify') {
      steps { script {
        if (env.CHANGE_ID != null) {
          cmn.githubNotify(
            apk: apkUrl, e2e: e2eUrl, ipa: ipaUrl, app: appUrl, dmg: dmgUrl, win: exeUrl,
          )
        }
      } }
    }
    stage('Publish') {
      steps { script {
        switch (btype) {
          case 'nightly':
            build('misc/status.im')
            break
          case 'release':
            build(
              job: 'misc/cn.status.im',
              parameters: [
                [name: 'APK_URL', value: apkUrl, $class: 'StringParameterValue'],
              ]
            )
            break
        }
      } }
    }
    stage('Run e2e') {
      when { expression { btype == 'nightly' } }
      steps { script {
        e2eApk = e2e.getBuildVariables().get('SAUCE_URL')
        build(
          job: 'end-to-end-tests/status-app-nightly', wait: false,
          parameters: [string(name: 'apk', value: "--apk=${e2eApk}")]
        )
      } }
    }
  }
}