#!/bin/sh

# this script is designed to detect if 'pTheme' is installed and update user
# configurations for jwm and gtk as necessary on a puppy linux upgrade.
# it is called from /etc/rc.update before X has started from an upgrade.
 
[ -d "$HOME/.ptheme" ] && exit # should be ok
[ -d "$HOME/.jwm/tmp" ] && exit

grep -q 'jwm' /etc/windowmanager || exit

# clean up old icon_switcher or send to null

rm -f /initrd/pup_{rw,ro1}/usr/sbin/icon_switcher 2>/dev/null

# if we have basic PoP (plain old puppy) theme we use it, else use 'stardust bright'
# but we leave the user's chosen wall paper.. they will moan enough about icons!

ls /usr/share/ptheme/globals|grep -q 'pop' && THEME='pop' || THEME='Bright Mouse'

if [ -f /usr/share/ptheme/globals/"${THEME}" ];then
	. /usr/share/ptheme/globals/"${THEME}"
else
	exit
fi

CUR_BACDROP_LINE="`grep 'backdrop' $HOME/Choices/ROX-Filer/PuppyPin`"
# ah.. but if using a wall on the main sfs it will be gone.. maybe
CUR_WALL="`echo "$CUR_BACDROP_LINE"|tr '/' '\n'|tr '<' '\n'|egrep -i 'png$|svg$|jpg$|jpeg$|gif$|tiff$|background$'`"
# so we drop them off at default wall for install
if ls /usr/share/backgrounds|grep -q "$CUR_WALL";then
	BACKDROP_LINE="$CUR_BACDROP_LINE"
else
	DEF="`ls /usr/share/backgrounds|egrep '^default.png|^default.svg|^default.jpg'|head -n1`"
	BACKDROP_LINE="`echo $CUR_BACDROP_LINE|sed "s%$CUR_WALL%$DEF%"`"
fi

##### JWM
cp -af /usr/share/jwm/themes/"${PTHEME_JWM_COLOR}-jwmrc" $HOME/.jwm/jwmrc-theme
cp -af /usr/share/jwm/themes/"${PTHEME_JWM_COLOR}-colors" $HOME/.jwm/jwm_colors
cp -f /usr/share/jwm/tray_templates/"$PTHEME_JWM_TRAY"/jwmrc-tray* $HOME/.jwm/
case $PTHEME_JWM_SIZE in
	small) rm root/.jwm/menuheights;;
	normal) echo "MENHEIGHT=24" > $HOME/.jwm/menuheights;;
	large) echo "MENHEIGHT=32" > $HOME/.jwm/menuheights;;
	huge) echo "MENHEIGHT=40" > $HOME/.jwm/menuheights;;
esac
mkdir $HOME/.jwm/window_buttons
Dir=/usr/share/jwm/themes_window_buttons/${PTHEME_JWM_BUTTONS}
for icon in $Dir/*; do
    ifile=$(basename $icon)
    ext=${ifile##*.}
    newicon=`echo $ifile|sed "s%${ext}$%png%"`
    #JWM does a crappy svg convert, so we help out if rsvg-convert is installed
    if [ "`which rsvg-convert`" ]; then
        rsvg-convert -w 48 -h 48 -o $HOME/.jwm/window_buttons/${newicon} ${icon}
    else
        ln -sf ${icon} $HOME/.jwm/window_buttons/${newicon}
    fi
done

##### GTK
cat > /root/.gtkrc-2.0 << _EOF
# -- THEME AUTO-WRITTEN BY gtk-theme-switch2 DO NOT EDIT
include "/usr/share/themes/${PTHEME_GTK}/gtk-2.0/gtkrc"

style "user-font"
{
  font_name=""
}
widget_class "*" style "user-font"

include "/root/.gtkrc-2.0.mine"

# -- THEME AUTO-WRITTEN BY gtk-theme-switch2 DO NOT EDIT
gtk-theme-name = "${PTHEME_GTK}"
_EOF

##### ROX
BACKDROP="$BACDROP_LINE"
echo -e '<?xml version="1.0"?>\n<pinboard>' > /tmp/newpin
echo "$BACKDROP" >> /tmp/newpin
while read line;do
	echo "$line" | grep -q 'pup_event' && echo "$line" >> /tmp/newpin || continue
done < $HOME/Choices/ROX-Filer/PuppyPin
cat /usr/share/ptheme/rox_pinboard/"${PTHEME_ROX_PIN}" >> /tmp/newpin
echo '</pinboard>' >> /tmp/newpin
cp -af /tmp/newpin $HOME/Choices/ROX-Filer/PuppyPin

##### CURSOR
if [ -d "$HOME/.icons/" ];then
	[ ! "`grep 'ORIGINAL THEME' <<< "$PTHEME_MOUSE"`" ] && ln -snf $HOME/.icons/$PTHEME_MOUSE $HOME/.icons/default
fi

##### GTKDIALOG
[ -d "$HOME/.config/ptheme/" ] || mkdir -p $HOME/.config/ptheme/
cp -f "/usr/share/ptheme/gtkdialog/$PTHEME_GTKDIALOG" $HOME/.config/ptheme/gtkdialog_active

##### ICONS
icon_switcher_cli -a "${PTHEME_ICONS}" 2>/dev/null # send to null coz X isn't up

# end
