name: Odin Bindings Update
on:
  push:
    branches: [main]
jobs:
  check_changes:
    runs-on: ubuntu-latest
    outputs:
      changed: ${{ steps.check_clay.outputs.changed }}
    steps:
      - name: Checkout repo
        uses: actions/checkout@v4
        with:
          fetch-depth: 2

      - name: Check if clay.h changed
        id: check_clay
        run: |
          if git diff --name-only HEAD^ HEAD | grep -Fx "clay.h"; then
            echo "changed=true" >> $GITHUB_OUTPUT
          else
            echo "changed=false" >> $GITHUB_OUTPUT
          fi

  build:
    needs: check_changes
    if: needs.check_changes.outputs.changed == 'true'
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest]
    steps:
      - name: Checkout repo
        uses: actions/checkout@v4

      - name: Install clang (Linux)
        if: runner.os == 'Linux'
        run: |
          sudo apt-get update
          sudo apt-get install -y clang

      - name: Build libs
        run: |
          mkdir -p build
          mkdir -p artifacts
          cp clay.h clay.c

          COMMON_FLAGS="-DCLAY_IMPLEMENTATION -fno-ident -frandom-seed=clay"

          if [[ "$(uname)" == "Linux" ]]; then
            mkdir -p artifacts/linux
            mkdir -p artifacts/windows
            mkdir -p artifacts/wasm

            echo "Building for Linux..."
            clang -c $COMMON_FLAGS -fPIC -ffreestanding -static -target x86_64-unknown-linux-gnu clay.c -o build/linux.o
            ar rD artifacts/linux/clay.a build/linux.o

            echo "Building for Windows..."
            clang -c $COMMON_FLAGS -ffreestanding -target x86_64-pc-windows-msvc -fuse-ld=llvm-lib clay.c -o artifacts/windows/clay.lib

            echo "Building for WASM..."
            clang -c $COMMON_FLAGS -fPIC -target wasm32 -nostdlib -static clay.c -o artifacts/wasm/clay.o
          elif [[ "$(uname)" == "Darwin" ]]; then
            mkdir -p artifacts/macos
            mkdir -p artifacts/macos-arm64
            echo "Building for macOS (x86_64)..."
            clang -c $COMMON_FLAGS -fPIC -target x86_64-apple-macos clay.c -o build/macos.o
            libtool -static -o artifacts/macos/clay.a build/macos.o

            echo "Building for macOS (ARM64)..."
            clang -c $COMMON_FLAGS -fPIC -target arm64-apple-macos clay.c -o build/macos-arm64.o
            libtool -static -o artifacts/macos-arm64/clay.a build/macos-arm64.o
          fi
          rm -f clay.c build/*.o

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: artifacts-${{ matrix.os }}
          path: artifacts/

  commit:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repo
        uses: actions/checkout@v4

      - name: Download artifacts
        uses: actions/download-artifact@v4

      - name: Move artifacts
        run: |
          cp -r artifacts-ubuntu-latest/* bindings/odin/clay-odin/
          cp -r artifacts-macos-latest/* bindings/odin/clay-odin/

      - name: Commit/Push changes
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add bindings/odin/clay-odin/
          git commit -m "[bindings/odin] Update Odin bindings"
          git push
