#!/bin/sh
# Points sudo at c8-askpass in /etc/sudo.conf ("Path askpass"), for sudo -A
# wherever SUDO_ASKPASS isn't passed down. The rest of the file is left
# as it is; run again, it only updates the path. Packages (DESTDIR) are
# left alone: /etc/sudo.conf is the admin's, not a package's.
set -eu
askpass=$1
if [ -n "${DESTDIR:-}" ]; then
	echo "c8-askpass: not touching sudo.conf in a DESTDIR install;" \
		"add \"Path askpass $askpass\" to /etc/sudo.conf to use it for sudo -A"
	exit 0
fi
conf=/etc/sudo.conf
if [ ! -e "$conf" ]; then
	# Some distributions ship theirs in /usr/etc; /etc then replaces it,
	# so start from it
	if [ -e /usr/etc/sudo.conf ]; then
		cp /usr/etc/sudo.conf "$conf"
	else
		: > "$conf"
	fi
	chmod 644 "$conf"
fi
if grep -Eq '^[[:space:]]*Path[[:space:]]+askpass[[:space:]]' "$conf"; then
	sed -i -E "s|^[[:space:]]*Path[[:space:]]+askpass[[:space:]].*|Path askpass $askpass|" "$conf"
else
	printf '\n# The password window for sudo -A (cont8nuum)\nPath askpass %s\n' \
		"$askpass" >> "$conf"
fi
echo "c8-askpass: sudo -A asks with $askpass ($conf)"
