#! /bin/bash

set -e

LOGO_SVG=/usr/share/plinth/static/themes/default/img/freedombox-logo-powered.svg
FORKAWESOME_CSS=/usr/share/fonts-fork-awesome/css/fork-awesome.css
MAIN_CSS=/usr/share/plinth/static/themes/default/css/main.css

FBOX_DIVERSIONS=(
	"$LOGO_SVG"
	"$FORKAWESOME_CSS"
	"$MAIN_CSS"
)

FBOX_MODULES_DIR=/usr/share/freedombox/modules-enabled
FBOX_MODULES_HIDE=(deluge minetest shadowsocks shadowsocksserver transmission)

# Divert/undivert original file $2 to/from location $3.
# $1 indicates the action to take; either --add or --remove
divert() {
	dpkg-divert --package pqccomms-server "$1" --rename \
		--divert "$3" "$2"
}

# Divert or undivert all diversions
# $1 indicates the action to take; either --add or --remove
divert_all() {
	for d in "${FBOX_DIVERSIONS[@]}"; do
		divert "$1" "$d" "$d.orig"
	done
	for m in "${FBOX_MODULES_HIDE[@]}"; do
		divert "$1" "$FBOX_MODULES_DIR/$m" "$FBOX_MODULES_DIR/.$m"
	done
}

# Check if this package has diverted the specified file
have_diverted() {
	[ "$(dpkg-divert --listpackage "$1")" = pqccomms-server ]
}

# Substitute a diverted file with a sed expression
make_subst_sed() {
	local file exp
	file="$1"
	exp="$2"
	# For forward compatibility, the package can mention
	# 'pqccomms-server' to stop us from doing this substitution.
	# (The file is still diverted though, so copy it as-is in that
	# case.)
	if grep -q pqccomms-server "$file.orig"; then
		cp -f "$file.orig" "$file"
	else
		sed -e "$exp" <"$file.orig" >"$file"
	fi
}

# Substitute a diverted file by copying a different file to replace it
make_subst_cp() {
	local file source
	file="$1"
	source="$2"
	cp -f "$2" "$1"
}

# Update a substitution in file "$1" diverted to "$1.orig".  'how' is either
# 'sed' (for make_subst_sed) or 'cp' (for make_subst_cp).  'source' is a sed
# expression or source file as needed by the method.
update_subst() {
	local file how source
	file="$1"
	how="$2"
	source="$3"
	if have_diverted "$file"; then
		if [ -f "$file.orig" ]; then
			"make_subst_$how" "$file" "$source"
		else
			# We have the diversion, but the source doesn't exist;
			# the package might not be installed.  Remove our
			# generated file if there is one.
			rm -f "$file"
		fi
	fi
}

update_all_subst() {
	update_subst "$LOGO_SVG" cp /usr/share/pqccomms-server/purism-logo-v.svg
	update_subst "$FORKAWESOME_CSS" sed 's/\\f2fd/\\2302/'
	update_subst "$MAIN_CSS" sed \
		's/--freedombox-blue-color: #[0-9a-fA-F]*;/--freedombox-blue-color: #383838;/'
}

# Remove a substituted file "$1" if we made the diversion
remove_subst() {
	if have_diverted "$1"; then
		rm -f "$1"
	fi
}

remove_all_subst() {
	for f in "${FBOX_DIVERSIONS[@]}"; do
		remove_subst "$f"
	done
}

# If we either divert or undivert the hidden modules, and plinth is installed,
# we need to restart plinth.
MODULES_WERE_DIVERTED=
if have_diverted "$FBOX_MODULES_DIR/${FBOX_MODULES_HIDE[0]}"; then
	MODULES_WERE_DIVERTED=y
fi

# preinst, postinst, and postrm are the same script since they share a lot of
# logic.
case "$(basename "$0")" in
	preinst)
		# If you don't want these branding changes, create
		# /etc/pqccomms-server-no-branding and reinstall this package.
		if ! [ -f /etc/pqccomms-server-no-branding ]; then
			divert_all --add
			# Updating substitutions happens in postinst so the new
			# icon is unpacked
		else
			remove_all_subst
			divert_all --remove
		fi
		;;
	pqccomms-server.postinst)
		case "$1" in
		triggered|configure)
			update_all_subst
			;;
		esac
		;;
	pqccomms-server.postrm)
		case "$1" in
		remove|purge|disappear|abort-install|abort-upgrade)
			if [ "$1" != abort-upgrade ] || dpkg --compare-versions "$2" lt 0.2; then
				remove_all_subst
				divert_all --remove
			fi
			;;
		esac
		;;
esac

MODULES_ARE_DIVERTED=
if have_diverted "$FBOX_MODULES_DIR/${FBOX_MODULES_HIDE[0]}"; then
	MODULES_ARE_DIVERTED=y
fi

if [ "$MODULES_WERE_DIVERTED" != "$MODULES_ARE_DIVERTED" ]; then
	echo "Module diversions changed; restarting plinth if running..." >&2
	systemctl try-restart plinth.service &>/dev/null || true
fi

#DEBHELPER#
