---
name: "Build & Test MacPorts Base"

on:
  - push
  - pull_request

jobs:
  build:
    name: ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [macos-13, macos-14, macos-15, macos-26, ubuntu-latest]

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 1
      - name: Cleanup /usr/local
        if: startsWith(matrix.os,'macos')
        run: |
          sudo mkdir /opt/local.old
          sudo mv /usr/local/* /opt/local.old
      - name: Select Xcode version
        if: startsWith(matrix.os,'macos')
        run: |
          case "`uname -r`" in
            22.*) sudo xcode-select --switch /Applications/Xcode_14.3.1.app/Contents/Developer
                ;;
            23.*) sudo xcode-select --switch /Applications/Xcode_15.4.app/Contents/Developer
                ;;
          esac
      - name: Install Linux Dependencies
        if: startsWith(matrix.os,'ubuntu')
        env:
            MP_RELEASE_BRANCH: ${{ vars.MP_RELEASE_BRANCH }}
        run: |
          set -eu
          sudo apt update
          sudo apt install mtree-netbsd libcurl4-openssl-dev
          if [ "$MP_RELEASE_BRANCH" = "$GITHUB_REF_NAME" ]; then
            # Extra dep for building HTML man pages.
            sudo apt install --no-install-recommends asciidoc-base
          fi
      - name: Configure MacPorts Base
        run: |
          set -eu
          ./standard_configure.sh
      - name: Build MacPorts Base
        run: |
          set -eu
          platform=$(uname -s)
          case ${platform} in
            Darwin) make -j$(sysctl -n hw.activecpu)
            ;;
            Linux) make -j$(nproc)
            ;;
          esac
      - name: Install MacPorts Base
        run: |
          set -eu
          sudo make install
      - name: Test MacPorts Base
        if: startsWith(matrix.os,'macos')
        run: |
          set -eu
          make test
      - name: Deploy man pages to web
        if: vars.MP_RELEASE_BRANCH == github.ref_name && startsWith(matrix.os,'ubuntu')
        env:
          MANPAGE_SSH_HOST: ${{ secrets.MANPAGE_SSH_HOST }}
          MANPAGE_SSH_HOSTKEY: ${{ secrets.MANPAGE_SSH_HOSTKEY }}
          MANPAGE_SSH_USER: ${{ secrets.MANPAGE_SSH_USER }}
          MANPAGE_SSH_KEY: ${{ secrets.MANPAGE_SSH_KEY }}
        run: |
          set -eu
          make -C doc html ASCIIDOC=/usr/bin/asciidoc XSLTPROC=/usr/bin/xsltproc \
            DOCBOOK_XSL=/usr/share/xml/docbook/stylesheet/docbook-xsl/manpages/docbook.xsl
            
          touch ssh_key
          chmod 0600 ssh_key
          echo "$MANPAGE_SSH_KEY" > ssh_key
          echo "$MANPAGE_SSH_HOSTKEY" > ssh_known_hosts

          export RSYNC_RSH="ssh -l $MANPAGE_SSH_USER -i ssh_key -oUserKnownHostsFile=ssh_known_hosts"
          echo "Uploading manpage HTML files"
          rsync -avzhC --progress --delay-updates --delete-after doc/*.html "${MANPAGE_SSH_HOST}:./"
          rm -f ssh_key ssh_known_hosts
