name: Preview build

on:
  workflow_call:
    secrets:
      PREVIEW_CLOUDFLARE_API_TOKEN:
        description: API token to upload a preview to Cloudflare Pages
        required: true
      PREVIEW_CLOUDFLARE_ACCOUNT_ID:
        description: Account ID to upload a preview to Cloudflare Pages
        required: true

jobs:
  preview:
    name: Build preview

    environment:
      name: preview
      url: https://preview.openttd.org/pr${{ github.event.pull_request.number }}/

    runs-on: ubuntu-latest
    container:
      # If you change this version, change the numbers in the cache step,
      # .github/workflows/ci-emscripten.yml (2x) and os/emscripten/Dockerfile too.
      image: emscripten/emsdk:3.1.57

    steps:
    - name: Checkout
      uses: actions/checkout@v6
      with:
        ref: ${{ github.event.pull_request.head.sha }}

    - name: Name branch
      run: |
        git config --global --add safe.directory ${GITHUB_WORKSPACE}
        git checkout -b pr${{ github.event.pull_request.number }}

    - name: Update to modern GCC
      run: |
        apt-get update
        apt-get install -y gcc-12 g++-12
        update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 100
        update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-12 100

    - name: Setup cache
      uses: actions/cache@v4
      with:
        path: /emsdk/upstream/emscripten/cache
        # If you change this version, change the numbers in the image configuration step,
        # .github/workflows/ci-emscripten.yml (2x) and os/emscripten/Dockerfile too.
        key: 3.1.57-${{ runner.os }}

    - name: Add liblzma support
      run: |
        cp ${GITHUB_WORKSPACE}/os/emscripten/ports/liblzma.py /emsdk/upstream/emscripten/tools/ports/contrib/

    - name: Build (host tools)
      run: |
        mkdir build-host
        cd build-host

        echo "::group::CMake"
        cmake .. -DOPTION_TOOLS_ONLY=ON
        echo "::endgroup::"

        echo "::group::Build"
        echo "Running on $(nproc) cores"
        make -j$(nproc) tools
        echo "::endgroup::"

    - name: Install GCC problem matcher
      uses: ammaraskar/gcc-problem-matcher@master

    - name: Build
      run: |
        mkdir build
        cd build

        echo "::group::CMake"
        emcmake cmake .. \
          -DHOST_BINARY_DIR=../build-host \
          -DCMAKE_BUILD_TYPE=Release \
          # EOF
        echo "::endgroup::"

        echo "::group::Build"
        echo "Running on $(nproc) cores"
        cmake --build . -j $(nproc) --target openttd
        echo "::endgroup::"

    - name: Prepare preview
      run: |
        mkdir public

        cp build/openttd.data public/
        cp build/openttd.html public/
        cp build/openttd.js public/
        cp build/openttd.wasm public/

        # Ensure we use the latest version of npm; the one we get with current
        # emscripten doesn't allow running "npx wrangler" as root.
        # Current emscripten can't install npm>=10.0.0 because node is too old.
        npm install -g npm@9

    - name: Publish preview
      uses: cloudflare/pages-action@v1
      with:
        apiToken: ${{ secrets.PREVIEW_CLOUDFLARE_API_TOKEN }}
        accountId: ${{ secrets.PREVIEW_CLOUDFLARE_ACCOUNT_ID }}
        projectName: ${{ vars.PREVIEW_CLOUDFLARE_PROJECT_NAME }}
        directory: public
        branch: pr${{ github.event.pull_request.number }}
