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

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

jobs:
  build:
    if: github.ref_name == github.event.repository.default_branch
    runs-on: ubuntu-latest
    container:
      image: ghcr.io/cross-rs/x86_64-unknown-netbsd
    steps:
      - uses: actions/checkout@v3
      - name: Install prerequisites
        run: apt install wget unzip
      - name: Retrieve OpenGL and X11 dev files (64 bit)
        run: |
          mkdir src/netbsd64
          cd src/netbsd64
          wget https://github.com/ClassiCube/rpi-compiling-stuff/raw/main/netbsd64.zip
          unzip netbsd64.zip
      - name: Compile NetBSD builds
        id: compile
        shell: bash
        env: 
          COMMON_FLAGS: "-O1 -s -fno-stack-protector -fno-math-errno -Qn"
          PLAT64_FLAGS: "-fno-pie -fvisibility=hidden -fcf-protection=none -rdynamic -DCC_BUILD_ICON -I netbsd64/include -L netbsd64/lib -Wl,--unresolved-symbols=ignore-in-shared-libs"
        run: |
          LATEST_FLAG=-DCC_COMMIT_SHA=\"${GITHUB_SHA::9}\"
          echo $LATEST_FLAG
          
          cd src
          x86_64-unknown-netbsd-gcc *.c ${{ env.COMMON_FLAGS }} ${{ env.PLAT64_FLAGS }} $LATEST_FLAG -o cc-netbsd64-gl1 -lm -lpthread -lX11 -lXi -lGL -lexecinfo


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