name: Release

on:
  push:
    tags:
      - "v*"
  workflow_dispatch:

jobs:
  build:
    name: Build release artifacts
    runs-on: ubuntu-latest
    container:
      image: archlinux:latest
    env:
      CARGO_TERM_COLOR: always
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Initialise pacman
        run: |
          pacman-key --init
          pacman-key --populate archlinux
          pacman -Syu --noconfirm

      - name: Install build dependencies
        run: |
          pacman -S --noconfirm --needed base-devel rust pkgconf openssl git pacman-contrib fzf cmake jq restic

      - name: Rust cache
        uses: Swatinem/rust-cache@v2

      - name: Create build user
        run: |
          useradd -m builder
          chown -R builder:builder "$PWD"

      - name: Extract version
        id: vars
        run: |
          version=$(awk -F '"' '/^version =/ {print $2; exit}' Cargo.toml)
          if [[ -z "$version" ]]; then
            echo "Failed to determine version from Cargo.toml" >&2
            exit 1
          fi
          tag_version="${GITHUB_REF_NAME#v}"
          if [[ -n "$tag_version" && "$tag_version" != "$version" ]]; then
            echo "Tag version $tag_version does not match Cargo.toml version $version" >&2
            exit 1
          fi
          echo "version=$version" >> "$GITHUB_OUTPUT"

      - name: Run tests
        run: |
          WORKSPACE="$PWD"
          su builder -c "cd '$WORKSPACE' && CARGO_TERM_COLOR=always cargo test --locked"

      - name: Build release binary
        run: |
          WORKSPACE="$PWD"
          su builder -c "cd '$WORKSPACE' && CARGO_TERM_COLOR=always cargo build --release --locked"

      - name: Strip binary
        run: |
          WORKSPACE="$PWD"
          su builder -c "cd '$WORKSPACE' && strip target/release/ins"

      - name: Collect artifacts
        env:
          VERSION: ${{ steps.vars.outputs.version }}
        run: |
          mkdir -p artifacts
          install -Dm0755 "target/release/ins" "artifacts/ins-${VERSION}-x86_64-unknown-linux-gnu"

      - name: Upload compiled binary
        uses: actions/upload-artifact@v4
        with:
          name: ins-${{ steps.vars.outputs.version }}-x86_64-unknown-linux-gnu
          path: artifacts/ins-${{ steps.vars.outputs.version }}-x86_64-unknown-linux-gnu

  pkgbuild:
    name: Build Arch package
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Extract version
        id: vars
        run: |
          version=$(awk -F '"' '/^version =/ {print $2; exit}' Cargo.toml)
          if [[ -z "$version" ]]; then
            echo "Failed to determine version from Cargo.toml" >&2
            exit 1
          fi
          echo "version=$version" >> "$GITHUB_OUTPUT"

      - name: Build package
        uses: 2m/arch-pkgbuild-builder@v1.16
        with:
          target: pkgbuild
          pkgname: pkgbuild/ins

      - name: Collect package artifacts
        env:
          VERSION: ${{ steps.vars.outputs.version }}
        run: |
          mkdir -p artifacts
          shopt -s nullglob
          for pkg in pkgbuild/ins/*.pkg.tar.zst; do mv "$pkg" artifacts/; done
          for src in pkgbuild/ins/*.src.tar.zst; do mv "$src" artifacts/; done

      - name: Upload Arch package
        uses: actions/upload-artifact@v4
        with:
          name: ins-${{ steps.vars.outputs.version }}-arch-packages
          path: artifacts/

  release:
    name: Publish GitHub release
    runs-on: ubuntu-latest
    needs:
      - build
      - pkgbuild
    permissions:
      contents: write
    steps:
      - name: Download artifacts
        uses: actions/download-artifact@v4
        with:
          path: dist
          merge-multiple: true

      - name: Publish release
        uses: softprops/action-gh-release@v2
        with:
          files: dist/**/*
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
