name: OS X Build

on:
    push:
      branches:
         - 'master'
         - 'osx-build'
         - 'heimdal-7-1-branch'
      paths:
         - '!docs/**'
         - '!**.md'
         - '!**.[1-9]'
         - '**.[chly]'
         - '**.hin'
         - '**.in'
         - '**.am'
         - '**.m4'
         - '**.ac'
         - '**.pl'
         - '**.py'
         - '**.asn1'
         - '**.opt'
         - '**/COPYING'
         - '**/INSTALL'
         - '**/README*'
         - '.github/workflows/osx.yml'

    pull_request:
      paths:
         - '!docs/**'
         - '!**.md'
         - '!**.[1-9]'
         - '**.[chly]'
         - '**.hin'
         - '**.in'
         - '**.am'
         - '**.m4'
         - '**.ac'
         - '**.pl'
         - '**.py'
         - '**.asn1'
         - '**.opt'
         - '**/COPYING'
         - '**/INSTALL'
         - '**/README*'
         - '.github/workflows/osx.yml'

jobs:
    osx:
        # Run if no [only X] tag, or if [only osx] is present (check both push commits and PR titles)
        if: >-
            (!contains(github.event.head_commit.message, '[only ') &&
             !contains(github.event.pull_request.title, '[only ')) ||
            contains(github.event.head_commit.message, '[only osx]') ||
            contains(github.event.pull_request.title, '[only osx]')
        runs-on: ${{ matrix.os }}
        strategy:
            fail-fast: false
            matrix:
                name: [osx-clang]
                include:
                    - name: osx-clang
                      os: macos-latest
                      compiler: clang
        steps:
            - name: Install packages
              run: |
                echo "bison, flex, ncurses, texinfo, and unzip are in the base OS."
                echo "berkeley-db, perl, python3, curl, and jq are installed in the"
                echo "base image already."
                brew install autoconf automake bison libtool cpanm texinfo texi2html bash openssl@3
                sudo cpanm install JSON
                # Use Homebrew bash for tests (much faster than /bin/bash on macOS)
                echo "$(brew --prefix)/bin" >> $GITHUB_PATH
            - name: Clone repository
              uses: actions/checkout@v4
            - name: Build
              env:
                CC: ${{ matrix.compiler }}
                MAKEVARS: ${{ matrix.makevars }}
                CONFIGURE_OPTS:  ${{ matrix.configureopts }}
              run: |
                /bin/sh ./autogen.sh
                mkdir build
                cd build
                ../configure --srcdir=`dirname "$PWD"` --disable-heimdal-documentation --enable-maintainer-mode --enable-developer $CONFIGURE_OPTS --prefix=$HOME/inst --with-openssl=/opt/homebrew/opt/openssl@3/ CFLAGS="-O0 -g -ggdb3 -Wno-error=shadow -Wno-error=bad-function-cast -Wno-error=unused-function -Wno-error=unused-result -Wno-error=deprecated-declarations"
                ulimit -c unlimited
                PATH=/usr/local/opt/texinfo/bin:$PATH
                export PATH
                make -j4
            #- name: Setup upterm session
            #  uses: lhotari/action-upterm@v1
            #  with:
            #      limit-access-to-actor: true
            - name: Test
              timeout-minutes: 25
              shell: bash
              env:
                CC: ${{ matrix.compiler }}
                MAKEVARS: ${{ matrix.makevars }}
                CONFIGURE_OPTS:  ${{ matrix.configureopts }}
              run: |
                set -vx
                # Verify we're using Homebrew bash (faster than system bash)
                echo "Using bash: $(which bash) version $BASH_VERSION"
                sudo lsof -nP -i:49188 || true
                cd build
                make check
            - name: Interrupted/timed out test diagnostics
              if: ${{ cancelled() || failure() }}
              run: |
                set -x
                # Show any test-related processes still running
                ps auxww | grep -E 'kdc|test_context|kinit|kadmin|check-' | grep -v grep || true
                # Show who's on the test port
                sudo lsof -nP -i:49188 || true
                # Show the newest .log file (the one likely interrupted)
                newest=$(find build/tests -name '*.log' -newer build/tests/gss/Makefile -print0 2>/dev/null | xargs -0 stat -f '%m %N' 2>/dev/null | sort -rn | head -1 | cut -d' ' -f2-)
                if [ -n "$newest" ]; then
                  echo "=== Newest test log: $newest ==="
                  cat "$newest"
                fi
                # Also show KDC messages.log if any exist
                for f in build/tests/gss/messages.log build/tests/kdc/messages.log; do
                  if [ -f "$f" ]; then
                    echo "=== $f ==="
                    tail -100 "$f"
                  fi
                done
            - name: Install
              run: |
                cd build || true
                make DESTDIR=/tmp/h5l install
                cd /tmp/h5l
                tar czf $HOME/heimdal-install-osx.tgz .
            - name: Test logs
              if: ${{ always() }}
              run: |
                find build -depth -name \*.trs|xargs grep -lw FAIL|sed -e 's/trs$/log/' | cpio -o > $HOME/logs-osx.cpio
                find build -name \*.trs|xargs grep -lw FAIL|sed -e 's/trs$/log/'|xargs cat
            - name: Failed Test logs
              if: ${{ failure() }}
              run: |
                find build -name \*.trs|xargs grep -lw FAIL|sed -e 's/trs$/log/'|xargs cat
            - name: Upload Install Tarball
              uses: actions/upload-artifact@v4
              with:
                name: Install Tarball (osx)
                path: '~/heimdal-install-osx.tgz'
            - name: Upload Artifacts
              uses: actions/upload-artifact@v4
              with:
                name: Test Logs (osx)
                path: '~/logs-osx.cpio'
