name: Release

on:
  workflow_dispatch:
  issue_comment:
    types: [ created ]

jobs:
  build_and_release:
    name: Build and release
    runs-on: ubuntu-latest
    container: devkitpro/devkita64:latest
    if: github.event_name == 'workflow_dispatch' || contains(github.event.comment.body, '/release-action')

    steps:
      - name: Checkout latest code
        uses: actions/checkout@v5
        with:
          ref: master
          clean: true
          fetch-depth: 0
          fetch-tags: true
          submodules: recursive
      - name: Setup ENV parameters
        run: |
          VER_FILE=Makefile
          VERSION=$(awk '/^APLVERSION_MAJOR/{print $3}' $VER_FILE).$(awk '/^APLVERSION_MINOR/{print $3}' $VER_FILE).$(awk '/^APLVERSION_BUGFX/{print $3}' $VER_FILE)
          echo "TAG=${VERSION}" >> "${GITHUB_ENV}"
          echo "RELEASE_NAME=AllgemeinerProblemLoeser ${VERSION}" >> "${GITHUB_ENV}"
        shell: bash
      - name: Build
        run: |
          export DEVKITPRO=/opt/devkitpro
          make all
        shell: bash
      - name: Upload Release Asset
        uses: softprops/action-gh-release@v2.0.9
        with:
          name: ${{ env.RELEASE_NAME }}
          tag_name: ${{ env.TAG }}
          draft: false
          prerelease: false
          generate_release_notes: yes
          make_latest: true
          files: |
            output/APL.bin
