### github action to make and publish a release
##
name: release

## Controls when the workflow will run
on:
  ## Triggers the workflow on push or pull request events but only for the master branch
  push:
    tags:
      - "v*"

## A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  create_package:
    name: "create a TeX Live package for tikz-trackschematic"
    outputs:
      version: ${{ steps.tag.outputs.tag }}
    runs-on: ubuntu-latest
    steps:
      # 1. get varibale tag and put it in ${{ steps.tag.outputs.tag }}
      - name: "get tag"
        id: tag
        uses: dawidd6/action-get-tag@v1
        with:
          # Optionally strip `v` prefix
          strip_v: false

      # 2. checkout the repo
      - name: "checkout"
        uses: actions/checkout@v2

      # 3. install TeX Live
      - name: "install ghostscript"
        run: sudo apt-get install -y ghostscript
        
      - name: "setup TeX Live (via paolobrasolin)"
        uses: paolobrasolin/setup-texlive-action@v1
        with:
          profile-path:  ${{ github.workspace }}/.github/tex/profile.minimal.txt
          packages-path: ${{ github.workspace }}/.github/tex/packages.doc.txt

      # 4. (re-)compile the documentation
      - name: "update tikz-trackschematic documentation before release"
        run: ./build.sh --non-interactive --memory-increase --compile-doc

      # 5. create package and release notes
      - name: "create tikz-trackschematic package"
        run: ./build.sh --non-interactive --release ${{ steps.tag.outputs.tag }}

      # 6. upload artifact to share it with other jobs
      - uses: actions/upload-artifact@v3
        with:
          path: |
            tikz-trackschematic-${{ steps.tag.outputs.tag }}.zip
            release-note-${{ steps.tag.outputs.tag }}.md
          if-no-files-found: error # 'warn' or 'ignore' are also available, defaults to `warn`
      - uses: actions/upload-artifact@v3
        with:
          path: .github/tex/tikz-trackschematic.pkg
          if-no-files-found: error # 'warn' or 'ignore' are also available, defaults to `warn`
      - uses: actions/upload-artifact@v3
        with:
          path: .github/zenodo/metadata.json
          if-no-files-found: error # 'warn' or 'ignore' are also available, defaults to `warn`

  publish_github:
    needs: create_package
    name: "publish on github"
    runs-on: ubuntu-latest
    steps:
      # 1. download artifact in folder artifact/
      - uses: actions/download-artifact@v3

      # 2. creating a new release
      - name: "create release"
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
        with:
          tag_name: ${{ github.ref }}
          release_name: Release ${{ github.ref }}
          body_path: artifact/release-note-${{needs.create_package.outputs.version}}.md
          draft: false
          prerelease: false

      # 3. upload package to new release
      - name: "upload release asset"
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: artifact/tikz-trackschematic-${{needs.create_package.outputs.version}}.zip
          asset_name: tikz-trackschematic-${{needs.create_package.outputs.version}}.zip
          asset_content_type: application/zip

      # 4. publish release on github
      - name: "publish release"
        uses: StuYarrow/publish-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          id: ${{ steps.create_release.outputs.id }}

  publish_CTAN:
    needs: create_package
    name: "publish on CTAN"
    runs-on: ubuntu-latest
    steps:
      # 1. download artifact in folder artifact/ and move it one level up
      - uses: actions/download-artifact@v3
      - run: |
          mv ./artifact/tikz-trackschematic-${{needs.create_package.outputs.version}}.zip ./
      
      # 2. install ctan-o-mat
      - name: "setup TeX Live (via paolobrasolin)"
        uses: paolobrasolin/setup-texlive-action@v1
        with:
          profile-path:  ${{ github.workspace }}/.github/tex/profile.minimal.txt
          packages-path: ${{ github.workspace }}/.github/tex/packages.upload.txt

      # 3. upload new release to CTAN
      - name: CTAN submit
        run: ctan-o-mat --verbose --submit artifact/tikz-trackschematic.pkg

  publish_zenodo:
    needs: create_package
    name: "publish on zenodo"
    outputs:
      doi: ${{ steps.zenodraft.outputs.doi }}
    runs-on: ubuntu-latest
    steps:
      # 1. download artifact in folder artifact/ and move it one level up
      - uses: actions/download-artifact@v3
      - run: |
          mv ./artifact/tikz-trackschematic-${{needs.create_package.outputs.version}}.zip ./

      # 2. install zenodraft
      - name: "install zenodraft"
        run: npm install -g zenodraft

      # 3. upload new release to zenodo
      - name: "uploading to zenodo"
        id: zenodraft
        env:
          ZENODO_ACCESS_TOKEN: ${{ secrets.ZENODO_ACCESS_TOKEN }}
          COLLECTION: 5539844
        run: |
          ID=$(zenodraft deposition create in-existing-collection $COLLECTION)
          zenodraft file add $ID tikz-trackschematic-*.zip
          zenodraft metadata update $ID artifact/metadata.json
          zenodraft deposition publish $ID
          echo "::set-output name=doi::$(zenodraft deposition show prereserved $ID)"

  update_citation:
    needs: [create_package, publish_zenodo]
    name: "updating CITATION.cff"
    runs-on: ubuntu-latest
    steps:
      # 1. checkout the repo
      - name: "checkout"
        uses: actions/checkout@v2
      
      # 2. update CITATION.cff
      - run: ./build.sh --update-cite ${{needs.publish_zenodo.outputs.doi}}
      
      # 3. push the change back to master
      - name: push
        uses: github-actions-x/commit@v2.8
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          push-branch: 'master'
          force-add: 'true'
          files: CITATION.cff
          commit-message: 'DOI updated to ${{needs.create_package.outputs.version}} (via github action)'
          name: Martin Scheidt
          email: m.scheidt@tu-bs.de
