#
# This runs when version tag is pushed
#

name: REL

on:
  push:
    tags: ["v[0-9]*"]

jobs:
  sdist:
    name: "Build source package"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with: {python-version: "3.11"}
      - run: python3 -m pip install -r etc/requirements.build.txt --disable-pip-version-check
      - run: python3 setup.py sdist
      - run: python3 setup.py bdist_wheel
      - uses: actions/upload-artifact@v4
        with: {name: "dist", path: "dist"}

  publish:
    name: "Publish"
    runs-on: ubuntu-latest
    needs: [sdist]
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with: {python-version: "3.11"}

      - run: python3 -m pip install -r etc/requirements.build.txt --disable-pip-version-check

      - name: "Get files"
        uses: actions/download-artifact@v4
        with: {name: "dist", path: "dist"}

      - name: "Install pandoc"
        run: |
          sudo -nH apt-get -u -y install pandoc
          pandoc --version

      - name: "Prepare"
        run: |
          PACKAGE=$(python3 setup.py --name)
          VERSION=$(python3 setup.py --version)
          TGZ="${PACKAGE}-${VERSION}.tar.gz"

          # default - gh:release, pypi
          # PRERELEASE -  gh:prerelease, pypi
          # DRAFT - gh:draft,prerelease, testpypi
          PRERELEASE="false"; DRAFT="false"
          case "${VERSION}" in
            *[ab]*|*rc*) PRERELEASE="true";;
            *dev*) PRERELEASE="true"; DRAFT="true";;
          esac

          test "${{github.ref}}" = "refs/tags/v${VERSION}" || { echo "ERR: tag mismatch"; exit 1; }
          test -f "dist/${TGZ}" || { echo "ERR: sdist failed"; exit 1; }
          echo "PACKAGE=${PACKAGE}" >> $GITHUB_ENV
          echo "VERSION=${VERSION}" >> $GITHUB_ENV
          echo "TGZ=${TGZ}" >> $GITHUB_ENV
          echo "PRERELEASE=${PRERELEASE}" >> $GITHUB_ENV
          echo "DRAFT=${DRAFT}" >> $GITHUB_ENV
          mkdir -p tmp
          make -s shownote > tmp/note.md
          cat tmp/note.md
          ls -l dist

      - name: "Create Github release"
        env:
          GH_TOKEN: ${{secrets.GITHUB_TOKEN}}
        run: |
          title="${PACKAGE} v${VERSION}"
          ghf="--notes-file=./tmp/note.md"
          if test "${DRAFT}" = "true"; then ghf="${ghf} --draft"; fi
          if test "${PRERELEASE}" = "true"; then ghf="${ghf} --prerelease"; fi
          gh release create "v${VERSION}" "dist/${TGZ}" --title="${title}" ${ghf}

      - name: "Upload to PYPI"
        id: pypi_upload
        env:
          PYPI_TOKEN: ${{secrets.PYPI_TOKEN}}
          PYPI_TEST_TOKEN: ${{secrets.PYPI_TEST_TOKEN}}
        run: |
          ls -l dist
          if test "${DRAFT}" = "false"; then
            python -m twine upload -u __token__ -p ${PYPI_TOKEN} \
              --repository pypi --disable-progress-bar dist/*
          else
            python -m twine upload -u __token__ -p ${PYPI_TEST_TOKEN} \
              --repository testpypi --disable-progress-bar dist/*
          fi

