name: Build and release

on:
  push:
    tags: v*.*.*

env:
  CARGO_TERM_COLOR: always
  NAME: tlrc

defaults:
  run:
    shell: bash

permissions:
  contents: write

jobs:
  release:
    name: Create the release
    runs-on: ubuntu-latest

    steps:
      - name: Checkout the repository
        uses: actions/checkout@v6

      - name: Create the release
        env:
            GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: gh release create --title "$GITHUB_REF_NAME" "$GITHUB_REF_NAME"

  build:
    name: Build ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    needs: release
    permissions:
      contents: write # to upload assets to releases
      attestations: write # to upload assets attestation for build provenance
      id-token: write # grant additional permission to attestation action to mint the OIDC token permission

    strategy:
      fail-fast: false
      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-24.04-arm

          - target: aarch64-unknown-linux-musl
            os: ubuntu-24.04-arm

          - target: x86_64-apple-darwin
            os: macos-latest

          - target: aarch64-apple-darwin
            os: macos-latest

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

          - target: aarch64-pc-windows-msvc
            os: windows-latest

    steps:
      - name: Checkout the repository
        uses: actions/checkout@v6

      - name: Install musl libc
        if: ${{ contains(matrix.target, 'musl') }}
        run: sudo apt install musl-tools

      - name: Set up Rust
        run: rustup update stable && rustup target add ${{ matrix.target }}

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

      - name: Build the Debian package
        if: ${{ contains(matrix.target, 'linux') }}
        run: |
          cargo install cargo-deb --locked
          cargo deb --target ${{ matrix.target }} --output "$NAME-$GITHUB_REF_NAME-${{ matrix.target }}.deb"

      - name: Create the archive
        env:
          BIN: tldr
        run: |
          if [[ ${{ matrix.os }} == *windows* ]]; then
            BIN="$BIN.exe"
            ARCHIVER=(7z a)
            EXTENSION="zip"
          else
            ARCHIVER=(tar -czvf)
            EXTENSION="tar.gz"
          fi

          mv "target/${{ matrix.target }}/release/$BIN" "$BIN"
          "${ARCHIVER[@]}" "$NAME-$GITHUB_REF_NAME-${{ matrix.target }}.$EXTENSION" "$BIN" LICENSE README.md tldr.1 completions

      - name: Upload the archive
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: gh release upload "$GITHUB_REF_NAME" "$NAME"-*

      - name: Attest release files
        id: attest
        uses: actions/attest-build-provenance@v4
        with:
          subject-path: '*.zip, *.tar.gz, *.deb'
