name: Tagged release

on:
  push:
    tags:
      - "*"

env:
  CARGO_TERM_COLOR: always
  RUST_TOOLCHAIN: 1.97.1

jobs:
  prepare-release:
    runs-on: linux-arm64
    outputs:
      release_id: ${{ steps.release.outputs.release_id }}
    steps:
      - name: Check out the tag
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Install the release Rust toolchain
        run: rustup toolchain install ${RUST_TOOLCHAIN} --profile minimal

      - name: Test release tooling
        run: cargo +${RUST_TOOLCHAIN} test --locked --package bds-release

      - name: Create draft release
        id: release
        env:
          GITEA_API_URL: ${{ gitea.server_url }}/api/v1
          GITEA_REPOSITORY: ${{ gitea.repository }}
          GITEA_REF_NAME: ${{ gitea.ref_name }}
          GITEA_SERVER_URL: ${{ gitea.server_url }}
          GITEA_SHA: ${{ gitea.sha }}
          GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
        run: cargo +${RUST_TOOLCHAIN} run --release --locked --package bds-release -- prepare

  build-macos:
    needs: prepare-release
    runs-on: linux-arm64
    container: ghcr.io/rust-cross/cargo-zigbuild@sha256:82af75c41958c2af2787e8bedd912da7678a9438937e223e9d83d006d747b38b
    env:
      CARGO_TARGET_AARCH64_APPLE_DARWIN_RUSTFLAGS: -C link-arg=-mmacosx-version-min=26.0 -C link-arg=-Wl,-rpath,@executable_path/../Resources -C link-arg=-Wl,-headerpad,0x1000
      CARGO_TARGET_X86_64_APPLE_DARWIN_RUSTFLAGS: -C link-arg=-mmacosx-version-min=26.0 -C link-arg=-Wl,-rpath,@executable_path/../Resources -C link-arg=-Wl,-headerpad,0x1000
    steps:
      - name: Check out the tag
        env:
          GITEA_REF_NAME: ${{ gitea.ref_name }}
          GITEA_REPOSITORY: ${{ gitea.repository }}
          GITEA_SERVER_URL: ${{ gitea.server_url }}
          GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
        run: |
          set -eu
          git init
          git -c http.extraHeader="Authorization: token $GITEA_TOKEN" fetch --depth=1 \
            "$GITEA_SERVER_URL/$GITEA_REPOSITORY.git" "refs/tags/$GITEA_REF_NAME"
          git checkout --detach FETCH_HEAD

      - name: Install release Rust targets
        run: >-
          rustup toolchain install ${RUST_TOOLCHAIN} --profile minimal
          --target aarch64-apple-darwin
          --target x86_64-apple-darwin

      - name: Build, package, and upload macOS releases
        env:
          GITEA_API_URL: ${{ gitea.server_url }}/api/v1
          GITEA_REF_NAME: ${{ gitea.ref_name }}
          GITEA_REPOSITORY: ${{ gitea.repository }}
          GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
          RELEASE_ID: ${{ needs.prepare-release.outputs.release_id }}
        run: |
          set -eu
          for build in \
            'aarch64-apple-darwin|aarch64-apple-darwin|darwin-arm64' \
            'x86_64-apple-darwin|x86_64-apple-darwin|darwin-x64'
          do
            target=${build%%|*}
            rest=${build#*|}
            build_target=${rest%%|*}
            platform=${rest#*|}
            cargo +${RUST_TOOLCHAIN} zigbuild --release --locked --target "$build_target" \
              --package bds-ui --package bds-cli --package bds-mcp
            rust_lib_dir=$(rustc +${RUST_TOOLCHAIN} --target "$target" --print target-libdir)
            cargo +${RUST_TOOLCHAIN} run --release --locked --package bds-release -- package \
              "$GITEA_REF_NAME" "target/$target/release" "$rust_lib_dir" "$platform" dist --upload
          done

  build-linux-arm64:
    needs: prepare-release
    runs-on: linux-arm64
    env:
      CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_RUSTFLAGS: -C link-arg=-Wl,-rpath,$ORIGIN
    steps:
      - name: Check out the tag
        uses: actions/checkout@v4

      - name: Install native Linux desktop dependencies
        run: sh .gitea/scripts/install-linux-build-dependencies.sh arm64

      - name: Install the release Rust target
        run: rustup toolchain install ${RUST_TOOLCHAIN} --profile minimal --target aarch64-unknown-linux-gnu

      - name: Build, package, and upload Linux arm64 release
        env:
          GITEA_API_URL: ${{ gitea.server_url }}/api/v1
          GITEA_REF_NAME: ${{ gitea.ref_name }}
          GITEA_REPOSITORY: ${{ gitea.repository }}
          GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
          RELEASE_ID: ${{ needs.prepare-release.outputs.release_id }}
        run: |
          set -eu
          target=aarch64-unknown-linux-gnu
          cargo +${RUST_TOOLCHAIN} build --release --locked --target "$target" \
            --package bds-ui --package bds-cli --package bds-mcp
          rust_lib_dir=$(rustc +${RUST_TOOLCHAIN} --target "$target" --print target-libdir)
          cargo +${RUST_TOOLCHAIN} run --release --locked --package bds-release -- package \
            "$GITEA_REF_NAME" "target/$target/release" "$rust_lib_dir" linux-arm64 dist --upload

  build-linux-x64:
    needs: prepare-release
    runs-on: linux-arm64
    env:
      AR_x86_64_unknown_linux_gnu: x86_64-linux-gnu-ar
      CC_x86_64_unknown_linux_gnu: x86_64-linux-gnu-gcc
      CXX_x86_64_unknown_linux_gnu: x86_64-linux-gnu-g++
      CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER: x86_64-linux-gnu-gcc
      CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUSTFLAGS: -C link-arg=-Wl,-rpath,$ORIGIN -L native=/usr/lib/x86_64-linux-gnu
      PKG_CONFIG_ALLOW_CROSS: "1"
      PKG_CONFIG_LIBDIR: /usr/lib/x86_64-linux-gnu/pkgconfig:/usr/share/pkgconfig
    steps:
      - name: Check out the tag
        uses: actions/checkout@v4

      - name: Install x64 Linux desktop dependencies and cross-linker
        run: sh .gitea/scripts/install-linux-build-dependencies.sh x64

      - name: Install the release Rust target
        run: rustup toolchain install ${RUST_TOOLCHAIN} --profile minimal --target x86_64-unknown-linux-gnu

      - name: Build, package, and upload Linux x64 release
        env:
          GITEA_API_URL: ${{ gitea.server_url }}/api/v1
          GITEA_REF_NAME: ${{ gitea.ref_name }}
          GITEA_REPOSITORY: ${{ gitea.repository }}
          GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
          RELEASE_ID: ${{ needs.prepare-release.outputs.release_id }}
        run: |
          set -eu
          target=x86_64-unknown-linux-gnu
          cargo +${RUST_TOOLCHAIN} build --release --locked --target "$target" \
            --package bds-ui --package bds-cli --package bds-mcp
          rust_lib_dir=$(rustc +${RUST_TOOLCHAIN} --target "$target" --print target-libdir)
          cargo +${RUST_TOOLCHAIN} run --release --locked --package bds-release -- package \
            "$GITEA_REF_NAME" "target/$target/release" "$rust_lib_dir" linux-x64 dist --upload

  build-windows:
    needs: prepare-release
    runs-on: linux-arm64
    container: messense/cargo-xwin@sha256:4696dd4e79edf8569fa99c4b06bd99273e0501c7adc983aa61d57945f795bef0
    env:
      CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_RUSTFLAGS: -C target-feature=+crt-static
    steps:
      - name: Check out the tag
        env:
          GITEA_REF_NAME: ${{ gitea.ref_name }}
          GITEA_REPOSITORY: ${{ gitea.repository }}
          GITEA_SERVER_URL: ${{ gitea.server_url }}
          GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
        run: |
          set -eu
          git init
          git -c http.extraHeader="Authorization: token $GITEA_TOKEN" fetch --depth=1 \
            "$GITEA_SERVER_URL/$GITEA_REPOSITORY.git" "refs/tags/$GITEA_REF_NAME"
          git checkout --detach FETCH_HEAD

      - name: Install the release Rust target
        run: rustup toolchain install ${RUST_TOOLCHAIN} --profile minimal --target x86_64-pc-windows-msvc

      - name: Build, package, and upload Windows release
        env:
          GITEA_API_URL: ${{ gitea.server_url }}/api/v1
          GITEA_REF_NAME: ${{ gitea.ref_name }}
          GITEA_REPOSITORY: ${{ gitea.repository }}
          GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
          RELEASE_ID: ${{ needs.prepare-release.outputs.release_id }}
        run: |
          set -eu
          target=x86_64-pc-windows-msvc
          cargo +${RUST_TOOLCHAIN} xwin build --release --locked --target "$target" \
            --package bds-ui --package bds-cli --package bds-mcp
          rust_lib_dir=$(rustc +${RUST_TOOLCHAIN} --target "$target" --print target-libdir)
          cargo +${RUST_TOOLCHAIN} run --release --locked --package bds-release -- package \
            "$GITEA_REF_NAME" "target/$target/release" "$rust_lib_dir" windows-x64 dist --upload

  publish-release:
    needs:
      - prepare-release
      - build-macos
      - build-linux-arm64
      - build-linux-x64
      - build-windows
    runs-on: linux-arm64
    steps:
      - name: Check out the tag
        uses: actions/checkout@v4

      - name: Install the release Rust toolchain
        run: rustup toolchain install ${RUST_TOOLCHAIN} --profile minimal

      - name: Publish complete release
        env:
          GITEA_API_URL: ${{ gitea.server_url }}/api/v1
          GITEA_REPOSITORY: ${{ gitea.repository }}
          GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
          RELEASE_ID: ${{ needs.prepare-release.outputs.release_id }}
        run: cargo +${RUST_TOOLCHAIN} run --release --locked --package bds-release -- publish
