name: Release

on:
  push:
    tags:
      - "v*"

permissions:
  contents: write

env:
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true

jobs:
  build:
    name: Build — ${{ matrix.label }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            label: Linux amd64
            binary_name: mdcms
            artifact_name: mdcms-linux-amd64
            make_deb: true

          - os: macos-latest
            label: macOS arm64
            binary_name: mdcms
            artifact_name: mdcms-macos-arm64
            make_deb: false

          - os: windows-latest
            label: Windows amd64
            binary_name: mdcms.exe
            artifact_name: mdcms-windows-amd64
            make_deb: false

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      - name: Install dependencies
        run: pip install pyinstaller click pyyaml certifi

      - name: Build binary
        run: pyinstaller --onefile --name mdcms mdcms.py

      - name: Rename binary (non-Windows)
        if: matrix.os != 'windows-latest'
        run: mv dist/mdcms dist/${{ matrix.artifact_name }}

      - name: Rename binary (Windows)
        if: matrix.os == 'windows-latest'
        run: mv dist/mdcms.exe dist/${{ matrix.artifact_name }}.exe

      - name: Build .deb (Linux only)
        if: matrix.make_deb
        run: |
          VERSION=${GITHUB_REF_NAME#v}
          sudo gem install fpm --no-document
          fpm \
            -s dir -t deb \
            -n mdcms \
            -v "$VERSION" \
            --description "MD-CMS companion CLI — manage and build MD-CMS sites" \
            --url "https://github.com/kbenestad/mdcms" \
            --maintainer "Kristian Benestad" \
            --license "Apache-2.0" \
            --architecture amd64 \
            --category utils \
            dist/mdcms-linux-amd64=/usr/local/bin/mdcms

      - name: Upload binary artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.artifact_name }}
          path: |
            dist/${{ matrix.artifact_name }}
            dist/${{ matrix.artifact_name }}.exe
          if-no-files-found: ignore

      - name: Upload .deb artifact
        if: matrix.make_deb
        uses: actions/upload-artifact@v4
        with:
          name: deb-package
          path: "*.deb"

  release:
    name: Create GitHub Release
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts
          merge-multiple: false

      - name: List artifacts
        run: find artifacts -type f

      - name: Create release
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          PRERELEASE=""
          if [[ "${{ github.ref_name }}" == *"-"* ]]; then
            PRERELEASE="--prerelease"
          fi
          gh release delete "${{ github.ref_name }}" --repo "${{ github.repository }}" --yes 2>/dev/null || true
          gh release create "${{ github.ref_name }}" \
            --repo "${{ github.repository }}" \
            --title "mdcms ${{ github.ref_name }}" \
            --generate-notes \
            $PRERELEASE \
            artifacts/mdcms-linux-amd64/mdcms-linux-amd64 \
            artifacts/mdcms-macos-arm64/mdcms-macos-arm64 \
            artifacts/mdcms-windows-amd64/mdcms-windows-amd64.exe \
            artifacts/deb-package/*.deb
