# This workflow is a demo of how to run all examples in the Icicle repository.
# For each language directory (c++, Rust, etc.) the workflow
#   (1) loops over all examples (msm, ntt, etc.) and
#   (2) runs ./run.sh in each directory.
# Each script should return 0 for success and 1 otherwise.

name: Examples

on:
  pull_request:
    branches:
      - V3
      - yshekel/V3     # TODO remove when merged to V3
  push:
    branches:
      - V3

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  check-changed-files:
    uses: ./.github/workflows/check-changed-files.yml

  extract-cuda-backend-branch:
    name: Extract cuda branch name
    runs-on: ubuntu-22.04
    outputs:
      cuda-backend-branch: ${{ steps.extract.outputs.cuda-backend-branch }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Extract Private Branch from PR Description
        id: extract
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          DESCRIPTION=$(gh pr view ${{ github.event.pull_request.number }} --json body -q '.body')
          echo "PR Description: $DESCRIPTION"
          CUDA_BE_BRANCH=$(echo "$DESCRIPTION" | grep -oP 'cuda-backend-branch:\s*\K[^\s]+') || true
          if [ -z "$CUDA_BE_BRANCH" ]; then
            CUDA_BE_BRANCH="main" # Default branch if not specified
          fi
          echo "Extracted CUDA Backend Branch: $CUDA_BE_BRANCH"
          echo "::set-output name=cuda-backend-branch::$CUDA_BE_BRANCH"

  run-examples:
    runs-on: [self-hosted, Linux, X64, icicle, examples]
    needs: [check-changed-files, extract-cuda-backend-branch]
    steps:
    - name: Checkout
      uses: actions/checkout@v4
    - name: Checkout CUDA Backend
      uses: actions/checkout@v4
      with:
        repository: ingonyama-zk/icicle-cuda-backend
        path: ./icicle_v3/backend/cuda
        token: ${{ secrets.GITHUB_TOKEN }}
        ssh-key: ${{ secrets.CUDA_PULL_KEY }}
        ref: ${{ needs.extract-branch.outputs.cuda-backend-branch }}
    - name: c++ examples
      working-directory: ./examples/c++
      if: needs.check-changed-files.outputs.cpp_cuda == 'true' || needs.check-changed-files.outputs.examples == 'true'
      run: |
        # loop over all directories in the current directory
        for dir in $(find . -mindepth 1 -maxdepth 1 -type d); do
          if [ -d "$dir" ]; then
            echo "Running command in $dir"
            cd $dir
            ./run.sh -d CUDA
            cd -
          fi
        done
    - name: Rust examples
      working-directory: ./examples/rust
      if: needs.check-changed-files.outputs.rust == 'true' || needs.check-changed-files.outputs.examples == 'true'
      run: |
        # loop over all directories in the current directory
        for dir in $(find . -mindepth 1 -maxdepth 1 -type d); do
          if [ -d "$dir" ]; then
            echo "Running command in $dir"
            cd $dir
            ./run.sh -d CUDA
            cd -
          fi
        done