#!/bin/sh
#
# probe-pkgrepo <fqdn> [timeout]
#
# One verdict for a package repository served over https: the index answers, and it lists at
# least one package. pkg_add enumerates a remote repository from the <a href="*.tgz"> of that
# page [verifie:openbsd/src usr.sbin/pkg_add/OpenBSD/PackageRepository.pm get_http_list], so an
# index that answers empty is a repository nothing can be installed from, and it answers 200 all
# the same -- an outage this probe is the only thing that sees.
#
# Silent when fine (rc 0, no output); one summary line on stderr otherwise (probe contract,
# silence on success, a single line naming the fault otherwise). Read-only, no root, no doas: it
# fetches a public page. Tools: sh, ftp (base, https), hn-timeout (sysutils/hn-timeout).
#
# Copyright (c) 2026 Jean-Gerard Pailloncy
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

set -u
export LC_ALL=C
# ksh imports the caller PATH over its own default and never puts it back [verifie:openbsd/src
# bin/ksh/main.c:220-226, :272-274]; /bin/sh IS ksh on OpenBSD [verifie:openbsd/src bin/ksh/
# Makefile, LINKS+= ${BINDIR}/ksh ${BINDIR}/sh], so the same import applies here.
# /usr/local/bin last, for hn-timeout alone (sysutils/hn-timeout): timeout(1) of base with its
# signals blocked before fork(), where base blocks them after and a command that exits in between
# leaves timeout asleep until its deadline. Last, so that no package shadows a base tool.
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin; export PATH

FQDN=${1:-}
TMO=${2:-15}
[ -n "$FQDN" ] || { echo "usage: ${0##*/} <fqdn> [timeout]" >&2; exit 2; }

URL="https://$FQDN/"

# -M drops the progress meter, -V the transfer statistics, -o - puts the body on stdout
# [verifie:man.openbsd.org/ftp.1]. hn-timeout bounds the WHOLE fetch: the -w of ftp bounds the
# connection alone, and a server that accepts then says nothing would hold the caller. 124 when
# it fired, the status of timeout(1) [verifie:man.openbsd.org/timeout.1, EXIT STATUS].
index=$(hn-timeout "$TMO" ftp -MVo - "$URL" 2>/dev/null)
rc=$?
if [ "$rc" != 0 ]; then
	case $rc in
	124) echo "package repo index timed out: $URL" >&2 ;;
	*)   echo "package repo index unreachable: $URL" >&2 ;;
	esac
	exit 1
fi

# The automatic listing of httpd renders each entry as <a href="<name>">. BRE, one pattern, no
# alternation -- OpenBSD's grep has none [verifie:re_format(7)].
n=$(printf '%s\n' "$index" | grep -c 'href="[^"]*\.tgz"')
if [ "$n" = 0 ]; then
	echo "package repo index lists no package: $URL" >&2
	exit 1
fi

exit 0
