name: Build latest (FreeBSD)
on: [push]

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

jobs:
  build:
    if: github.ref_name == github.event.repository.default_branch
    runs-on: ubuntu-latest
    container:
      image: empterdose/freebsd-cross-build:11.4
    steps:
      - uses: actions/checkout@v4
      - name: Install prerequisites
        run: apk add bash wget
      - name: Retrieve OpenGL and X11 dev files (64 bit)
        run: |
          mkdir src/freebsd64
          cd src/freebsd64
          wget https://github.com/ClassiCube/rpi-compiling-stuff/raw/main/freebsd64.zip
          unzip freebsd64.zip
      - name: Retrieve OpenGL and X11 dev files (32 bit)
        run: |
          mkdir src/freebsd32
          cd src/freebsd32
          wget https://github.com/ClassiCube/rpi-compiling-stuff/raw/main/freebsd32.zip
          unzip freebsd32.zip  
      - name: Compile FreeBSD builds
        id: compile
        shell: bash
        env: 
          COMMON_FLAGS: "-O1 -s -fno-stack-protector -fno-math-errno -Qn"
          PLAT32_FLAGS: "-fno-pie -fvisibility=hidden -fcf-protection=none -rdynamic -DCC_BUILD_ICON -I freebsd32/include -L freebsd32/lib"
          PLAT64_FLAGS: "-fno-pie -fvisibility=hidden -fcf-protection=none -rdynamic -DCC_BUILD_ICON -I freebsd64/include -L freebsd64/lib"
        run: |
          apk add curl
          LATEST_FLAG=-DCC_COMMIT_SHA=\"${GITHUB_SHA::9}\"
          
          cd src
          i386-freebsd11-clang *.c ${{ env.COMMON_FLAGS }} ${{ env.PLAT32_FLAGS }} $LATEST_FLAG -o cc-fbsd32-gl1 -lm -lpthread -lX11 -lXi -lGL -lexecinfo
          x86_64-freebsd11-clang *.c ${{ env.COMMON_FLAGS }} ${{ env.PLAT64_FLAGS }} $LATEST_FLAG -o cc-fbsd64-gl1 -lm -lpthread -lX11 -lXi -lGL -lexecinfo


      # otherwise notify_failure doesn't work
      - name: Install curl when necessary
        if: ${{ always() && steps.compile.outcome == 'failure' }}
        run: apk add curl

      - uses: ./.github/actions/notify_failure
        if: ${{ always() && steps.compile.outcome == 'failure' }}
        with:
          NOTIFY_MESSAGE: 'Failed to compile FreeBSD build(s)'
          WEBHOOK_URL: '${{ secrets.WEBHOOK_URL }}'
          
          
      - uses: ./.github/actions/upload_build
        if: ${{ always() && steps.compile.outcome == 'success' }}
        with:
          SOURCE_FILE: 'src/cc-fbsd32-gl1'
          DEST_NAME: 'ClassiCube-FreeBSD-32'
          
      - uses: ./.github/actions/upload_build
        if: ${{ always() && steps.compile.outcome == 'success' }}
        with:
          SOURCE_FILE: 'src/cc-fbsd64-gl1'
          DEST_NAME: 'ClassiCube-FreeBSD-64'
          
          
      - uses: ./.github/actions/notify_success
        if: ${{ always() && steps.compile.outcome == 'success' }}
        with:
          DESTINATION_URL: '${{ secrets.NOTIFY_URL }}'
          WORKFLOW_NAME: 'freebsd'