### 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:
  release:
    runs-on: ubuntu-latest
    steps:
      # 1. checkout the repo
      - name: "checkout"
        uses: actions/checkout@v2

      # 2. 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

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

      # 4. create package and release notes
        ## get varibale ${{ steps.tag.outputs.tag }}
      - name: "get tag"
        id: tag
        uses: dawidd6/action-get-tag@v1
        with:
          # Optionally strip `v` prefix
          strip_v: false

      - name: "create tikz-trackschematic package"
        run: ./build.sh --non-interactive --release ${{ steps.tag.outputs.tag }}
      
      # 5. publish package as new release on github
      - 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: release-note-${{ steps.tag.outputs.tag }}.md
          draft: false
          prerelease: false
        
      - 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: tikz-trackschematic-${{ steps.tag.outputs.tag }}.zip
          asset_name: tikz-trackschematic-${{ steps.tag.outputs.tag }}.zip
          asset_content_type: application/zip

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

      # 6. upload new release to CTAN
      ## TODO