name: SourcePawn scripting
on: 
  push:
    branches:
      - master
      - '[0-9]+.[0-9]+-dev'
    paths:
      - 'plugins/include/*'
      - 'sourcepawn/**'
  workflow_dispatch:
  schedule:
    - cron: '53 05 01 */3 *' # Artifacts expire every 3 months
jobs:
  build:
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
        include:
          - os: ubuntu-latest
            os_short: linux
          - os: windows-latest
            os_short: win
          - os: macos-latest
            os_short: mac
      fail-fast: false
    runs-on: ${{ matrix.os }}
    env:
      ARCH: x86,x86_64
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: recursive

      # Setup Python for AMBuild
      - uses: actions/setup-python@v4
        name: Setup Python 3.8
        with:
          python-version: 3.8
      - name: Install AMBuild
        run: |
          python -m pip install --upgrade pip setuptools wheel
          pip install git+https://github.com/alliedmodders/ambuild

      - name: Build only for x64 on macOS
        if: startsWith(runner.os, 'macOS')
        run: echo "ARCH=x86_64" >> $GITHUB_ENV

      - name: Install Linux dependencies
        if: startsWith(runner.os, 'Linux')
        run: |
          sudo dpkg --add-architecture i386
          sudo apt-get update
          sudo apt-get install -y --no-install-recommends \
            gcc-multilib g++-multilib libstdc++6 lib32stdc++6 \
            libc6-dev libc6-dev-i386 linux-libc-dev \
            linux-libc-dev:i386 lib32z1-dev ${{ matrix.compiler_cc }}

      - name: Select clang compiler
        if: startsWith(runner.os, 'Linux') || startsWith(runner.os, 'macOS')
        run: |
          echo "CC=clang" >> $GITHUB_ENV
          echo "CXX=clang++" >> $GITHUB_ENV
          clang --version
          clang++ --version

      - name: Build
        shell: bash
        run: |
          mkdir build
          cd build
          python ../configure.py --enable-optimize --scripting-only --targets=${{ env.ARCH }}
          ambuild
          echo "SM_VERSION=$(cat ../product.version)" >> $GITHUB_ENV

      - name: Archive tooling
        uses: actions/upload-artifact@v3
        with:
          name: sourcemod-tooling-${{ env.SM_VERSION }}-${{ matrix.os_short }}
          path: build/package
