name: Build latest (RPI)
# trigger via either push to selected branches or on manual run
on:
  push:
    branches:
      - main
      - master
  workflow_dispatch:

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

jobs:
#============================================
# ================ 32 BIT RPI ===============
# ===========================================
  build-RPI32:
    runs-on: ubuntu-latest
    container:
      image: dockcross/linux-armv6-lts
    steps:
      - uses: actions/checkout@v4
      - name: Retrieve OpenGL and X11 dev files
        run: |
          mkdir src/rpi
          cd src/rpi
          wget https://github.com/ClassiCube/rpi-compiling-stuff/raw/main/rpi32.zip
          unzip rpi32.zip
      - name: Compile RPI 32 bit build
        id: compile
        env: 
          COMMON_FLAGS: "-O1 -s -fno-stack-protector -fno-math-errno -Qn"
          RPI32_FLAGS: "-fvisibility=hidden -rdynamic -DCC_BUILD_ICON -DCC_BUILD_RPI -I rpi/include -L rpi/lib -Wl,--unresolved-symbols=ignore-in-shared-libs"
        run: |
          LATEST_FLAG=-DCC_COMMIT_SHA=\"$GITHUB_SHA\"
          
          cd src
          $CC *.c ${{ env.COMMON_FLAGS }} ${{ env.RPI32_FLAGS }} $LATEST_FLAG -o cc-rpi32 -lGLESv2 -lEGL -lX11 -lXi -lm -lpthread -ldl -lrt


      - uses: ./.github/actions/notify_failure
        if: ${{ always() && steps.compile.outcome == 'failure' }}
        with:
          NOTIFY_MESSAGE: 'Failed to compile RPI 32 bit build'
          WEBHOOK_URL: '${{ secrets.WEBHOOK_URL }}'
          
          
      - uses: ./.github/actions/upload_build
        if: ${{ always() && steps.compile.outcome == 'success' }}
        with:
          SOURCE_FILE: 'src/cc-rpi32'
          DEST_NAME: 'cc-rpi32'
          
          
      - uses: ./.github/actions/notify_success
        if: ${{ always() && steps.compile.outcome == 'success' }}
        with:
          DESTINATION_URL: '${{ secrets.NOTIFY_URL }}'
          WORKFLOW_NAME: 'rpi32'


#============================================
# ================ 64 BIT RPI ===============
# ===========================================
  build-RPI64:
    runs-on: ubuntu-latest
    container:
      image: dockcross/linux-arm64-lts
    steps:
      - uses: actions/checkout@v4
      - name: Retrieve OpenGL and X11 dev files
        run: |
          mkdir src/rpi
          cd src/rpi
          wget https://github.com/ClassiCube/rpi-compiling-stuff/raw/main/rpi64.zip
          unzip rpi64.zip     
      - name: Compile RPI 64 bit build
        id: compile
        env: 
          COMMON_FLAGS: "-O1 -s -fno-stack-protector -fno-math-errno -Qn"
          RPI64_FLAGS: "-fvisibility=hidden -rdynamic -DCC_BUILD_ICON -DCC_BUILD_RPI -I rpi/include -L rpi/lib -Wl,--unresolved-symbols=ignore-in-shared-libs"
        run: |
          LATEST_FLAG=-DCC_COMMIT_SHA=\"$GITHUB_SHA\"
          
          cd src
          $CC *.c ${{ env.COMMON_FLAGS }} ${{ env.RPI64_FLAGS }} $LATEST_FLAG -o cc-rpi64 -lGLESv2 -lEGL -lX11 -lXi -lm -lpthread -ldl -lrt


      - uses: ./.github/actions/notify_failure
        if: ${{ always() && steps.compile.outcome == 'failure' }}
        with:
          NOTIFY_MESSAGE: 'Failed to compile RPI 64 bit build'
          WEBHOOK_URL: '${{ secrets.WEBHOOK_URL }}'
          
          
      - uses: ./.github/actions/upload_build
        if: ${{ always() && steps.compile.outcome == 'success' }}
        with:
          SOURCE_FILE: 'src/cc-rpi64'
          DEST_NAME: 'cc-rpi64'
          
          
      - uses: ./.github/actions/notify_success
        if: ${{ always() && steps.compile.outcome == 'success' }}
        with:
          DESTINATION_URL: '${{ secrets.NOTIFY_URL }}'
          WORKFLOW_NAME: 'rpi64'