name: Worker
on:
  workflow_call:
    inputs:
      bldtype:
        required: true
        type: string
      runtype:
        required: true
        type: string
      os:
        required: true
        type: string

jobs:
  worker:
    runs-on: ${{ inputs.os }}

    steps:
    - name: Checkout
      uses: actions/checkout@v6
      with:
        fetch-depth: 50

    - name: Add an ACL to KVM for ourselves
      run: |
          echo 'SUBSYSTEM=="misc", KERNEL=="kvm", RUN+="/usr/bin/setfacl -m u:'${USER}':rw- /dev/kvm"' | sudo tee /etc/udev/rules.d/99-kvm4dosemu2.rules
          sudo udevadm control --reload-rules
          sudo udevadm trigger --name-match=kvm

    - name: Information
      run: |
        #
        echo "::group::Git History (Current Branch vs Devel)"
        if [ "$(git rev-parse --abbrev-ref HEAD)" = "devel" ] ; then
          echo "Current branch is Devel"
          git log --color=always --oneline -n 5
        else
          git fetch origin devel:refs/remotes/origin/devel --depth=50 >/dev/null 2>&1
          if [ "${{ github.event_name }}" = "pull_request" ] ; then
            REF="origin/devel"
            HEAD="HEAD^2"
            git log --color=always --graph --oneline --boundary --first-parent ${REF}..${HEAD} || echo "Devel is probably more than 50 commits away"
          else
            REF="origin/devel^"
            HEAD="HEAD"
            git log --color=always --graph --oneline ${REF}..${HEAD} -n 20
          fi
        fi
        echo "::endgroup::"

        echo "::group::CPU"
        lscpu
        echo "::endgroup::"

        echo "::group::Kernel Config"
        cat /boot/config-$(uname -r)
        echo "::endgroup::"

        echo "::group::Kernel Modules"
        sudo lsmod
        echo "::endgroup::"

        echo "::group::Kernel Parameters (sysctl)"
        sysctl -a
        echo "::endgroup::"

        echo "::group::KVM permissions"
        getfacl -p /dev/kvm
        echo "::endgroup::"

    - name: Package install
      run: ./ci_prereq.sh

    - name: Build or install software under test
      env:
        BLDTYPE: ${{ inputs.bldtype }}
        RUNTYPE: ${{ inputs.runtype }}
        OS: ${{ inputs.os }}
      run: ./ci_build_or_install.sh

    - name: Cache
      id: cache
      uses: actions/cache@v5
      with:
        path: ~/cache
        key: cache-v00001
        restore-keys: |
          cache-

    - name: Populate cache
      if: steps.cache.outputs.cache-hit != 'true'
      run: |
        echo "Cache missed! Populating with fresh binaries"
        [ -h "test-binaries" ] || ln -s "${HOME}"/cache "test-binaries"
        python3 test/test_dosemu.py --get-test-binaries

    - name: Install test prerequisites
      id: test_prereq
      run: ./ci_test_prereq.sh

    - name: Open Watcom install
      uses: open-watcom/setup-watcom@v1
      with:
        version: 2.0-64

    - name: Test
      id: test
      env:
        BLDTYPE: ${{ inputs.bldtype }}
        RUNTYPE: ${{ inputs.runtype }}
        OS: ${{ inputs.os }}
        BASE_SHA: ${{ github.event.pull_request.base.sha }}
      run: ./ci_test.sh

    - name: Upload failure logs
      if: ${{ always() && (steps.test.outcome == 'failure') }}
      uses: actions/upload-artifact@v6
      with:
        name: failure-logs-${{ format('{0}-{1}-{2}', inputs.bldtype, inputs.runtype, inputs.os) }}
        path: test_*
