name: Release
on:
  push:
    tags:
      - "v*"
  workflow_dispatch:
jobs:
  build:
    name: Build release artifacts
    runs-on: blacksmith-4vcpu-ubuntu-2404
    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 unzip python3
      - name: Rust cache
        uses: Swatinem/rust-cache@v2
      - name: Create build user
        run: |
          useradd -m builder
          chown -R builder:builder "$PWD"
      - name: Determine target triple
        id: target
        run: |
          target=$(rustc -vV | awk '/^host:/ {print $2}')
          if [[ -z "$target" ]]; then
            echo "Failed to determine host target triple" >&2
            exit 1
          fi
          echo "triple=$target" >> "$GITHUB_OUTPUT"
      - 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 }}
          TARGET: ${{ steps.target.outputs.triple }}
        run: |
          mkdir -p artifacts
          pkg_dir="ins-${TARGET}-v${VERSION}"
          tmpdir=$(mktemp -d)
          mkdir -p "$tmpdir/$pkg_dir"
          install -Dm0755 "target/release/ins" "$tmpdir/$pkg_dir/ins"
          tar -czf "artifacts/${pkg_dir}.tgz" -C "$tmpdir" "$pkg_dir"
          sha256sum "artifacts/${pkg_dir}.tgz" > "artifacts/${pkg_dir}.tgz.sha256"
          rm -rf "$tmpdir"
      - name: Build AppImage
        env:
          VERSION: ${{ steps.vars.outputs.version }}
        run: |
          mkdir -p artifacts
          WORKSPACE="$PWD"
          su builder -c "cd '$WORKSPACE' && chmod +x utils/build_appimage.sh && ./utils/build_appimage.sh"
          install -Dm0644 "target/appimage/InstantCLI-${VERSION}-x86_64.AppImage" "artifacts/ins-${VERSION}-x86_64.AppImage"
          sha256sum "artifacts/ins-${VERSION}-x86_64.AppImage" > "artifacts/ins-${VERSION}-x86_64.AppImage.sha256"
      - name: Upload release package
        uses: actions/upload-artifact@v4
        with:
          name: ins-${{ steps.vars.outputs.version }}-${{ steps.target.outputs.triple }}
          path: |
            artifacts/ins-${{ steps.target.outputs.triple }}-v${{ steps.vars.outputs.version }}.tgz
            artifacts/ins-${{ steps.target.outputs.triple }}-v${{ steps.vars.outputs.version }}.tgz.sha256
            artifacts/ins-${{ steps.vars.outputs.version }}-x86_64.AppImage
            artifacts/ins-${{ steps.vars.outputs.version }}-x86_64.AppImage.sha256
  cross-compile:
    name: Cross-compile for ARM targets
    runs-on: blacksmith-4vcpu-ubuntu-2404
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - 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: Cross compile for ARM64 Linux
        uses: houseabsolute/actions-rust-cross@v1
        with:
          command: build
          target: aarch64-unknown-linux-gnu
          args: "--locked --release --features cross-compile"
          strip: true
      - name: Cross compile for ARMv7 Linux
        uses: houseabsolute/actions-rust-cross@v1
        with:
          command: build
          target: armv7-unknown-linux-gnueabihf
          args: "--locked --release --features cross-compile"
          strip: true
      - name: Install cargo-ndk
        run: cargo install cargo-ndk
      - name: Add Android target
        run: rustup target add aarch64-linux-android
      - name: Build for Android
        run: cargo ndk -t aarch64-linux-android build --release --locked --features cross-compile
      - name: Collect ARM artifacts
        env:
          VERSION: ${{ steps.vars.outputs.version }}
        run: |
          mkdir -p artifacts

          # ARM64 Linux
          pkg_dir_aarch64="ins-aarch64-unknown-linux-gnu-v${VERSION}"
          tmpdir_aarch64=$(mktemp -d)
          mkdir -p "$tmpdir_aarch64/$pkg_dir_aarch64"
          install -Dm0755 "target/aarch64-unknown-linux-gnu/release/ins" "$tmpdir_aarch64/$pkg_dir_aarch64/ins"
          tar -czf "artifacts/${pkg_dir_aarch64}.tgz" -C "$tmpdir_aarch64" "$pkg_dir_aarch64"
          sha256sum "artifacts/${pkg_dir_aarch64}.tgz" > "artifacts/${pkg_dir_aarch64}.tgz.sha256"
          rm -rf "$tmpdir_aarch64"

          # ARMv7 Linux
          pkg_dir_armv7="ins-armv7-unknown-linux-gnueabihf-v${VERSION}"
          tmpdir_armv7=$(mktemp -d)
          mkdir -p "$tmpdir_armv7/$pkg_dir_armv7"
          install -Dm0755 "target/armv7-unknown-linux-gnueabihf/release/ins" "$tmpdir_armv7/$pkg_dir_armv7/ins"
          tar -czf "artifacts/${pkg_dir_armv7}.tgz" -C "$tmpdir_armv7" "$pkg_dir_armv7"
          sha256sum "artifacts/${pkg_dir_armv7}.tgz" > "artifacts/${pkg_dir_armv7}.tgz.sha256"
          rm -rf "$tmpdir_armv7"

          # Android ARM64 (Termux)
          pkg_dir_termux="ins-aarch64-termux-v${VERSION}"
          tmpdir_termux=$(mktemp -d)
          mkdir -p "$tmpdir_termux/$pkg_dir_termux"
          install -Dm0755 "target/aarch64-linux-android/release/ins" "$tmpdir_termux/$pkg_dir_termux/ins"
          tar -czf "artifacts/${pkg_dir_termux}.tgz" -C "$tmpdir_termux" "$pkg_dir_termux"
          sha256sum "artifacts/${pkg_dir_termux}.tgz" > "artifacts/${pkg_dir_termux}.tgz.sha256"
          rm -rf "$tmpdir_termux"
      - name: Upload ARM artifacts
        uses: actions/upload-artifact@v4
        with:
          name: ins-${{ steps.vars.outputs.version }}-arm-binaries
          path: |
            artifacts/ins-aarch64-unknown-linux-gnu-v${{ steps.vars.outputs.version }}.tgz
            artifacts/ins-aarch64-unknown-linux-gnu-v${{ steps.vars.outputs.version }}.tgz.sha256
            artifacts/ins-armv7-unknown-linux-gnueabihf-v${{ steps.vars.outputs.version }}.tgz
            artifacts/ins-armv7-unknown-linux-gnueabihf-v${{ steps.vars.outputs.version }}.tgz.sha256
            artifacts/ins-aarch64-termux-v${{ steps.vars.outputs.version }}.tgz
            artifacts/ins-aarch64-termux-v${{ steps.vars.outputs.version }}.tgz.sha256
  pkgbuild:
    name: Build Arch package
    runs-on: blacksmith-4vcpu-ubuntu-2404
    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: blacksmith-4vcpu-ubuntu-2404
    needs:
      - build
      - cross-compile
      - 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 }}
