name: Builds, tests & co

on:
  push:
  pull_request:
  schedule:
    - cron: 0 1 * * MON

jobs:
  build-and-test:
    strategy:
      fail-fast: false
      matrix:
        os:
          - ubuntu-latest
          - macos-latest
          # - windows-latest
        ocaml-compiler:
          - 4

    runs-on: ${{ matrix.os }}

    steps:
      - name: Checkout tree
        uses: actions/checkout@v4

      # - name: Set SHELLOPTS=igncr on Windows
      #   if: runner.os == 'Windows'
      #   run: echo "SHELLOPTS=igncr" >>"$GITHUB_ENV"

      - name: Restore fonts cache
        uses: actions/cache@v4
        id: fonts-cache
        with:
          path: lib-satysfi/dist/fonts
          key: cache-dist-fonts-${{ hashFiles('download-fonts.sh') }}

      - name: Set-up OCaml
        uses: ocaml/setup-ocaml@v3
        with:
          ocaml-compiler: ${{ matrix.ocaml-compiler }}
          dune-cache: true
          opam-repositories: |
            satysfi-external: git+https://github.com/gfngfn/satysfi-external-repo.git
            default: git+https://github.com/ocaml/opam-repository.git

      - name: Install SATySFi dependencies
        run: opam install . --deps-only

      - name: Build SATySFi
        run: opam exec -- make all

      - name: Run tests
        run: opam exec -- dune runtest

      - name: Download fonts
        if: steps.fonts-cache.outputs.cache-hit != 'true'
        run: ./download-fonts.sh

      - name: Install SATySFi
        run: |
          opam install .
          ./install-libs.sh ~/.satysfi

      - name: Build demo docs
        run: |
          (cd demo; opam exec -- make)
          (cd doc; opam exec -- make)
          (cd tests; opam exec -- make)

  lint-fmt:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout tree
        uses: actions/checkout@v4
      - name: Set-up OCaml
        uses: ocaml/setup-ocaml@v3
        with:
          ocaml-compiler: 4
          opam-repositories: |
            satysfi-external: git+https://github.com/gfngfn/satysfi-external-repo.git
            default: git+https://github.com/ocaml/opam-repository.git
      - uses: ocaml/setup-ocaml/lint-fmt@v3

  lint-opam:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout tree
        uses: actions/checkout@v4
      - name: Set-up OCaml
        uses: ocaml/setup-ocaml@v3
        with:
          ocaml-compiler: 4
          opam-repositories: |
            satysfi-external: git+https://github.com/gfngfn/satysfi-external-repo.git
            default: git+https://github.com/ocaml/opam-repository.git
      - uses: ocaml/setup-ocaml/lint-opam@v3
