name: Release

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: write

jobs:
  publish-crates-io:
    name: Publish to Crates.io
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install System Dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y libwayland-dev libdbus-1-dev libgtk-3-dev pkg-config
      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
      - name: Cargo Publish
        run: cargo publish --allow-dirty
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

  create-release:
    name: Create GitHub Release
    runs-on: ubuntu-latest
    outputs:
      id: ${{ steps.create_release.outputs.id }}
    steps:
      - uses: actions/checkout@v4
      - name: Create Release
        id: create_release
        uses: softprops/action-gh-release@v2
        with:
          name: Fount v${{ github.ref_name }}
          body_path: CHANGELOG.md
          draft: false
          prerelease: false
          token: ${{ secrets.GITHUB_TOKEN }}

  upload-assets:
    name: ${{ matrix.target }}
    needs: [publish-crates-io, create-release]
    strategy:
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
          - target: x86_64-unknown-linux-musl
            os: ubuntu-latest
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest
          - target: universal-apple-darwin
            os: macos-latest
          - target: x86_64-pc-windows-msvc
            os: windows-latest
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
      - name: Install System Dependencies (Linux)
        if: matrix.os == 'ubuntu-latest'
        run: |
          sudo apt-get update
          sudo apt-get install -y libwayland-dev libdbus-1-dev libgtk-3-dev pkg-config musl-tools
      - name: Prepare Portable Instructions
        shell: bash
        run: |
          if [[ "${{ matrix.os }}" == "windows-latest" ]]; then
            cp docs/readme/windows.txt PORTABLE.txt
          elif [[ "${{ matrix.os }}" == "macos-latest" ]]; then
            cp docs/readme/macos.txt PORTABLE.txt
          else
            cp docs/readme/linux.txt PORTABLE.txt
          fi
      - name: Upload Binary
        uses: taiki-e/upload-rust-binary-action@v1
        with:
          bin: fount
          target: ${{ matrix.target }}
          include: LICENSE,PORTABLE.txt
          tar: unix
          zip: windows
          token: ${{ secrets.GITHUB_TOKEN }}
