#!/usr/bin/env groovy
library 'status-jenkins-lib@v1.8.18'

/* Options section can't access functions in objects. */
def isPRBuild = utils.isPRBuild()
def isNightlyBuild = utils.isNightlyBuild()

pipeline {
  agent { label 'linux && x86_64 && nix-2.14' }

  parameters {
    booleanParam(
      name: 'RELEASE',
      description: 'Decides whether release credentials are used.',
      defaultValue: params.RELEASE ?: false
    )
    booleanParam(
      name: 'INCLUDE_DEBUG_SYMBOLS',
      description: 'Decides whether binaries are built with debug symbols.',
      defaultValue: params.INCLUDE_DEBUG_SYMBOLS ?: false
    )
    choice(
      name: 'VERBOSE',
      description: 'Level of verbosity based on nimbus-build-system setup.',
      choices: ['0', '1', '2']
    )
    string(
      name: 'NIMFLAGS',
      description: 'Extra Nim flags. Examples: --verbosity:2 --passL:"-v" --passC:"-v"',
      defaultValue: '--colors:off'
    )
    booleanParam(
      name: 'USE_MOCKED_KEYCARD_LIB',
      description: 'Decides whether the mocked status-keycard-go library is built.',
      defaultValue: isNightlyBuild
    )
  }

  options {
    timestamps()
    /* Prevent Jenkins jobs from running forever */
    timeout(time: 25, unit: 'MINUTES')
    /* manage how many builds we keep */
    buildDiscarder(logRotator(
      numToKeepStr: '10',
      daysToKeepStr: '30',
      artifactNumToKeepStr: '1',
    ))
    /* Allows combined build to copy */
    copyArtifactPermission('/status-desktop/*')
    /* Abort old PR builds. */
    disableConcurrentBuilds(
      abortPrevious: isPRBuild
    )
  }

  environment {
    PLATFORM = "linux-nix/${getArch()}"
    /* Improve make performance */
    MAKEFLAGS = "-j4 V=${params.VERBOSE}"
    /* Avoid weird bugs caused by stale cache. */
    QML_DISABLE_DISK_CACHE = "true"
    /* Control output the filename */
    STATUS_CLIENT_APPIMAGE = "pkg/${utils.pkgFilename(ext: 'AppImage', arch: getArch())}"
    STATUS_CLIENT_TARBALL = "pkg/${utils.pkgFilename(ext: 'tar.gz', arch: getArch())}"
  }

  stages {
    stage('Deps') {
      steps { script {
        nix.shell('make update', pure: true)
        nix.shell('make deps', pure: true)
      } }
    }

    stage('status-go') {
      steps { script {
        nix.shell('make status-go', pure: true)
      } }
    }

    stage('Package') {
      steps { script {
        linux.bundle('tgz-linux', 0, true)
      } }
    }

    stage('Parallel Upload') {
      parallel {
        stage('Upload') {
          steps { script {
            env.PKG_URL = s3.uploadArtifact(env.STATUS_CLIENT_TARBALL)
            jenkins.setBuildDesc(AppImage: env.PKG_URL)
          } }
        }
        stage('Archive') {
          steps { script {
            archiveArtifacts("${env.STATUS_CLIENT_TARBALL}*")
          } }
        }
      }
    }
  }

  post {
    success { script { github.notifyPR(true) } }
    failure { script { github.notifyPR(false) } }
    cleanup { sh './scripts/clean-git.sh' }
  }
}

def getArch() {
  def tokens = Thread.currentThread().getName().split('/')
  for (def arch in ['x86_64', 'aarch64']) {
    if (tokens.contains(arch)) { return arch }
  }
}