on:
  push:
    branches:
     - master
  pull_request:
name: Check Irssi
env:
  apt_build_deps: libutf8proc-dev libperl-dev libotr5-dev
  apt_build_deps_meson: ninja-build
  apt_build_deps_autotools: autoconf automake libtool
  get_pip_build_deps_meson: pip3 install setuptools${setuptools_ver}; pip3 install wheel; pip3 install meson${meson_ver}
  build_options_meson: -Dwith-proxy=yes -Dwith-bot=yes -Dwith-perl=yes -Dwith-otr=yes
  build_options_configure: --with-proxy=module --with-bot --with-perl=module --with-otr=module
  prefix: ~/irssi-build
jobs:
  dist:
    runs-on: ubuntu-latest
    steps:
      - name: prepare required software
        run: |
          sudo apt install $apt_build_deps $apt_build_deps_autotools
      - uses: actions/checkout@main
      - name: make dist
        run: |
          # make dist
          ./autogen.sh $build_options_configure
          make dist
      - uses: actions/upload-artifact@v2
        with:
          path: irssi-*.tar.gz
          retention-days: 1
  install:
    runs-on: ${{ matrix.os }}
    env:
      CC: ${{ matrix.compiler }}
    needs: dist
    continue-on-error: ${{ contains(matrix.flags, 'FAILURE-OK') }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-16.04, ubuntu-latest]
        builder: [meson, configure]
        compiler: [clang, gcc]
        flags: [regular]
        include:
          - os: ubuntu-16.04
            builder: meson
            meson_ver: ==0.49.2
            setuptools_ver: <51
          - os: ubuntu-latest
            builder: meson
            meson_ver: <0.59.0
          - os: ubuntu-latest
            builder: meson
            flags: meson-latest FAILURE-OK
    steps:
      - name: fetch dist
        uses: actions/download-artifact@v2
      - name: set PATH
        run: |
          echo "$HOME/.local/bin" >> $GITHUB_PATH
      - name: prepare required software
        env:
          meson_ver: ${{ matrix.meson_ver }}
          setuptools_ver: ${{ matrix.setuptools_ver }}
        run: |
          sudo apt install $apt_build_deps $apt_build_deps_${{ matrix.builder }}
          eval "$get_pip_build_deps_${{ matrix.builder }}"
          curl -SLf https://github.com/irssi-import/actions-irssi/raw/master/check-irssi/render.pl -o ~/render.pl && chmod +x ~/render.pl
      - name: unpack archive
        run: tar xaf artifact/irssi-*.tar.gz
      - name: build and install with meson
        run: |
          # ninja install
          cd irssi-*/
          meson Build $build_options_meson --prefix=${prefix/\~/~}
          ninja -C Build
          ninja -C Build install
        if: ${{ matrix.builder == 'meson' }}
      - name: build and install with Makefile
        run: |
          # make install
          cd irssi-*/
          mkdir Build
          cd Build
          ../configure $build_options_configure --prefix=${prefix/\~/~} --enable-always-build-tests
          make CFLAGS="-Wall -Werror -Werror=declaration-after-statement"
          make install
        if: ${{ matrix.builder == 'configure' }}
      - name: run tests with Makefile
        run: |
          # make check
          cd irssi-*/Build
          make -C tests -sk check
          find -name test-suite.log -exec cat {} +
        if: ${{ matrix.builder == 'configure' }}
      - name: run tests with Meson
        run: |
          # ninja test
          cd irssi-*/
          ninja -C Build test
          find -name testlog.txt -exec sed -i -e '/Inherited environment:.* GITHUB/d' {} + -exec cat {} +
        if: ${{ matrix.builder == 'meson' }}
      - name: run launch test
        env:
          TERM: xterm
        run: |
          # automated irssi launch test
          cd
          mkdir irssi-test
          echo 'echo automated irssi launch test
          ^set settings_autosave off
          ^set -clear log_close_string
          ^set -clear log_day_changed
          ^set -clear log_open_string
          ^set log_timestamp * 
          ^window log on' > irssi-test/startup
          echo load perl >> irssi-test/startup
          echo load proxy >> irssi-test/startup
          echo ^quit >> irssi-test/startup
          irssi-build/bin/irssi --home irssi-test | perl -Mutf8 -C ~/render.pl
          cat irc.log.*
