name: Releases

on:
  push:
    tags:
      - 'v*.*'  # Only run on version tags

jobs:
  set_versions:
    runs-on: ubuntu-latest
    outputs:
      ff_version: ${{ steps.get_version.outputs.ff_version }}
      ubuntu_versions: ${{ steps.set_os_versions.outputs.ubuntu_versions }}
      windows_versions: ${{ steps.set_os_versions.outputs.windows_versions }}
      is_master: ${{ steps.check_branch.outputs.is_master }}

    steps:
      - uses: actions/checkout@v6

      - name: Extract version from configure.ac
        id: get_version
        run: |
          VERSION=$(grep AC_INIT configure.ac | cut -d"," -f2 | cut -d"[" -f2 | cut -d"]" -f1)
          echo "ff_version=$VERSION" >> $GITHUB_OUTPUT
          echo "Extracted FreeFEM version: $VERSION"

      - name: Set OS versions
        id: set_os_versions
        run: |
          echo "ubuntu_versions=[22.04, 24.04]" >> $GITHUB_OUTPUT
          echo "windows_versions=[2025]" >> $GITHUB_OUTPUT

      - name: Check if tag is on master
        id: check_branch
        run: |
          git fetch origin master
          if git branch -a --contains ${{ github.sha }} | grep -q 'origin/master'; then
            echo "is_master=true" >> $GITHUB_OUTPUT
            echo "Tag is on master branch - will create a release"
          else
            echo "is_master=false" >> $GITHUB_OUTPUT
            echo "Tag is NOT on master - will create a pre-release"
          fi

  get_deb_packages:
    needs: set_versions
    uses: ./.github/workflows/release_deb.yml
    with:
      release_version: ${{ needs.set_versions.outputs.ff_version }}
      ubuntu_versions: ${{ needs.set_versions.outputs.ubuntu_versions }}

  get_exe_packages:
    needs: set_versions
    uses: ./.github/workflows/full-msmpi.yml
    with:
      windows_versions: ${{ needs.set_versions.outputs.windows_versions }}
      build_types: '["release"]'
      skip_check: true
      upload_build_artifacts: true

  create_release:
    needs: [set_versions, get_deb_packages, get_exe_packages]
    runs-on: ubuntu-latest
    permissions:
      contents: write # Required to create releases
    steps:
      - name: Download all assets
        uses: actions/download-artifact@v4
        with:
          path: ./release-assets
          merge-multiple: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          files: |
            ./release-assets/*.deb
            ./release-assets/*.exe
          draft: false
          prerelease: ${{ needs.set_versions.outputs.is_master == 'false' }}
          tag_name: ${{ github.ref_name }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  check_release:
    needs: [set_versions, create_release]
    uses: ./.github/workflows/release_check.yml
    with:
      release_version: ${{ github.ref_name }}
      windows_versions: ${{ needs.set_versions.outputs.windows_versions }}
      ubuntu_versions: ${{ needs.set_versions.outputs.ubuntu_versions }}
