name: build

on:
  workflow_dispatch:
  push:
    tags:
      - "[0-9]+.[0-9]+.[0-9]+"

jobs:
  build-release:
    runs-on: ubuntu-24.04
    strategy:
      matrix:
        target:
          - x86_64-unknown-linux-gnu
    name: ${{ matrix.target }}
    env:
      SHELL_COMPLETIONS_DIR: completions
    steps:
      - uses: actions/checkout@v6

      - name: Extract crate information
        id: info
        shell: bash
        run: |
          PKG_VERSION=$(sed -n 's/^version = "\(.*\)"/\1/p' Cargo.toml | head -n1)
          echo ::set-output name=PKG_VERSION::${PKG_VERSION}

      - name: Check Cargo.toml version against tag
        shell: bash
        if: startsWith(github.ref, 'refs/tags/')
        run: |
          TAG_VERSION="${GITHUB_REF#refs/tags/}"
          CARGO_VERSION="${{ steps.info.outputs.PKG_VERSION }}"

          if [[ ${TAG_VERSION} != "${CARGO_VERSION}" ]]; then
            echo "Cargo.toml version differs from the current tag!"
            echo "Cargo.toml: ${CARGO_VERSION}"
            echo "tag:        ${TAG_VERSION}"
            exit 1
          fi

      - name: Install prerequisites
        shell: bash
        run: |
          sudo add-apt-repository -y ppa:strukturag/libheif
          sudo apt-get update
          sudo apt-get install -y --no-install-recommends libheif-dev libheif1 libsharpyuv-dev

      - name: Install rust toolchain
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          target: ${{ matrix.target }}

      - name: Show version information
        shell: bash
        run: |
          rustup -V
          rustup toolchain list
          rustup default
          cargo -V
          rustc -V

      - name: Create completions dir
        run: mkdir -p "${SHELL_COMPLETIONS_DIR}"

      - name: Build the binary
        uses: actions-rs/cargo@v1
        with:
          command: build
          args: --release --locked --target=${{ matrix.target }}

      - name: Strip the binary
        run: strip "target/${{ matrix.target }}/release/timewall"

      - name: Build the archive artifact
        id: build
        shell: bash
        run: |
          PKG_NAME="timewall-${{ steps.info.outputs.PKG_VERSION }}-${{ matrix.target }}"
          PKG_STAGING="${PWD}/${PKG_NAME}"
          mkdir -p "${PKG_STAGING}"

          cp "target/${{ matrix.target }}/release/timewall" "${PKG_STAGING}"
          cp -r "${SHELL_COMPLETIONS_DIR}" "${PKG_STAGING}"
          cp {README.md,LICENSE,timewall.service} "${PKG_STAGING}"

          PKG_ARTIFACT_NAME="${PKG_NAME}.tar.gz"
          PKG_ARTIFACT_PATH="${PWD}/${PKG_ARTIFACT_NAME}"
          cd "${PKG_STAGING}"
          tar -czf "${PKG_ARTIFACT_PATH}" *

          echo ::set-output name=PKG_ARTIFACT_NAME::${PKG_ARTIFACT_NAME}
          echo ::set-output name=PKG_ARTIFACT_PATH::${PKG_ARTIFACT_PATH}

      - name: Upload the artifact
        uses: actions/upload-artifact@v6
        with:
          name: ${{ steps.build.outputs.PKG_ARTIFACT_NAME }}
          path: ${{ steps.build.outputs.PKG_ARTIFACT_PATH }}

      - name: Release the artifact
        uses: softprops/action-gh-release@v2
        if: startsWith(github.ref, 'refs/tags/')
        with:
          draft: true
          files: ${{ steps.build.outputs.PKG_ARTIFACT_PATH }}

      - name: Clean build artifacts
        if: startsWith(github.ref, 'refs/tags/')
        run: git checkout -- . && git clean -fd

      - name: Publish to crates.io
        if: startsWith(github.ref, 'refs/tags/')
        run: cargo publish --locked
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
