# To contribute improvements to CI/CD templates, please follow the Development guide at:
# https://docs.gitlab.com/ee/development/cicd/templates.html
# This specific template is located at:
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/LaTeX.gitlab-ci.yml

---
#variables:
  # Feel free to choose the image that suits you best.
  # blang/latex:latest ... Former image used in this template. No longer maintained by author.
  # listx/texlive:2020 ... The default, referring to TexLive 2020. Current at least to 2021-02-02.

  # Additional alternatives with high Docker pull counts:
  # thomasweise/docker-texlive-full
  # thomasweise/texlive
  # adnrv/texlive
#  LATEX_IMAGE: listx/texlive:2020

stages:
  - build
  - release

build-windows:
  stage: build
  tags:
    - windows
    - latex
  before_script:
    - echo $CI_JOB_ID
    # Writing GE_JOB_ID variable to environment file, will need the value in the next stage.
    - Add-Content -Path variables.env -Value "BUILD_JOB_ID=$CI_JOB_ID"
    - Add-Content -Path variables.env -Value "CI_COMMIT_TAG=$CI_COMMIT_TAG"
  script:
    # prepare all possible variants of the thesis
    - (Get-Content ./Example.tex -Raw) -Replace 'ThesisType.=.{\w*}', 'ThesisType = {SEM}' | Set-Content ./Example_SEM.tex
    #- (Get-Content ./Example.tex -Raw) -Replace 'ThesisType.=.{\w*}', 'ThesisType = {PA}' | Set-Content ./Example_PA.tex
    #- (Get-Content ./Example.tex -Raw) -Replace 'ThesisType.=.{\w*}', 'ThesisType = {BA}' | Set-Content ./Example_BA.tex
    #- (Get-Content ./Example.tex -Raw) -Replace 'ThesisType.=.{\w*}', 'ThesisType = {MA}' | Set-Content ./Example_MA.tex
    #- (Get-Content ./Example.tex -Raw) -Replace 'ThesisType.=.{\w*}', 'ThesisType = {PHD}' | Set-Content ./Example_PHD.tex
    # remove Example.tex - no need to compile it now
    - rm ./Example.tex
    - rm ./Example.pdf
    # compile all .tex files
    - latexmk -pdf
  artifacts:
    paths:
      - "*.pdf"
    reports:
      # To ensure we've access to this file in the next stage
      dotenv: variables.env

release-windows:
  stage: release
  tags:
    - windows
    - latex
  needs:
    - job: build-windows
      artifacts: true
  #rules: 
  #  - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH # only do a release on commits or merges to the master branch 
  variables:
    TAG: '$CI_COMMIT_SHORT_SHA'
  script:
    - echo "Create Release $TAG from Artifacts of the $BUILD_JOB_ID job"
    - echo ${BUILD_JOB_ID}  
#typeotheroo >
    - >
      release-cli --insecure-https create --name  "Release $TAG" --description "Release $TAG" --tag-name "$TAG"
      --assets-links-name "Example_Seminararbeit"
      --assets-links-url "https://git.ift.tuwien.ac.at/lab/pub/ift-latex/-/jobs/${BUILD_JOB_ID}/artifacts/file/Example_SEM.pdf"
        #othe
        #--assets-link '{\"name\":\"Example Projektarbeit\",\"url\":\"https://git.ift.tuwien.ac.at/lab/pub/ift-latex/-/jobs/${BUILD_JOB_ID}/artifacts/file/Example_PA.pdf\",\"link_type\":\"other\"}' `
        #--assets-link '{\"name\":\"Example Bachelor Thesis\",\"url\":\"https://git.ift.tuwien.ac.at/lab/pub/ift-latex/-/jobs/${BUILD_JOB_ID}/artifacts/file/Example_BA.pdf\",\"link_type\":\"other\"}' `
        #--assets-link '{\"name\":\"Example Masters Thesis\",\"url\":\"https://git.ift.tuwien.ac.at/lab/pub/ift-latex/-/jobs/${BUILD_JOB_ID}/artifacts/file/Example_MA.pdf\",\"link_type\":\"other\"}' `
        #--assets-link '{\"name\":\"Example PHD Thesis\",\"url\":\"https://git.ift.tuwien.ac.at/lab/pub/ift-latex/-/jobs/${BUILD_JOB_ID}/artifacts/file/Example_PHD.pdf\",\"link_type\":\"other\"}'

    #FIXME: insecure https - solution from https://gitlab.com/gitlab-org/release-cli/-/issues/47
    #FIXME: multiline looks awful
  only:
  #  - tags
  #   - dev_CICD
  #  - master


