name: "Build binaries"

on:
  workflow_call:
    inputs:
      binary:
        description: "binary to build"
        type: string
        required: true
      tag:
        description: "tag name to upload binaries to"
        type: string
        required: true

env:
  CARGO_TERM_COLOR: always

jobs:
  linux:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          - arch: x86_64
          # - arch: aarch64
    container:
      image: messense/rust-musl-cross:${{ matrix.arch }}-musl
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Setup Rust toolchain
        uses: dsherret/rust-toolchain-file@v1

      - name: Setup cache
        uses: Swatinem/rust-cache@v2
        with:
          cache-directories: ~/.cache/sccache
          shared-key: marine
          save-if: false

      - name: Setup sccache
        uses: mozilla-actions/sccache-action@v0.0.3

      - name: Build ${{ inputs.binary }}
        run: cargo build -p ${{ inputs.binary }} --release

      - name: Prepare binary for upload
        id: prepare
        env:
          BIN: "${{ inputs.binary }}-linux-${{ matrix.arch }}"
        run: |
          mv target/${{ matrix.arch }}-unknown-linux-musl/release/${{ inputs.binary }} ${BIN}
          echo "bin=${BIN}" >> $GITHUB_OUTPUT

      - name: Upload binary to checks
        uses: actions/upload-artifact@v3
        with:
          name: ${{ steps.prepare.outputs.bin }}-artifact
          path: ${{ steps.prepare.outputs.bin }}

  macos:
    runs-on: macos-12
    strategy:
      fail-fast: false
      matrix:
        include:
          - arch: x86_64
    env:
      CARGO_BUILD_TARGET: ${{ matrix.arch }}-apple-darwin
      MACOS_SDK: "macosx12.3"
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Prepare build variables
        run: |
          sudo xcode-select -switch /Applications/Xcode_13.4.1.app
          SDKROOT=$(xcrun -sdk ${{ env.MACOS_SDK }} --show-sdk-path)
          MACOSX_DEPLOYMENT_TARGET=$(xcrun -sdk ${{ env.MACOS_SDK }} --show-sdk-platform-version)

          echo "SDKROOT=${SKDROOT}" >> $GITHUB_ENV
          echo "MACOSX_DEPLOYMENT_TARGET=${SKDROOT}" >> $GITHUB_ENV

      - name: Setup Rust toolchain
        uses: dsherret/rust-toolchain-file@v1

      - name: Setup cache
        uses: Swatinem/rust-cache@v2
        with:
          cache-directories: ~/.cache/sccache
          shared-key: marine
          save-if: false

      - name: Setup sccache
        uses: mozilla-actions/sccache-action@v0.0.3

      - name: Build ${{ inputs.binary }}
        run: cargo build -p ${{ inputs.binary }} --release

      - name: Prepare binary for upload
        id: prepare
        env:
          BIN: "${{ inputs.binary }}-darwin-${{ matrix.arch }}"
        run: |
          mv target/${{ matrix.arch }}-apple-darwin/release/${{ inputs.binary }} ${BIN}
          echo "bin=${BIN}" >> $GITHUB_OUTPUT

      - name: Upload binary to checks
        uses: actions/upload-artifact@v3
        with:
          name: ${{ steps.prepare.outputs.bin }}-artifact
          path: ${{ steps.prepare.outputs.bin }}

  upload:
    name: "Upload ${{ inputs.binary }}"
    runs-on: ubuntu-latest
    needs:
      - linux
      - macos

    permissions:
      contents: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Download binaries
        uses: actions/download-artifact@v3
        with:
          path: binaries/

      - name: Generate checksums
        id: sum
        run: |
          ls -R
          mv ${{ inputs.binary }}-*/* .
          rm -rf *-artifact
          sha256sum * | tee > ${{ inputs.binary }}_SHA256_SUMS
        working-directory: binaries

      - name: Upload binaries
        uses: softprops/action-gh-release@v1
        with:
          files: |
            binaries/*
          tag_name: ${{ inputs.tag }}
