name: CI

on:
  - pull_request
  - push

jobs:
  build:
    name: Build
    strategy:
      matrix:
        os:
          - 'ubuntu-latest'
#         - 'macos-latest'
        ocaml-version:
          - 4.11.2
          - 4.12.1
          - 4.13.1
          - 4.14.1
        with-satysfi:
          - false
        oldest-dependencies:
          - true
          - false
    runs-on: ${{ matrix.os }}
    env:
      OPAMSOLVERTIMEOUT: 3600
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Setup OCaml ${{ matrix.ocaml-version }}
        uses: ocaml/setup-ocaml@v2
        with:
          ocaml-compiler: ${{ matrix.ocaml-version }}
          dune-cache: true

          cache-prefix: "oldestdeps-${{ matrix.oldest-dependencies }}-satysfi-${{ matrix.with-satysfi }}"

      - name: Check validity of the snapshot OPAM files
        run: |
          opam lint --strict *.opam

      - name: Add SATySFi-related repos
        if: matrix.with-satysfi
        run: |
          opam repository add satysfi-external https://github.com/gfngfn/satysfi-external-repo.git
          opam repository add satyrographos https://github.com/na4zagin3/satyrographos-repo.git

      - name: Install SATySFi
        if: matrix.with-satysfi
        run: |
          # opam depext satysfi --yes --with-doc --with-test
          # opam install satysfi --yes --with-doc --with-test
          opam depext satysfi --yes
          opam install satysfi --yes

      - name: Install Satyrographos
        if: ${{ ! matrix.oldest-dependencies }}
        run: |
          opam install satyrographos --yes --with-doc


      - name: Install Satyrographos (oldest dependencies)
        if: matrix.oldest-dependencies
        run: |
          OCAML_PACKAGE="ocaml.$(opam show --color=never -f version ocaml)"
          OCAML_BASE_COMPILER="ocaml-base-compiler.$(opam show --color=never -f version ocaml-base-compiler)"
          SATYROGRAPHOS_PACKAGE=satyrographos
          # export OPAMEXTERNALSOLVER=builtin-0install
          # export OPAMCRITERIA=+removed,+count[version-lag,solution]
          opam install opam-0install
          opam install --yes $(opam exec -- opam-0install --prefer-oldest "$SATYROGRAPHOS_PACKAGE" "$OCAML_PACKAGE" "$OCAML_BASE_COMPILER")


      - name: Test Satyrographos
        run: |
          opam lint
          opam exec -- dune runtest
