#!/bin/sh
# (c) Robert Shingledecker 2009-2010
# Called to setup jwm tce menu

. /etc/init.d/tc-functions
. /usr/local/bin/jwm_menu_common

USER=`cat /etc/sysconfig/tcuser`
PORT="$(cat /usr/share/doc/tc/release.txt | cut -f1 -d:)"
CFGDIR=/usr/local/share/jwm/tce

[ -f /home/"$USER"/.jwmrc ] || cp "$CFGDIR"/.jwmrc /home/"$USER"/
[ -f /home/"$USER"/.jwmrc-keys ] || cp "$CFGDIR"/.jwmrc-keys /home/"$USER"/
[ -f /home/"$USER"/.jwmrc-tray ] || cp "$CFGDIR"/.jwmrc-tray /home/"$USER"/

if [ "$PORT" == "dCore-wheezy" ] || [ "$PORT" == "dCore-jessie" ] || [ "$PORT" == "dCore-trusty" ] && [ ! -f /home/"$USER"/.jwmrc-theme ]; then
	cp "$CFGDIR"/.jwmrc-theme_legacy /home/"$USER"/.jwmrc-theme
	jwm -restart
fi

if [ ! -f /home/"$USER"/.jwmrc-theme ]; then
	cp "$CFGDIR"/.jwmrc-theme /home/"$USER"/
	jwm -restart
fi

TCEMENU="/usr/local/tce.jwm"
sudo cp /usr/local/share/jwm/tce/menu_template "$TCEMENU"

SYSMENU="/tmp/.jwm-sysmenu"
echo "<JWM>" > "$SYSMENU"
echo "<Menu label=\"System Tools\">" >> "$SYSMENU"
for D in `ls /usr/local/share/applications/tinycore-*`; do
  writeMenuItem "$D" >> "$SYSMENU"
done
echo "</Menu>" >> "$SYSMENU"
echo "</JWM>" >> "$SYSMENU"

jwm_ondemand

sudo chown "$USER":staff "$TCEMENU" /home/"$USER"/.jwm*
sudo chmod g+w "$TCEMENU" /home/"$USER"/.jwm*
