name: mcxtrace-autobuild
on:
  schedule:
    - cron: '50 23 * * *'  # 23:50 every day

  workflow_dispatch:
    inputs:
      manual-debugging:
        type: boolean
        description: Launch manual debugging tmate for inspection (automatic in case of errors)
        default: false

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        include:
        - { os: ubuntu-latest, CC: gcc,      CXX: g++,        python: '3.11' }
        - { os: macos-latest,      CC: clang,    CXX: clang++,    python: "3.11" }

    name: ${{ matrix.os }}.${{ matrix.CC }}.python-${{ matrix.python }}
    runs-on: ${{ matrix.os }}
    env:
      CC: ${{ matrix.CC }}
      CXX: ${{ matrix.CXX }}
    defaults:
      run:
        shell: bash -el {0}

    steps:
    - name: Checkout
      uses: actions/checkout@v4
      with:
        path: McCode

    - name: Setup python
      uses: actions/setup-python@v5
      with:
        python-version: ${{ matrix.python }}

    - name: Setup build-deps
      id: setup-build-deps
      run: |
           if [ "$RUNNER_OS" == "Linux" ]; then sudo apt-get update && sudo apt-get -y install equivs ; fi
           if [ "$RUNNER_OS" == "Linux" ]; then sudo apt-get -y install xvfb mingw-w64 gfortran-mingw-w64 mingw-w64-x86-64-dev libz-mingw-w64-dev dos2unix nsis wine64; fi
           if [ "$RUNNER_OS" == "Linux" ]; then sudo dpkg --add-architecture i386 && sudo apt-get update && sudo apt-get install wine32; fi
           ( if [ "$RUNNER_OS" == "Linux" ]; then Xvfb :0 -screen 0 1024x768x16 ; fi ) &
           if [ "$RUNNER_OS" == "Linux" ]; then mkdir /tmp/innosetup && cd /tmp/innosetup && wget https://jrsoftware.org/download.php/is.exe && DISPLAY=:0 wine ./is.exe /SP- /VERYSILENT /ALLUSERS /SUPPRESSMSGBOXES /DOWNLOADISCRYPT=1 ; fi
           if [ "$RUNNER_OS" == "Linux" ]; then killall -9 Xvfb ; fi

    - name: Check versions
      id: version-checks
      run: |
           which python3
           python3 --version
           which cmake
           cmake --version
           #NB: bison and flex in path are actually too old, on mac we
           #inject these via a brew keg later

    - name: Build McXtrace
      id: mcxtrace-build
      run: |
           if [ "$RUNNER_OS" == "macOS" ]; then export SDKROOT=$(xcrun --sdk macosx --show-sdk-path); fi
           set -e
           set -u
           set -x
           cd McCode
           export REV=`git -c 'versionsort.suffix=-' ls-remote --exit-code --refs --sort='version:refname' --tags origin 'mcxtrace-*.*.*' | tail -1 | cut -f3 -d/ | cut -f2 -d\-`
           if [ "$RUNNER_OS" == "macOS" ]; then ./buildscripts/build_macos_mcstas ${REV}; fi
           if [ "$RUNNER_OS" == "Linux" ]; then ./buildscripts/build_debs_mcstas ${REV} meta; fi
           tar cfz dist-${{ matrix.os }}.${{ matrix.CC }}.python-${{ matrix.python }}.tgz dist/

    - name: Cross-Build McXtrace for windows
      id: mcxtrace-mingw-build
      run: |
           set -e
           set -u
           set -x
           cd McCode
           export REV=`git -c 'versionsort.suffix=-' ls-remote --exit-code --refs --sort='version:refname' --tags origin 'mcxtrace-*.*.*' | tail -1 | cut -f3 -d/ | cut -f2 -d\-`
           if [ "$RUNNER_OS" == "Linux" ]; then rm -rf dist; fi
           if [ "$RUNNER_OS" == "Linux" ]; then ./buildscripts/build_windows_mcxtrace ${REV}; fi
           if [ "$RUNNER_OS" == "Linux" ]; then tar cfz dist-Windows.mingw64.python-${{ matrix.python }}.tgz dist/; fi

    - name: 'Upload Artifact'
      id: tar-upload
      uses: actions/upload-artifact@v4
      if: always()
      with:
        name: mcxtrace-artefacts-${{ matrix.os }}.${{ matrix.CC }}.python-${{ matrix.python }}
        path: "McCode/dist-*.tgz"

    - name: Setup tmate session for manual debugging
      uses: mxschmitt/action-tmate@v3
      if: always() && (inputs.manual-debugging == true && steps.tar-package.outcome != 'success')
      with:
        limit-access-to-actor: true
