#!/bin/ash

# LxQt-Panel Theme Switcher
# Copyright (C) JakeSFR 2018
#
# License: GNU GPL Version 3 or later

# std localisation stanza
export TEXTDOMAIN=fatdog
. gettext.sh

APPTITLE="$(gettext 'LXQt-Panel Theme Switcher')"
CONFIGFILE="${XDG_CONFIG_HOME}/lxqt/lxqt.conf"
THEMESDIR="/usr/share/lxqt/themes"
THEMES="$(ls "${THEMESDIR}")"

NOTE="$(gettext "If the new theme has not been applied,
you may need to restart X for changes to take effect.")"

HS="$(echo -ne '\xc2\xa0')"	# hardspace replacement for Xdialog, in case some theme's dirname contains spaces

if [ "$THEMES" ]; then

	while true; do

		ARGS=''
		CURRENT="$(sed -n 's/^theme=\(.*\)/\1/p' ${CONFIGFILE})"

		for THEME in ${THEMES// /${HS}}; do
			[ -d "${THEMESDIR}/${THEME//${HS}/ }" ] || continue
			[ -e "${THEMESDIR}/${THEME//${HS}/ }/lxqt-panel.qss" ] || continue
			[ "${THEME//${HS}/ }" = "${CURRENT}" ] && ACTIVE='on' || ACTIVE='off'
			ARGS="${ARGS} ${THEME} ${THEME} ${ACTIVE}"
		done

		RESULT=$(Xdialog \
			--stdout \
			--no-tags \
			--title "${APPTITLE}" \
			--backtitle "$(gettext 'Change LXQt-Panel theme')" \
			--radiolist "$(gettext 'Available themes:')" 16 48 5 ${ARGS})

		[ $? -ne 0 ] && exit

		# Set new theme
		sed -i 's/^theme=.*/theme='"${RESULT//${HS}/ }"'/' "${CONFIGFILE}"
		
		Xdialog --title "${APPTITLE}" \
				--backtitle "$(gettext "Theme changed to:") ${RESULT}" \
				--infobox "${NOTE}" 0 0 10000

	done

else

	Xdialog --title "${APPTITLE}" --infobox "$(gettext 'No themes found.')" 0 0 5000

fi
