name: run Lmod tests
on: [push, pull_request]

jobs:
  Lmod_tests:
      runs-on: ${{ matrix.os }}
      strategy:
        matrix:
          os: [ubuntu-latest, macos-latest]
          luaVersion: ["5.1", "5.2", "5.3", "5.4"]
      steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: install dependencies MacOS
        if: matrix.os == 'macos-latest'
        run: brew install coreutils gnu-sed
      - name: install dependencies Linux
        if: matrix.os == 'ubuntu-latest'
        run: sudo apt install tcsh tcl tcl-dev uuid r-base r-base-dev cmake fish tclsh zsh ksh
      - name: set up lua
        uses: leafo/gh-actions-lua@v8.0.0
        with:
          luaVersion: ${{ matrix.luaVersion }}
      - name: install luarocks
        uses: leafo/gh-actions-luarocks@v4.0.0
      - name: install lua dependencies
        run: |
          luarocks install luaposix
          luarocks install luafileSystem
          luarocks install luajson
          luarocks install lua-term
          luarocks install busted
      - name: set up Hermes
        run: |
         cd /tmp
         git clone https://github.com/rtmclay/Hermes.git
         echo "$PWD/Hermes/bin" >> $GITHUB_PATH
         cd -
      - name: Run tests
        run: tm
      - name: show output of failed Lmod tests
        if: ${{ failure() }}
        run: |
         set +e
         for dir in $(ls -pd rt/* | grep '/$'); do
           diff -c ${dir}t1/*/_err.left ${dir}/t1/*/_err.right > /dev/null 2>&1
           if [ $? -ne 0 ]; then
             echo ">>>> ${dir}/err.txt"
             diff -c ${dir}t1/*/_err.left ${dir}/t1/*/_err.right || echo
             echo ">>>> ${dir}/out.txt"
             diff -c ${dir}/out.txt ${dir}/t1/*/out.txt || echo
             echo ">>>> ${dir}/t1.txt"
             cat ${dir}t1/*/t1.log
           fi
         done
      - name: Lmod_test_suite
        run: |
         set -e
         mkdir b0
         cd b0
         ../configure --prefix=/tmp
         make install
         sudo mkdir -p /etc/profile.d
         sudo cp /tmp/lmod/lmod/init/profile /etc/profile.d/z00_lmod.sh
         cd -
         cd /tmp
         git clone https://github.com/rtmclay/Lmod_test_suite.git
         cd -
         cd /tmp/Lmod_test_suite
         sudo cp z98_StdEnv.sh /etc/profile.d
         tm -k normal
      - name: show output of failed suite tests
        if: ${{ failure() }}
        run: |
         cd /tmp/Lmod_test_suite
         set +e
         for dir in $(ls -pd rt/* | grep '/$'); do
           diff -c ${dir}t1/*/_err.left ${dir}/t1/*/_err.right > /dev/null 2>&1
           if [ $? -ne 0 ]; then
             echo ">>>> ${dir}/err.txt"
             diff -c ${dir}t1/*/_err.left ${dir}/t1/*/_err.right || echo
             echo ">>>> ${dir}/_err.left (gold)"
             cat ${dir}t1/*/_err.left
             echo ">>>> ${dir}/_err.right"
             cat ${dir}t1/*/_err.right
             echo ">>>> ${dir}/t1.txt"
             cat ${dir}t1/*/t1.log
           fi
         done

