#!/bin/sh
# jpps-freepbx-module -- install or remove the JPPS Mailer FreePBX module.
#
#   jpps-freepbx-module install   trust the JPPS module signing key, copy the module into
#                                 the FreePBX web root (unless Module Admin already has a
#                                 newer version there), install it, and create the request
#                                 directory its page writes to
#   jpps-freepbx-module remove    uninstall the module and delete its files (the Postfix
#                                 relay it set up keeps working; see jpps-freepbx-mailer)
#
# Run by the package's postinst/prerm. Safe to re-run.
set -eu

SRC=/usr/share/jpps-freepbx/modules/jppsmailer
REQDIR=/var/lib/jpps-freepbx/mailer
KEYFILE=/usr/share/jpps-freepbx/jpps-modules.asc
KEYFPR=D4CE088B7F8831F13F18A5FE5A0911A28985815C

have_freepbx() { command -v fwconsole >/dev/null 2>&1; }

get_setting() {
	fwconsole setting "$1" 2>/dev/null | sed -n 's/^Setting of "[^"]*" is ([a-z]*)\[\(.*\)\]$/\1/p' | tail -n 1
}

webroot() { w=$(get_setting AMPWEBROOT); echo "${w:-/var/www/html}"; }
webuser() { u=$(get_setting AMPASTERISKWEBUSER); echo "${u:-asterisk}"; }

# FreePBX checks module.sig against the web user's own GPG keyring (~/.gnupg) and
# accepts a signature from any key marked ultimately trusted there. Import ours as
# that user so the keyring's ownership stays right.
trust_key() {
	user=$1
	home=$(getent passwd "$user" | cut -d: -f6)
	[ -n "$home" ] && [ -f "$KEYFILE" ] || return 0
	# As that user, never root: their home is theirs to fill with symlinks.
	runuser -u "$user" -- mkdir -p -m 0700 "$home/.gnupg"
	runuser -u "$user" -- gpg --homedir "$home/.gnupg" --batch --quiet --import "$KEYFILE" 2>/dev/null
	echo "$KEYFPR:6:" | runuser -u "$user" -- gpg --homedir "$home/.gnupg" --batch --quiet --import-ownertrust 2>/dev/null
}

untrust_key() {
	user=$1
	home=$(getent passwd "$user" | cut -d: -f6)
	[ -n "$home" ] && [ -d "$home/.gnupg" ] || return 0
	runuser -u "$user" -- gpg --homedir "$home/.gnupg" --batch --yes --quiet --delete-keys "$KEYFPR" 2>/dev/null || true
}

module_version() {
	sed -n 's#.*<version>\(.*\)</version>.*#\1#p' "$1/module.xml" 2>/dev/null | head -n 1
}

install_module() {
	dest="$(webroot)/admin/modules/jppsmailer"
	user=$(webuser)
	install -d -m 0755 /var/lib/jpps-freepbx
	install -d -m 0700 -o "$user" -g "$user" "$REQDIR"
	trust_key "$user"
	have=$(module_version "$dest")
	ours=$(module_version "$SRC")
	if [ -n "$have" ] && php -r 'exit(version_compare($argv[1], $argv[2], ">") ? 0 : 1);' "$have" "$ours"; then
		echo "jpps-freepbx: JPPS Mailer $have (from Module Admin) is newer than the packaged $ours; keeping it"
	else
		# The modules directory belongs to the web user, so copy as that user:
		# root writing there could be redirected by a planted symlink.
		rm -rf "$dest"
		runuser -u "$user" -- cp -R --no-preserve=ownership "$SRC" "$dest"
	fi
	fwconsole ma install jppsmailer >/dev/null
	fwconsole reload >/dev/null
	echo "jpps-freepbx: JPPS Mailer module installed (Settings > JPPS Mailer)"
}

remove_module() {
	dest="$(webroot)/admin/modules/jppsmailer"
	if [ -d "$dest" ]; then
		fwconsole ma uninstall jppsmailer >/dev/null 2>&1 || true
		fwconsole ma delete jppsmailer >/dev/null 2>&1 || rm -rf "$dest"
		rm -rf "$dest"
		fwconsole reload >/dev/null 2>&1 || true
	fi
	rm -rf "$REQDIR"
	untrust_key "$(webuser)"
	echo "jpps-freepbx: JPPS Mailer module removed"
}

case "${1:-}" in
	install|remove)
		if ! have_freepbx; then
			echo "jpps-freepbx-module: fwconsole not found -- is FreePBX installed? Nothing to $1." >&2
			exit 0
		fi
		if [ "$1" = install ]; then install_module; else remove_module; fi ;;
	*)
		echo "usage: $(basename "$0") install|remove" >&2
		exit 2 ;;
esac
