stages:
  - build
  - upload

default:
  interruptible: true
  timeout: 4h

variables:
  AUTOBUILD_BUILD_ID: $CI_PIPELINE_ID
  AUTOBUILD_INSTALLABLE_CACHE: "$CI_PROJECT_DIR/.cache/autobuild"
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
  VIEWER_USE_CRASHPAD: "TRUE"
  VIEWER_CRASHPAD_URL: $SENTRY_DSN

.win_build:
  stage: build
  tags:
    - autobuild
    - windows
  cache:
    key:
      files:
        - autobuild.xml
      prefix: ${CI_JOB_NAME}
    paths:
      - .cache/autobuild
      - .cache/pip
      - .venv/
    when: 'always'
  before_script:
    - virtualenv .venv
    - .\.venv\Scripts\activate.ps1
    - pip install --upgrade autobuild -i https://pkg.alchemyviewer.org/repository/autobuild/simple --extra-index-url https://pypi.org/simple
  script:
    - |
        autobuild configure -c Release -- -DUSE_FMODSTUDIO=ON -DUSE_NVAPI=ON -DUSE_LTO=ON -DVS_DISABLE_FATAL_WARNINGS=ON -DREVISION_FROM_VCS=FALSE
        autobuild build -c Release --no-configure
  artifacts:
    name: "$env:CI_COMMIT_REF_NAME-$env:CI_COMMIT_SHORT_SHA"
    expire_in: 1 week
    paths:
      - build-vc-*/newview/Release/build_data.json
      - build-vc-*/newview/Release/singularity-bin.exe
      - build-vc-*/newview/Release/*.pdb
      - build-vc-*/newview/Release/*.dll
      - build-vc-*/newview/Release/Singularity_*_Setup.exe

.linux_build:
  stage: build
  image: r.alchemyviewer.org/singularity/infrastructure/debian-build-image:latest
  tags:
    - linux
    - docker
  cache:
    key:
      files:
        - autobuild.xml
      prefix: ${CI_JOB_NAME}
    paths:
      - .cache/autobuild
      - .cache/pip
      - .venv
    when: 'always'
  before_script:
    - virtualenv .venv -p python2
    - source .venv/bin/activate
    - pip install --upgrade autobuild -i https://pkg.alchemyviewer.org/repository/autobuild/simple --extra-index-url https://pypi.org/simple
  script:
    - |
        autobuild configure -c Release -- -DUSE_FMODSTUDIO=ON -DUSE_NVAPI=ON -DUSE_LTO=ON -DUNIX_DISABLE_FATAL_WARNINGS=ON -DREVISION_FROM_VCS=FALSE
        autobuild build -c Release --no-configure
  artifacts:
    name: "$env:CI_COMMIT_REF_NAME-$env:CI_COMMIT_SHORT_SHA"
    expire_in: 1 week
    paths:
      - build-linux-*/build_data.json
      - build-linux-*/newview/Singularity_*.tar.xz

.win32_build:
  extends: .win_build
  variables:
    AUTOBUILD_ADDRSIZE: 32
    VIEWER_USE_CRASHPAD: "FALSE"
  cache:
    key:
      prefix: windows32

.win64_build:
  extends: .win_build
  variables:
    AUTOBUILD_ADDRSIZE: 64
  cache:
    key:
      prefix: windows64

.linux32_build:
  extends: .linux_build
  variables:
    AUTOBUILD_ADDRSIZE: 32
  cache:
    key:
      prefix: linux32

.linux64_build:
  extends: .linux_build
  variables:
    AUTOBUILD_ADDRSIZE: 64
  cache:
    key:
      prefix: linux64

.master_rules:
  rules:
    - if: $BUILD_CHANNEL || $CI_COMMIT_TAG
      when: never
    - if: '$CI_PIPELINE_SOURCE == "web"'
    - if: '$CI_PIPELINE_SOURCE == "schedule"'
    #- if: '$CI_COMMIT_BRANCH == "master" && $CI_PIPELINE_SOURCE == "push"'
    #  when: delayed
    #  start_in: '12 hours'
  variables:
    VIEWER_CHANNEL_TYPE: Test
    VIEWER_USE_CRASHPAD: "FALSE"

.project_rules:
  rules:
    - if: '$BUILD_CHANNEL == "Project" && ($CI_PIPELINE_SOURCE == "web" || $CI_PIPELINE_SOURCE == "schedule")'
    - if: '$CI_COMMIT_TAG =~ /.*-project/'
  variables:
    VIEWER_CHANNEL_TYPE: Project

.beta_rules:
  rules:
    - if: '$BUILD_CHANNEL == "Beta" && ($CI_PIPELINE_SOURCE == "web" || $CI_PIPELINE_SOURCE == "schedule")'
    - if: '$CI_COMMIT_TAG =~ /.*-beta/'
  variables:
    VIEWER_CHANNEL_TYPE: Beta

.release_rules:
  rules:
    - if: '$BUILD_CHANNEL == "Release" && ($CI_PIPELINE_SOURCE == "web" || $CI_PIPELINE_SOURCE == "schedule")'
    - if: '$CI_COMMIT_TAG =~ /.*-release/'
  variables:
    VIEWER_CHANNEL_TYPE: Release

.build:master:linux64:
  extends:
    - .linux64_build
    - .master_rules

build:master:windows32:
  extends:
    - .win32_build
    - .master_rules

build:master:windows64:
  extends:
    - .win64_build
    - .master_rules

.build:project:linux64:
  extends:
    - .linux64_build
    - .project_rules

build:project:windows32:
  extends:
    - .win32_build
    - .project_rules

build:project:windows64:
  extends:
    - .win64_build
    - .project_rules

.build:beta:linux64:
  extends:
    - .linux64_build
    - .beta_rules

build:beta:windows32:
  extends:
    - .win32_build
    - .beta_rules

build:beta:windows64:
  extends:
    - .win64_build
    - .beta_rules

.build:release:linux64:
  extends:
    - .linux64_build
    - .release_rules

build:release:windows32:
  extends:
    - .win32_build
    - .release_rules

build:release:windows64:
  extends:
    - .win64_build
    - .release_rules

.upload_template:
  stage: upload
  tags:
    - autobuild
    - windows
  allow_failure: false
  script:
    - |
        $BuildData = Get-Content .\build-vc-64\newview\Release\build_data.json | ConvertFrom-Json
        $BuildChannelVersion = $BuildData."Channel" + ' ' + $BuildData."Version"
        $UploadDestViewerDir = $BuildChannelVersion.ToLower().Replace(" ", "/")
        $UploadDestURL = "https://pkg.alchemyviewer.org/repository/viewer/${UploadDestViewerDir}"

        $UploadParams = @{ UseBasicParsing = $true;
         Method = "PUT";
         Headers = @{
           ContentType = "application/x-executable";
           Authorization = "Basic $([System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("$env:AUTOBUILD_HTTP_USER`:$env:AUTOBUILD_HTTP_PASS")))"; };
         Verbose = $true; };

        Function try_upload($build_type, $file_pattern)
        {
          $build_newview = ".\build-${build_type}\newview\"
          If (Test-Path -LiteralPath $build_newview)
          {
            Push-Location $build_newview
              $FileName = Get-ChildItem -Path . -Name -Include $file_pattern
              Invoke-WebRequest @UploadParams -InFile .\$FileName -Uri "${UploadDestURL}/${FileName}"

              If (($env:VIEWER_USE_CRASHPAD -eq 'TRUE') -and ($build_type -eq "vc-64"))
              {
                sentry-cli upload-dif --include-sources singularity-bin.exe singularity-bin.pdb crashpad_handler.exe crashpad_handler.pdb fmod.dll libcrypto-1_1.dll libcrypto-1_1.pdb libssl-1_1.dll libssl-1_1.pdb libcrypto-1_1-x64.dll libcrypto-1_1-x64.pdb libssl-1_1-x64.dll libssl-1_1-x64.pdb vcruntime140.dll msvcp140.dll libhunspell.dll libhunspell.pdb glod.dll
              }
            Pop-Location
          }
        }

        try_upload("linux-64", "Singularity_*.tar.xz")
        try_upload("vc-64", "Singularity_*_Setup.exe")
        try_upload("vc-32", "Singularity_*_Setup.exe")

        If ($env:VIEWER_USE_CRASHPAD -eq 'TRUE')
        {
          sentry-cli releases new $BuildChannelVersion
          sentry-cli releases set-commits --auto $BuildChannelVersion
          sentry-cli releases finalize $BuildChannelVersion
        }

upload:project:
  extends:
    - .upload_template
  rules:
    - if: '$BUILD_CHANNEL == "Project" && ($CI_PIPELINE_SOURCE == "web" || $CI_PIPELINE_SOURCE == "schedule")'
      when: manual
    - if: '$CI_COMMIT_TAG =~ /.*-project/'
      when: manual
  environment:
    name: qa

upload:beta:
  extends:
    - .upload_template
  rules:
    - if: '$BUILD_CHANNEL == "Beta" && ($CI_PIPELINE_SOURCE == "web" || $CI_PIPELINE_SOURCE == "schedule")'
      when: manual
    - if: '$CI_COMMIT_TAG =~ /.*-beta/'
      when: manual
  environment:
    name: beta

upload:release:
  extends:
    - .upload_template
  rules:
    - if: '$BUILD_CHANNEL == "Release" && ($CI_PIPELINE_SOURCE == "web" || $CI_PIPELINE_SOURCE == "schedule")'
      when: manual
    - if: '$CI_COMMIT_TAG =~ /.*-release/'
      when: manual
  environment:
    name: release
