#!/bin/sh

# Conky Launcher by JakeSFR'2018,2020
# GNU GPL v2 applies

export TEXTDOMAIN=conky_launcher
export OUTPUT_CHARSET=UTF-8

HS="$(echo -ne '\xc2\xa0')"	# Hardspace, for Xdialog

CONFIG_DIRS="/etc/conky ${XDG_CONFIG_HOME}/conky"

while true; do

MENU="$(find /etc/conky ${XDG_CONFIG_HOME}/conky -type f -iname "*.conf" 2>/dev/null | sort | ( while read -r ITEM; do
  NAME="${ITEM##*/}"
  CMD="${ITEM}"
  echo "${CMD// /${HS}} ${NAME// /${HS}}"	# replace spaces with hard spaces in CMD, for Xdialog
done))"

RESULT=$(Xdialog --stdout --no-tags \
		--title "$(gettext "Conky Launcher")" \
		--backtitle "$(gettext "Note:\n
- '(Re)Start' adds autostart to $HOME/Startup/\n
- 'Stop' removes it")" \
		--ok-label "$(gettext "(Re)Start")" \
		--cancel-label "$(gettext "Stop")" \
		--menu "$(gettext "Available configs:")" 16 48 16 ${MENU}
)

case $? in
  0)	killall -0 conky 2>/dev/null && killall conky
		rm -f "${HOME}/Startup/conky"
		echo -e "#!/bin/sh\nconky -q -d -c \"${RESULT//${HS}/ }\"" > "${HOME}/Startup/conky"
		chmod +x "${HOME}/Startup/conky"
		conky -q -d -c "${RESULT//${HS}/ }"
		;;
  1)	killall -0 conky 2>/dev/null && killall conky
		rm -f $HOME/Startup/conky
		;;
  *)	exit
		;;
esac

done
