#!/bin/sh
# jpps-freepbx-branding -- apply / remove JPPS branding on a FreePBX 17 system.
#
#   jpps-freepbx-branding apply    copy theme assets into the FreePBX web root and
#                                  point FreePBX's BRAND_* settings at them
#   jpps-freepbx-branding reset    restore FreePBX's default branding (only for
#                                  settings still pointing at JPPS values)
#   jpps-freepbx-branding status   show the current BRAND_* values
#
# Called automatically by the package's postinst (apply) and prerm (reset).
# Safe to re-run any time, e.g. after a FreePBX reinstall wiped the settings.
set -eu

SRC=/usr/share/jpps-freepbx-branding
SUBDIR=assets/jpps-branding            # relative to <webroot>/admin, as FreePBX expects
TITLE="JPPS PBX Administration"
ALT="Jeff Parrish PC Services LLC"
LINK="https://www.jpps.us"
KEYS="BRAND_CSS_CUSTOM BRAND_IMAGE_TANGO_LEFT BRAND_IMAGE_FAVICON BRAND_TITLE BRAND_FREEPBX_ALT_LEFT BRAND_IMAGE_FREEPBX_LINK_LEFT"

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

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

set_setting() {
	fwconsole setting "$1" "$2" >/dev/null
}

reset_setting() {
	fwconsole setting -n --reset "$1" >/dev/null
}

pkg_version() {
	dpkg-query -W -f='${Version}' jpps-freepbx 2>/dev/null || echo dev
}

webroot() {
	w=$(get_setting AMPWEBROOT)
	echo "${w:-/var/www/html}"
}

webuser() {
	u=$(get_setting AMPASTERISKWEBUSER)
	echo "${u:-asterisk}"
}

apply() {
	dest="$(webroot)/admin/$SUBDIR"
	user=$(webuser)
	install -d -m 0755 "$dest"
	for f in jpps.css logo-navbar.svg favicon.ico; do
		install -m 0644 "$SRC/$f" "$dest/$f"
	done
	if id "$user" >/dev/null 2>&1; then
		chown -R "$user:$user" "$dest"
	fi

	# ?v= busts browser caches when the theme changes between package versions
	# (FreePBX appends its own ?load_version tag, which doesn't change for us).
	ver=$(pkg_version | tr -c 'A-Za-z0-9.~+-' '_')
	set_setting BRAND_CSS_CUSTOM "$SUBDIR/jpps.css?v=$ver"
	set_setting BRAND_IMAGE_TANGO_LEFT "$SUBDIR/logo-navbar.svg"
	set_setting BRAND_IMAGE_FAVICON "$SUBDIR/favicon.ico"
	set_setting BRAND_TITLE "$TITLE"
	set_setting BRAND_FREEPBX_ALT_LEFT "$ALT"
	set_setting BRAND_IMAGE_FREEPBX_LINK_LEFT "$LINK"
	echo "jpps-freepbx-branding: JPPS branding applied ($dest)"
}

reset() {
	for key in $KEYS; do
		cur=$(get_setting "$key")
		case "$key:$cur" in
			*:*jpps-branding*|BRAND_TITLE:"$TITLE"|BRAND_FREEPBX_ALT_LEFT:"$ALT"|BRAND_IMAGE_FREEPBX_LINK_LEFT:"$LINK")
				reset_setting "$key" ;;
			*)
				[ -n "$cur" ] && echo "jpps-freepbx-branding: leaving $key alone (customized: $cur)" ;;
		esac
	done
	rm -rf "$(webroot)/admin/$SUBDIR"
	echo "jpps-freepbx-branding: FreePBX default branding restored"
}

status() {
	for key in $KEYS; do
		printf '%-32s %s\n' "$key" "$(get_setting "$key")"
	done
}

cmd="${1:-}"
case "$cmd" in
	apply|reset|status)
		if ! have_freepbx; then
			echo "jpps-freepbx-branding: fwconsole not found -- is FreePBX installed? Nothing to $cmd." >&2
			[ "$cmd" = status ] && exit 1
			exit 0
		fi
		"$cmd" ;;
	*)
		echo "usage: $(basename "$0") apply|reset|status" >&2
		exit 2 ;;
esac
