on:
  push:
    branches: [main, master]
  pull_request:
    branches: [main, master]

name: test
 
jobs:
  run-tests:
    runs-on: ${{ matrix.os }}-latest
    name: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu]
    steps:
      - uses: actions/checkout@v2
      - name: Install R
        if: runner.os == 'Linux'
        run: |
          sudo apt update
          sudo apt install -y r-base
          sudo apt install -y r-base libtirpc-dev
          mkdir -p $HOME/Rlibs 
          R -e "install.packages('RInside', lib='$HOME/Rlibs'); install.packages('Rcpp', lib='$HOME/Rlibs')"
          echo "R_LIBS=$HOME/Rlibs" >> ~/.Renviron
          R -e "library(RInside); library(Rcpp)"
          echo "R_LIBS_USER=$HOME/Rlibs" >> $GITHUB_ENV

      - name: Install SWI-Prolog on Linux
        if: runner.os == 'Linux'
        run: |
          sudo apt-add-repository ppa:swi-prolog/devel
          sudo apt-get update
          sudo apt-get install swi-prolog
        shell: bash

      - name: Check installation
        run: swipl -t check_installation
        shell: bash

      - name: Install rolog
        run: |
          export R_LIBS_USER=$HOME/Rlibs
          swipl -t "pack_install(rolog, [interactive(false)])"
        shell: bash

      - name: Move rolog.so to foreign directory
        run: |
          mkdir -p ~/.local/share/swi-prolog/pack/rolog/foreign
          cp ~/.local/share/swi-prolog/pack/rolog/lib/x86_64-linux/rolog.so ~/.local/share/swi-prolog/pack/rolog/foreign/

      - name: Install pack
        run: |
          export R_LIBS_USER=$HOME/Rlibs
          if [ "${{ github.event_name }}" = "pull_request" ]; then
            REPO_URL="https://github.com/${{ github.event.pull_request.head.repo.full_name }}.git"
          else
            REPO_URL="https://github.com/${{ github.repository }}.git"
          fi
          swipl -t "pack_install('$REPO_URL', [interactive(false), link(false)])"
        shell: bash
