name: Release

on:
  push:
    tags:
      - "v*"

jobs:
  create-release:
    runs-on: ubuntu-latest
    outputs:
      release_id: ${{ steps.create.outputs.release_id }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Create Gitea release
        id: create
        env:
          GITEA_TOKEN: ${{ secrets.GITEATOKEN }}
          TAG: ${{ github.ref_name }}
        run: |
          sudo apt-get update -qq && sudo apt-get install -y -qq jq
          body=$(git for-each-ref --format='%(contents)' "refs/tags/$TAG")
          release_id=$(curl -s -X POST \
            "${{ github.server_url }}/api/v1/repos/${{ github.repository }}/releases" \
            -H "Authorization: token $GITEA_TOKEN" \
            -H "Content-Type: application/json" \
            -d "{\"tag_name\":\"$TAG\",\"name\":\"$TAG\",\"body\":$(echo "$body" | jq -Rs .)}" | jq -r '.id')
          echo "release_id=$release_id" >> $GITHUB_OUTPUT

  build-linux-x86_64:
    needs: create-release
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: src
    steps:
      - uses: actions/checkout@v4

      - name: Install Rust + zigbuild
        run: |
          curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain stable
          echo "$HOME/.cargo/bin" >> $GITHUB_PATH
          sudo apt-get update -qq && sudo apt-get install -y -qq jq
          pip install ziglang --quiet --break-system-packages
          $HOME/.cargo/bin/cargo install cargo-zigbuild
          $HOME/.cargo/bin/rustup target add x86_64-unknown-linux-musl

      - name: Create musl C/C++ wrappers
        run: |
          ZIG=$(python3 -c "import ziglang, os; print(os.path.join(os.path.dirname(ziglang.__file__), 'zig'))")
          printf '#!/bin/sh\nexec "%s" cc -target x86_64-linux-musl "$@"\n' "$ZIG" | sudo tee /usr/local/bin/x86_64-linux-musl-gcc > /dev/null
          printf '#!/bin/sh\nexec "%s" c++ -target x86_64-linux-musl "$@"\n' "$ZIG" | sudo tee /usr/local/bin/x86_64-linux-musl-g++ > /dev/null
          sudo chmod +x /usr/local/bin/x86_64-linux-musl-gcc /usr/local/bin/x86_64-linux-musl-g++

      - name: Cache cargo registry
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            src/target
          key: linux-musl-cargo-${{ hashFiles('src/Cargo.lock') }}
          restore-keys: linux-musl-cargo-

      - name: Build static binary
        env:
          PKG_CONFIG_ALLOW_CROSS: "1"
        run: cargo zigbuild --release --target x86_64-unknown-linux-musl

      - name: Prepare and upload artifact
        env:
          GITEA_TOKEN: ${{ secrets.GITEATOKEN }}
          RELEASE_ID: ${{ needs.create-release.outputs.release_id }}
        run: |
          mkdir -p /tmp/dist
          cp target/x86_64-unknown-linux-musl/release/obikmer /tmp/dist/obikmer-linux-x86_64
          strip /tmp/dist/obikmer-linux-x86_64
          curl -s -X POST \
            "${{ github.server_url }}/api/v1/repos/${{ github.repository }}/releases/$RELEASE_ID/assets" \
            -H "Authorization: token $GITEA_TOKEN" \
            -F "attachment=@/tmp/dist/obikmer-linux-x86_64"

  build-macos-arm64:
    needs: create-release
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Login to registry
        run: echo "${{ secrets.REGISTRYTOKEN }}" | docker login registry.metabarcoding.org -u ${{ secrets.REGISTRYUSER }} --password-stdin

      - name: Cache cargo registry
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            src/target
          key: macos-arm64-cargo-${{ hashFiles('src/Cargo.lock') }}
          restore-keys: macos-arm64-cargo-

      - name: Build macOS binary
        run: |
          docker run --rm \
            -v "${{ github.workspace }}:/src" \
            -w /src/src \
            registry.metabarcoding.org/cibuilder/rustcrossosx:latest \
            cargo build --release --target aarch64-apple-darwin --no-default-features

      - name: Prepare and upload artifact
        env:
          GITEA_TOKEN: ${{ secrets.GITEATOKEN }}
          RELEASE_ID: ${{ needs.create-release.outputs.release_id }}
        run: |
          mkdir -p /tmp/dist
          cp src/target/aarch64-apple-darwin/release/obikmer /tmp/dist/obikmer-macos-arm64
          curl -s -X POST \
            "${{ github.server_url }}/api/v1/repos/${{ github.repository }}/releases/$RELEASE_ID/assets" \
            -H "Authorization: token $GITEA_TOKEN" \
            -F "attachment=@/tmp/dist/obikmer-macos-arm64"
