#!/sbin/sh
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#
# SMF method for svc:/application/pkg/pkg6depotd. The daemon stays in the
# foreground (startd/duration=child); SMF stops it with :kill.

# shellcheck disable=SC1091
. /lib/svc/share/smf_include.sh

DEPOTD=/usr/lib/pkg6depotd
DEFAULT_CONFIG=/etc/pkg6depotd/pkg6depotd.kdl

getprop() {
	val=$(/usr/bin/svcprop -p "$1" "$SMF_FMRI" 2>/dev/null)
	# svcprop prints "" for an empty astring
	[ "$val" = '""' ] && val=
	printf '%s\n' "$val"
}

case "$1" in
start)
	config=$(getprop config/file)
	[ -n "$config" ] || config=$DEFAULT_CONFIG
	inst_root=$(getprop pkg/inst_root)
	port=$(getprop pkg/port)
	address=$(getprop pkg/address)
	tls_cert_file=$(getprop pkg/ssl_cert_file)
	tls_key_file=$(getprop pkg/ssl_key_file)
	[ -n "$port" ] || port=0

	# Empty/zero properties leave existing KDL-only instances unchanged.
	# Once an instance sets a repository or listener property, require the
	# complete pair so it cannot accidentally serve the KDL repository on
	# another port, or another repository on the KDL port.
	case "$port" in
	''|*[!0-9]*)
		echo "pkg6depotd: pkg/port must be a number from 0 to 65535"
		exit "$SMF_EXIT_ERR_CONFIG"
		;;
	esac
	# Keep shell arithmetic bounded even if an operator supplies a large
	# count property. SMF normally prints count values in decimal.
	if [ "${#port}" -gt 5 ]; then
		echo "pkg6depotd: pkg/port must be a number from 0 to 65535"
		exit "$SMF_EXIT_ERR_CONFIG"
	fi
	if [ -n "$inst_root" ] || [ "$port" -ne 0 ] || [ -n "$address" ]; then
		if [ -z "$inst_root" ] || [ "$port" -eq 0 ] ||
		    [ "$port" -gt 65535 ]; then
			echo "pkg6depotd: set both pkg/inst_root and pkg/port (1..65535)"
			exit "$SMF_EXIT_ERR_CONFIG"
		fi
		if [ ! -d "$inst_root" ] || [ ! -r "$inst_root" ] ||
		    [ ! -x "$inst_root" ]; then
			echo "pkg6depotd: repository $inst_root is missing or unreadable by $(/usr/bin/id -un)"
			exit "$SMF_EXIT_ERR_CONFIG"
		fi
		[ -n "$address" ] || address=127.0.0.1
		case "$address" in
		*:* ) bind="[$address]:$port" ;;
		* ) bind="$address:$port" ;;
		esac
		set -- --config "$config" --repository-root "$inst_root" --bind "$bind"
	else
		set -- --config "$config"
	fi
	if [ -n "$tls_cert_file" ] || [ -n "$tls_key_file" ]; then
		if [ -z "$tls_cert_file" ] || [ -z "$tls_key_file" ]; then
			echo "pkg6depotd: set both pkg/ssl_cert_file and pkg/ssl_key_file"
			exit "$SMF_EXIT_ERR_CONFIG"
		fi
		set -- "$@" --tls-cert-file "$tls_cert_file" --tls-key-file "$tls_key_file"
	fi

	# pkg6depotd itself refuses a missing configuration; checking here as
	# well reports SMF_EXIT_ERR_CONFIG with a clear log line.
	if [ ! -r "$config" ]; then
		echo "pkg6depotd: configuration $config is missing or" \
		    "unreadable by $(/usr/bin/id -un)"
		exit "$SMF_EXIT_ERR_CONFIG"
	fi

	log_filter=$(getprop config/log_filter)
	if [ -n "$log_filter" ]; then
		RUST_LOG=$log_filter
		export RUST_LOG
	fi

	# With duration=child SMF marks the service online as soon as this
	# method execs the daemon, before it parses the file. Validate first,
	# as the same user and with the same file, so a readable but invalid
	# configuration fails the start method and the instance goes to
	# maintenance instead of restarting in a loop. config-test prints only
	# a fixed summary (no credentials) and makes no ACME requests.
	if ! $DEPOTD "$@" config-test; then
		echo "pkg6depotd: configuration $config is invalid; fix it," \
		    "check this instance's KDL and pkg/* properties," \
		    "then run 'svcadm clear $SMF_FMRI'"
		exit "$SMF_EXIT_ERR_CONFIG"
	fi

	exec $DEPOTD "$@" start
	;;
*)
	echo "Usage: $0 start"
	exit "$SMF_EXIT_ERR_FATAL"
	;;
esac
