name: release

on:
  push:
    tags: ["v*"]

concurrency:
  group: "pages"
  cancel-in-progress: false

env:
  CARGO_TERM_COLOR: always

jobs:
  create-release:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    outputs:
      version: ${{ steps.version.outputs.version }}
    steps:
      - uses: actions/checkout@v6

      - name: Extract version from tag
        id: version
        run: echo "version=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT

      - name: Create or update release
        uses: softprops/action-gh-release@v2
        with:
          generate_release_notes: true
          draft: false

  build:
    needs: create-release
    permissions:
      contents: write
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: aarch64-apple-darwin
            os: macos-latest
            archive: tar.gz

          - target: x86_64-pc-windows-msvc
            os: windows-latest
            archive: zip

          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            archive: tar.gz

    runs-on: ${{ matrix.os }}
    env:
      VERSION: ${{ needs.create-release.outputs.version }}
      TARGET: ${{ matrix.target }}
    steps:
      - uses: actions/checkout@v6

      - name: Set version in Cargo files
        shell: bash
        run: |
          if [[ "$RUNNER_OS" == "macOS" ]]; then
            sed -i '' "s/0\.0\.999/$VERSION/" Cargo.toml Cargo.lock
          else
            sed -i "s/0\.0\.999/$VERSION/" Cargo.toml Cargo.lock
          fi

      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}

      - name: Cache cargo
        uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.target }}

      - name: Build release binary
        run: cargo build --release --target ${{ matrix.target }}

      - name: Create tar.gz archive
        if: matrix.archive == 'tar.gz'
        shell: bash
        run: |
          cd target/$TARGET/release
          tar -czvf ../../../mosox-$VERSION-$TARGET.tar.gz mosox

      - name: Create zip archive
        if: matrix.archive == 'zip'
        shell: pwsh
        run: |
          Compress-Archive -Path "target/$env:TARGET/release/mosox.exe" -DestinationPath "mosox-$env:VERSION-$env:TARGET.zip"

      - name: Upload release assets
        uses: softprops/action-gh-release@v2
        with:
          files: mosox-${{ env.VERSION }}-${{ matrix.target }}.${{ matrix.archive }}

  publish:
    needs: [create-release, build]
    runs-on: ubuntu-latest
    environment: release
    permissions:
      contents: write
      id-token: write
    steps:
      - uses: actions/checkout@v6

      - name: Set version in Cargo files
        run: |
          sed -i "s/0\.0\.999/${{ needs.create-release.outputs.version }}/" Cargo.toml Cargo.lock

      - uses: rust-lang/crates-io-auth-action@v1
        id: auth

      - run: cargo publish --allow-dirty
        env:
          CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }}

  pages:
    needs: [create-release, build]
    runs-on: ubuntu-latest
    permissions:
      pages: write
      id-token: write
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    steps:
      - uses: actions/checkout@v6
      - name: Interpolate version into HTML
        env:
          VERSION: ${{ needs.create-release.outputs.version }}
        run: |
          sed -i "s|__VERSION__|$VERSION|g" site/index.html
          sed -i "s|__MACOS_URL__|https://github.com/carderne/mosox/releases/download/v$VERSION/mosox-$VERSION-aarch64-apple-darwin.tar.gz|g" site/index.html
          sed -i "s|__WINDOWS_URL__|https://github.com/carderne/mosox/releases/download/v$VERSION/mosox-$VERSION-x86_64-pc-windows-msvc.zip|g" site/index.html
          sed -i "s|__LINUX_URL__|https://github.com/carderne/mosox/releases/download/v$VERSION/mosox-$VERSION-x86_64-unknown-linux-gnu.tar.gz|g" site/index.html
      - uses: actions/configure-pages@v5
      - uses: actions/upload-pages-artifact@v3
        with:
          path: site
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4
