#!/bin/sh
set -u

template=/usr/share/echo-themes/lxqt-skel
[ -d "$template/.config/lxqt" ] || exit 0

install -d -m 755 /etc/skel/.config
cp -a "$template/.config/." /etc/skel/.config/

getent passwd | while IFS=: read -r user _ uid _ _ home _; do
	[ "$uid" -ge 1000 ] || continue
	[ -d "$home" ] || continue
	group=$(id -gn "$user") || continue

	lxqt_dir="$home/.config/lxqt"
	if [ ! -d "$lxqt_dir" ]; then
		install -d -m 755 -o "$user" -g "$group" "$lxqt_dir"
	fi
	for src in "$template/.config/lxqt"/*; do
		dst="$lxqt_dir/${src##*/}"
		[ -e "$dst" ] || install -m 644 -o "$user" -g "$group" "$src" "$dst"
	done

	lxqt_conf="$lxqt_dir/lxqt.conf"
	if [ -f "$lxqt_conf" ]; then
		lxqt_changed=false
		if [ -d /usr/share/lxqt/themes/Echo ] && grep -qx 'theme=Arch-Colors' "$lxqt_conf"; then
			sed --follow-symlinks -i 's/^theme=Arch-Colors$/theme=Echo/' "$lxqt_conf"
			lxqt_changed=true
		fi
		old_palette=true
		for old_color in \
			'base_color=#052434' \
			'highlight_color=#1899da' \
			'highlighted_text_color=#0a0a2b' \
			'link_color=#1899da' \
			'link_visited_color=#dd5cee' \
			'text_color=#b8b8b8' \
			'tooltip_base_color=#041b27' \
			'tooltip_text_color=#b8b8b8' \
			'window_color=#041b27' \
			'window_text_color=#e1e6e6'; do
			if ! grep -qx "$old_color" "$lxqt_conf"; then
				old_palette=false
				break
			fi
		done
		if [ "$old_palette" = true ]; then
			sed --follow-symlinks -i \
				-e 's/^base_color=#052434$/base_color=#232629/' \
				-e 's/^highlight_color=#1899da$/highlight_color=#2ecc71/' \
				-e 's/^highlighted_text_color=#0a0a2b$/highlighted_text_color=#fcfcfc/' \
				-e 's/^link_color=#1899da$/link_color=#2ecc71/' \
				-e 's/^link_visited_color=#dd5cee$/link_visited_color=#27ae60/' \
				-e 's/^text_color=#b8b8b8$/text_color=#eff0f1/' \
				-e 's/^tooltip_base_color=#041b27$/tooltip_base_color=#31363b/' \
				-e 's/^tooltip_text_color=#b8b8b8$/tooltip_text_color=#eff0f1/' \
				-e 's/^window_color=#041b27$/window_color=#31363b/' \
				-e 's/^window_text_color=#e1e6e6$/window_text_color=#eff0f1/' \
				"$lxqt_conf"
			lxqt_changed=true
		fi
		if [ "$lxqt_changed" = true ]; then
			chown "$user:$group" "$lxqt_conf"
		fi
	fi

	panel="$lxqt_dir/panel.conf"
	if [ -f "$panel" ] && [ -e /usr/share/icons/Flat-Remix-Green-Dark/places/scalable/distributor-logo.svg ] && \
		grep -Eq '^icon=/usr/share/icons/(parrot-logo\.(svg|png)|parrot-menu\.png)$' "$panel"; then
		sed -E --follow-symlinks -i \
			-e 's#^icon=/usr/share/icons/(parrot-logo\.(svg|png)|parrot-menu\.png)$#icon=/usr/share/icons/Flat-Remix-Green-Dark/places/scalable/distributor-logo.svg#' \
			"$panel"
		chown "$user:$group" "$panel"
	fi

	pcmanfm="$home/.config/pcmanfm-qt/lxqt/settings.conf"
	if [ ! -e "$pcmanfm" ]; then
		src="$template/.config/pcmanfm-qt/lxqt/settings.conf"
		if [ -f "$src" ]; then
			install -d -m 755 -o "$user" -g "$group" "$(dirname "$pcmanfm")"
			install -m 644 -o "$user" -g "$group" "$src" "$pcmanfm"
		fi
	fi

	xfwm4="$home/.config/xfce4/xfconf/xfce-perchannel-xml/xfwm4.xml"
	skel_xfwm4=/usr/lib/parrot-skel/etc/skel/.config/xfce4/xfconf/xfce-perchannel-xml/xfwm4.xml
	if [ ! -e "$xfwm4" ] && [ -f "$skel_xfwm4" ]; then
		install -d -m 755 -o "$user" -g "$group" "$(dirname "$xfwm4")"
		install -m 644 -o "$user" -g "$group" "$skel_xfwm4" "$xfwm4"
	fi
	if [ -f "$xfwm4" ] && [ -d /usr/share/themes/Echo/xfwm4 ] && \
		grep -q 'name="theme" type="string" value="ARK-Dark"' "$xfwm4"; then
		sed --follow-symlinks -i \
			-e 's#\(name="theme" type="string" value="\)ARK-Dark\("\)#\1Echo\2#' \
			"$xfwm4"
		chown "$user:$group" "$xfwm4"
	fi
done
