name: Github Actions CI
on:
  pull_request:
  schedule:
    # Every friday
    - cron: '0 0 * * 5'
jobs:
  unit:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: Ubuntu
            os: ubuntu-latest
          - name: No FUSE
            os: ubuntu-latest
            configure_args: "--disable-fuse"
            check_features: demo
          - name: distcheck
            os: ubuntu-latest
          - name: Old distro
            os: ubuntu-22.04
          - name: macFUSE
            os: macos-15
          - name: FUSE-T
            os: macos-15
    env:
      CHECK_FEATURES: ${{ matrix.check_features }}
      CONFIGURE_ARGS: ${{ matrix.configure_args }}
    steps:
      - name: checkout
        uses: actions/checkout@v2
      - name: apt dependencies
        run: > 
          sudo apt-get update &&
          sudo apt-get install -y automake autoconf libtool pkgconf
          zlib1g-dev liblzo2-dev liblzma-dev liblz4-dev libzstd-dev
          squashfs-tools fio
        if: runner.os == 'Linux'
      - name: apt fuse 2
        run: sudo apt-get install -y libfuse-dev fuse
        if: matrix.os == 'ubuntu-22.04'
      - name: apt fuse 3
        run: sudo apt-get install -y libfuse3-dev fuse3
        if: matrix.os == 'ubuntu-latest'
      - name: homebrew dependencies
        env:
          HOMEBREW_NO_AUTO_UPDATE: 1
        run: |
          brew install autoconf automake libtool squashfs coreutils
          echo "LDFLAGS=-L$(brew --prefix)/lib" >> $GITHUB_ENV
          echo "CPPFLAGS=-I$(brew --prefix)/include" >> $GITHUB_ENV
        if: runner.os == 'macOS'
      - name: macfuse
        run: brew install --cask macfuse
        if: matrix.name == 'macFUSE'
      - name: fuse-t
        run: |
          find /usr/local -ls
          brew tap macos-fuse-t/homebrew-cask
          brew install fuse-t
        if: matrix.name == 'FUSE-T'
      - name: configure
        run: |
          ./autogen.sh
          env CPPFLAGS="-Werror $CPPFLAGS" ./configure $CONFIGURE_ARGS
      - name: check discovered features
        run: diff -u ci/expected-features/${CHECK_FEATURES:-all} ci/features
        if: matrix.name != 'distcheck'
      - name: build
        run: make -j2 V=1
        if: matrix.name != 'distcheck'
      - name: test
        run: make check
        # Can't accept security warning to allow macFUSE kext to work in CI
        if: matrix.name != 'macFUSE' && matrix.name != 'distcheck'
      - name: distcheck
        run: make distcheck
        if: matrix.name == 'distcheck'
      - name: install
        run: sudo make install
        if: matrix.name != 'distcheck'
      - name: output
        run: |
            cp /tmp/*.log . || true
            mksquashfs > mksquashfs.log || true
        if: always()
      - name: upload
        uses: actions/upload-artifact@v4
        with:
          name: logs ${{ matrix.name }}
          path: |
            *.log
            tests/*.log
            ci/features
        if: always()
  windows:
    runs-on: windows-2019
    steps:
      - name: install chocolatey dependencies
        shell: powershell
        run: choco install -y squashfs 
      - name: setup msbuild
        uses: microsoft/setup-msbuild@v2
      - name: checkout
        uses: actions/checkout@v2
      - name: generate header
        shell: bash
        run: ./gen_swap.sh squashfs_fs.h
      - name: build
        working-directory: ./win
        shell: powershell
        run: MSBuild.exe squashfuse_ls.vcxproj -p:PlatformToolset=v142
      - name: test
        working-directory: ./win
        shell: bash
        run: |
          set -x
          mkdir test
          touch test/foo test/bar test/'iggy blah'
          mksquashfs test test.squashfs
          ls test | sort > expected
          ./Debug/squashfuse_ls.exe test.squashfs | dos2unix | sort > actual
          diff -u expected actual
