#!/bin/sh
#(c) copyright Barry Kauler aug 2009. Licence LGPL.
#120202 rodin.s: i18n
#121020 fix msg where locate 'cursor_themes' pet pkg.

export TEXTDOMAIN=pcur
export OUTPUT_CHARSET=UTF-8
CURLIST=""
FIRSTITEM=""
LISTHEIGHT=50
PREVTHEME=""
[ -e $HOME/.icons/default ] && PREVTHEME="`readlink $HOME/.icons/default | rev | cut -f 1 -d '/' | rev`"
mkdir -p /tmp/xcur2png

func_switch (){
	ln -snf "$1" $HOME/.icons/default
	[ "$(</etc/windowmanager)" = "jwm" ] && jwm -restart
}

func_download (){
	REPO=http://distro.ibiblio.org/puppylinux/pet_packages-noarch
	PKG=`grep -iE "cursor_themes" ~/.packages/Packages-puppy-noarch-official|awk -F'|' '{print $8}'`
	download_file ${REPO}/${PKG}
	[ $? -ne 0 ] && /usr/lib/gtkdialog/box_ok "pCur - $(gettext 'Cursor theme setter')" error "$(gettext 'Failed to download package')" && exit 1
	petget +${PKG}
	[ $? -ne 0 ] && /usr/lib/gtkdialog/box_ok "pCur - $(gettext 'Cursor theme setter')" error "$(gettext 'Failed to install package')" && exit 1
	rm $PKG
	/usr/lib/gtkdialog/box_ok "pCur - $(gettext 'Cursor theme setter')" complete "$(gettext 'New themes are installed. <b>Please restart pCur</b> - Cursor theme setter.')" && exit 0
}
export -f func_switch func_download

for ONEITEM in `ls -1 $HOME/.icons | grep -v 'ROX'`; do
	#precaution that 'cursors' subdir exists...
	[ ! -d $HOME/.icons/${ONEITEM}/cursors ] && continue
	if [ ! -f /usr/share/icons/${ONEITEM}.png ]; then
		cd $HOME/.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>"
		FIRSTITEM="<item icon=\"${ONEITEM}\">${ONEITEM}</item>"
	else
		CURLIST="${CURLIST}<item icon=\"${ONEITEM}\">${ONEITEM}</item>"
	fi
	LISTHEIGHT=`expr $LISTHEIGHT + 25`
done

[ $LISTHEIGHT -gt 550 ] && LISTHEIGHT=550
CURITEMS='
<tree>
  <label>'$(gettext 'Cursor theme')'</label>
  '${FIRSTITEM}'
  <item icon="default_left_ptr">ORIGINAL THEME</item>
  '${CURLIST}'
  <variable>CHOOSECUR</variable>
  <height>'${LISTHEIGHT}'</height>
  <action>func_switch "$CHOOSECUR"</action>
</tree>'
  
if [ "$CURLIST" = "" ]; then
	TXT="$(gettext "<b>No themes found.</b> Click the install button to get a nice collection of cursor themes.")"
	CURITEMS='
	<frame>
	  <hbox homogeneous="true" space-fill="true" space-expand="true">
	    <hbox margin="20" space-fill="false" space-expand="false">
	      <button image-position="2">
	        '"`/usr/lib/gtkdialog/xml_button-icon install huge`"'
	        <label>'$(gettext 'Download and Install')'</label>
	        <action>func_download &</action>
	        <action type="exit">exit</action>
	      </button>
	    </hbox>
	  </hbox>
	</frame>'
elif [ "$(</etc/windowmanager)" != "jwm" ]; then
	TXT="<b>$(gettext 'You must restart X to use the new theme.')</b>"
else
	TXT="$(gettext '<b>Choose your prefered mousepointer theme.</b> Themes are installed to') $HOME/.icons."
fi

S='
<window title="Pcur - '$(gettext 'Cursor theme setter')'">
<vbox space-fill="true" space-expand="true">
  '"`/usr/lib/gtkdialog/xml_info fixed mouse_cursor.svg 60 "$(gettext "$TXT")"`"'
  <vbox space-fill="true" space-expand="true">
    '${CURITEMS}'
   <hbox space-expand="false" space-fill="false">
     <button space-expand="false" space-fill="false">
       '"`/usr/lib/gtkdialog/xml_button-icon internet`"'
       <label>'$(gettext 'More themes')'</label>
       <action>defaulthtmlviewer http://www.murga-linux.com/puppy/viewtopic.php?t=37852 &</action>
     </button>'
     [ "`which ptheme`" ] && S=$S'<button space-expand="false" space-fill="false">
       '"`/usr/lib/gtkdialog/xml_button-icon puppy_theme.svg`"'
       <label>" '$(gettext 'Global theming')' "</label>
       <action>/usr/sbin/ptheme &</action>
     </button>'
     S=$S'<text space-expand="true" space-fill="true"><label>""</label></text>
      <button space-expand="false" space-fill="false">
        <label>'$(gettext "Apply")'</label>
        '"`/usr/lib/gtkdialog/xml_button-icon apply`"'
        <action>func_switch "$CHOOSECUR"</action>
      </button>
      <button space-expand="false" space-fill="false">
        <label>'$(gettext "Ok")'</label>
        '"`/usr/lib/gtkdialog/xml_button-icon ok`"'
        <action>func_switch "$CHOOSECUR"</action>
        <action>exit:quit</action>
      </button>
      <button space-expand="false" space-fill="false">
        <label>'$(gettext "Quit")'</label>
        '"`/usr/lib/gtkdialog/xml_button-icon quit`"'
        <action>exit:quit</action>
      </button>
      '"`/usr/lib/gtkdialog/xml_scalegrip`"'
    </hbox>
  </vbox>
</vbox>
</window>'
export Pcur="$S"
. /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
RETVARS="`gtkdialog -p Pcur`"

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

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

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