#!/bin/sh
#(c) copyright Barry Kauler aug 2009. Licence LGPL.

PREVTHEME=""
if [ -e /root/.icons/default ];then
 PREVTHEME="`readlink /root/.icons/default | rev | cut -f 1 -d '/' | rev`"
fi

CURLIST=""
FIRSTITEM=""
LISTHEIGHT=30
mkdir -p /tmp/xcur2png
for ONEITEM in `ls -1 /root/.icons | grep -v 'ROX'`
do

 #precaution that 'cursors' subdir exists...
 [ ! -d /root/.icons/${ONEITEM}/cursors ] && continue
 
 if [ ! -f /usr/share/icons/${ONEITEM}.png ];then
  cd /root/.icons/${ONEITEM}/cursors
  xcur2png -d /tmp/xcur2png left_ptr
  mv -f /tmp/xcur2png/left_ptr_000.png /usr/share/icons/${ONEITEM}.png
  rm -f /tmp/xcur2png/left_ptr_*.png
 fi
 
 if [ "$ONEITEM" = "$PREVTHEME" ];then
  FIRSTITEM="<item icon=\"${ONEITEM}\">${ONEITEM} CURRENT THEME</item>"
 else
  CURLIST="${CURLIST}<item icon=\"${ONEITEM}\">${ONEITEM}</item>"
 fi
 LISTHEIGHT=`expr $LISTHEIGHT + 25`
done

[ $LISTHEIGHT -gt 550 ] && LISTHEIGHT=550
CURITEMS="  <tree>
  <label>Cursor theme</label>
    ${FIRSTITEM}
    <item icon=\"default_left_ptr\">ORIGINAL THEME</item>
    ${CURLIST}
    <variable>CHOOSECUR</variable>
    <height>${LISTHEIGHT}</height>
  </tree>"
  
if [ "$CURLIST" = "" ];then
 CURITEMS="<text use-markup=\"true\">
    <label>\"<b>You first need to install some cursor themes! For now, quit this program. Run the Puppy Package Manager and look in the 'Desktop' category</b>\"</label></text>"
fi

export MAIN_DIALOG="
<window title=\"Pcur: select a cursor theme\">
  <vbox>
    <text use-markup=\"true\">
      <label>\"<b>You must restart X to use the new theme</b>\"</label>
    </text>
    <text><label>Choose one you like, click 'OK', close down all applications, then choose 'Restart X server' in the 'Shutdown' menu...</label></text>
    
    
    ${CURITEMS}
    
    <hbox>
     <button ok></button>
     <button cancel></button>
    </hbox>
  </vbox>
</window>
"
RETVARS="`gtkdialog3 --program=MAIN_DIALOG`"

[ "$CURLIST" = "" ] && exit
echo "$RETVARS"
eval "${RETVARS}"

[ "$EXIT" != "OK" ] && exit

[ "`echo "$CHOOSECUR" | grep 'CURRENT THEME'`" != "" ] && exit #already current.

if [ "$CHOOSECUR" = "ORIGINAL THEME" ];then
 rm -f /root/.icons/default
 exit
fi

ln -snf $CHOOSECUR /root/.icons/default
