name: "Actions to run for installing R packages"
inputs:
  token:
    description: GitHub token, set to secrets.GITHUB_TOKEN
    required: true
  r-version:
    description: Passed on to r-lib/actions/setup-r@v2
    required: false
    default: release
  install-r:
    description: Passed on to r-lib/actions/setup-r@v2
    required: false
    default: true
  needs:
    description: Passed on to r-lib/actions/setup-r-dependencies@v2
    required: false
    default: ""
  packages:
    description: Passed on to r-lib/actions/setup-r-dependencies@v2
    required: false
    default: deps::., any::sessioninfo
  extra-packages:
    description: Passed on to r-lib/actions/setup-r-dependencies@v2
    required: false
    default: any::rcmdcheck
  cache-version:
    description: Passed on to r-lib/actions/setup-r-dependencies@v2
    required: false
    default: 1

runs:
  using: "composite"
  steps:
    - name: Set environment variables
      run: |
        echo "R_REMOTES_NO_ERRORS_FROM_WARNINGS=true" | tee -a $GITHUB_ENV
        echo "R_KEEP_PKG_SOURCE=yes" | tee -a $GITHUB_ENV
        echo "_R_CHECK_SYSTEM_CLOCK_=false" | tee -a $GITHUB_ENV
        echo "_R_CHECK_FUTURE_FILE_TIMESTAMPS_=false" | tee -a $GITHUB_ENV
        # prevent rgl issues because no X11 display is available
        echo "RGL_USE_NULL=true" | tee -a $GITHUB_ENV
      shell: bash

    - name: Update apt
      if: runner.os == 'Linux'
      run: |
        sudo apt-get update
      shell: bash

    - uses: r-lib/actions/setup-pandoc@v2

    - uses: r-lib/actions/setup-r@v2
      with:
        r-version: ${{ inputs.r-version }}
        install-r: ${{ inputs.install-r }}
        http-user-agent: ${{ matrix.config.http-user-agent }}
        use-public-rspm: true

    - id: get-extra
      uses: ./.github/workflows/get-extra

    - uses: r-lib/actions/setup-r-dependencies@v2
      env:
        GITHUB_PAT: ${{ inputs.token }}
      with:
        pak-version: devel
        needs: ${{ inputs.needs }}
        packages: ${{ inputs.packages }}
        extra-packages: ${{ inputs.extra-packages }} ${{ ( matrix.config.covr && 'any::covr' ) || '' }} ${{ steps.get-extra.outputs.packages }}
        cache-version: ${{ inputs.cache-version }}

    - name: Add pkg.lock to .gitignore
      run: |
        set -x
        if ! [ -f .github/.gitignore ] || [ -z "$(grep '^/pkg.lock$' .github/.gitignore)" ]; then
          echo /pkg.lock >> .github/.gitignore
        fi
      shell: bash

    - name: Add fake qpdf and checkbashisms
      if: runner.os == 'Linux'
      run: |
        sudo ln -s $(which true) /usr/local/bin/qpdf
        sudo ln -s $(which true) /usr/local/bin/checkbashisms
      shell: bash

    - name: Install ccache
      uses: krlmlr/ccache-action@parallel-dir
      with:
        max-size: 10G
        verbose: 1
        save: false
        restore: false

    - name: Use ccache for compiling R code, and parallelize
      run: |
        mkdir -p ~/.R
        echo 'CC      :=  ccache $(CC)' >> ~/.R/Makevars
        echo 'CXX     :=  ccache $(CXX)' >> ~/.R/Makevars
        echo 'CXX11   :=  ccache $(CXX11)' >> ~/.R/Makevars
        echo 'CXX14   :=  ccache $(CXX14)' >> ~/.R/Makevars
        echo 'CXX17   :=  ccache $(CXX17)' >> ~/.R/Makevars
        echo 'MAKEFLAGS = -j2' >> ~/.R/Makevars
        cat ~/.R/Makevars

        echo 'CCACHE_SLOPPINESS=locale,time_macros' | tee -a $GITHUB_ENV

        # echo 'CCACHE_DEBUG=true' | tee -a $GITHUB_ENV
        # echo "CCACHE_DEBUGDIR=$(dirname $(pwd))/ccache-debug" | tee -a $GITHUB_ENV
        # mkdir -p $(dirname $(pwd))/.ccache-debug

        echo 'PKG_BUILD_EXTRA_FLAGS=false' | tee -a $GITHUB_ENV

        # Repair
        git rm -rf .ccache || true
        rm -rf .ccache
      shell: bash
