#!/usr/bin/bash

get_config() {
	CHANGELOG_CONFIG_FILE="${changelog_CONFIG_FILE:-/etc/ublue-os/changelog.json}"
	QUERY="$1"
	FALLBACK="$2"
	shift
	shift
	OUTPUT="$(jq -r "$QUERY" "$CHANGELOG_CONFIG_FILE" 2>/dev/null || echo "$FALLBACK")"
	if [ "$OUTPUT" == "null" ] ; then
		echo "$FALLBACK"
		return
	fi
	echo "$OUTPUT"
}

DEFAULT_THEME="$(get_config '."default-theme"' "slate")"
TARGET_URL="$(get_config '."target-url"' "invalid")"
JQ_EXPRESSION="$(get_config '."jq-expression"' ".body")"
THEMES_DIRECTORY="$(get_config '."themes-directory"' "/usr/share/ublue-os/motd/themes")"

THEME=$(dconf read /org/gnome/desktop/interface/accent-color 2>/dev/null || echo -e "$DEFAULT_THEME")
# Dconf will fail if the accent-color has not been changed yet
if [ "$THEME" == "" ] ; then
	# Gsettings will not update if the system's schemas have not been configured properly
	THEME=$(gsettings get org.gnome.desktop.interface accent-color 2>/dev/null || echo -e "$DEFAULT_THEME")
fi
THEME=${THEME//\'/}
THEME=${CHANGELOG_FORCE_THEME:-$THEME}

curl -s "$TARGET_URL" | \
	jq -r "$JQ_EXPRESSION" | \
	glow -s "${THEMES_DIRECTORY}/${THEME}.json" -w 78 - $@
