#!/bin/sh
# Makes the system see cont8nuum in /opt/cont8nuum, from a release
# tarball: run once after unpacking (and again after updating), as root.
#
#   sudo /opt/cont8nuum/setup           set it up
#   sudo /opt/cont8nuum/setup --remove  undo it (then remove /opt/cont8nuum)
#
# Links, so an update of /opt/cont8nuum needs no new setup: the session
# entry, the session's systemd user units, the D-Bus services (Files as
# the file manager and file picker), the font and, if SDDM is there, its
# theme. Copies, where the reader wants a file of its own: the lock
# screen's PAM rules, the polkit action for the sign-in picture, the
# askpass for every session, Files' portal, the portal-wlr settings and
# the XR glasses' udev rule. And sudo -A asks with c8-askpass
# (/etc/sudo.conf's "Path askpass"; the rest of the file is kept).
set -eu
p=/opt/cont8nuum
[ "$(id -u)" = 0 ] || { echo "setup: run it as root: sudo $0 $*" >&2; exit 1; }
[ -x "$p/bin/c8" ] || { echo "setup: cont8nuum isn't in $p" >&2; exit 1; }
remove=no
case "${1:-}" in
--remove) remove=yes ;;
'') ;;
*) echo "usage: $0 [--remove]" >&2; exit 2 ;;
esac

link() { # what, where
	if [ $remove = yes ]; then
		[ -L "$2" ] && [ "$(readlink "$2")" = "$1" ] && rm -f "$2" && echo "removed $2"
		return 0
	fi
	mkdir -p "$(dirname "$2")"
	ln -sfn "$1" "$2"
	echo "$2 -> $1"
}
copy() { # what, where
	if [ $remove = yes ]; then
		[ -f "$2" ] && cmp -s "$1" "$2" && rm -f "$2" && echo "removed $2"
		return 0
	fi
	install -D -m 0644 "$1" "$2"
	echo "$2"
}

s=$p/share
sys=$s/cont8nuum/system
link "$s/wayland-sessions/cont8nuum.desktop" /usr/local/share/wayland-sessions/cont8nuum.desktop
for u in "$p"/lib/systemd/user/*; do
	if [ -e "$u" ]; then link "$u" "/usr/local/lib/systemd/user/$(basename "$u")"; fi
done
for d in "$s"/dbus-1/services/*.service; do
	if [ -e "$d" ]; then link "$d" "/usr/local/share/dbus-1/services/$(basename "$d")"; fi
done
link "$s/fonts/selawik" /usr/local/share/fonts/selawik
link "$s/xdg-desktop-portal/cont8nuum-portals.conf" /usr/local/share/xdg-desktop-portal/cont8nuum-portals.conf
if [ -d /usr/share/sddm/themes ] && [ -d "$s/sddm/themes/cont8nuum" ]; then
	link "$s/sddm/themes/cont8nuum" /usr/share/sddm/themes/cont8nuum
fi

copy "$sys/pam.d/c8-lock" /etc/pam.d/c8-lock
copy "$sys/polkit/org.cont8nuum.login-picture.policy" \
	/usr/share/polkit-1/actions/org.cont8nuum.login-picture.policy
copy "$sys/profile.d/c8-askpass.sh" /etc/profile.d/c8-askpass.sh
copy "$sys/portals/c8.portal" /usr/share/xdg-desktop-portal/portals/c8.portal
copy "$p/etc/xdg/xdg-desktop-portal-wlr/cont8nuum" /etc/xdg/xdg-desktop-portal-wlr/cont8nuum
copy "$p/lib/udev/rules.d/70-cont8nuum-xreal.rules" /etc/udev/rules.d/70-cont8nuum-xreal.rules

askpass="Path askpass $p/bin/c8-askpass"
if [ $remove = yes ]; then
	if [ -f /etc/sudo.conf ] && grep -qxF "$askpass" /etc/sudo.conf; then
		sed -i -e "\\|^$askpass\$|d" -e '/^# The password window for sudo -A (cont8nuum)$/d' /etc/sudo.conf
		echo "removed sudo -A's askpass from /etc/sudo.conf"
	fi
else
	"$p/libexec/cont8nuum/sudo-askpass.sh" "$p/bin/c8-askpass"
fi

command -v fc-cache >/dev/null && fc-cache -f >/dev/null 2>&1 || true
command -v udevadm >/dev/null && udevadm control --reload 2>/dev/null || true
if [ $remove = yes ]; then
	echo "Done. Now: sudo rm -r $p"
else
	echo "Done. Log out and pick cont8nuum on the sign-in screen."
fi
