#!/bin/bash
#(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 (){
	[ "$1" ] || return
	if [[ "$(grep -iE "ORIGINAL THEME" <<< "$1")" ]] ; then
		rm -f $HOME/.icons/default
		if [ `id -u` -eq 0 ] ; then
			rm -f /home/spot/.icons/default
		fi
	elif [[ -d $HOME/.icons/"$1" ]]; then
		ln -snf "$1" $HOME/.icons/default
		if [ `id -u` -eq 0 ] ; then
			mkdir -p /home/spot/.icons/
			ln -snf /root/.icons/"$1" /home/spot/.icons/default
			chown -R spot:spot /home/spot/.icons
		fi
	fi
	if [ "$UPDATE_WM" != "false" ] ; then
		if pidof jwm &>/dev/null ; then
			jwm -restart
		elif pidof openbox &>/dev/null ; then
			openbox --restart
		fi
	fi
}

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}
	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


#parameters
while [ $# != 0 ]; do
	I=1
	while [ $I -le `echo $# | wc -c` ]; do 
		case $1 in
			-t) export THEME="$2"
				[ ! "$THEME" ] && export THEME="X"
				shift;;
			-u) export UPDATE_WM=false;;
			-h|--help)
echo 'Usage: pcur [OPTIONS]

Options
  -t THEME   Set theme without open gui 
  -u         Do NOT update JWM after theme switch
  -h         Show this help message'; exit;;
		esac
		shift
		I=$[$I+1]
	done
done
if [ "$THEME" ]; then
	func_switch "$THEME" &
	exit
fi
#---

for ONEITEM in `ls -1 $HOME/.icons | grep -Ev 'ROX|^default$'`; 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}</item>"
	else
		CURLIST="${CURLIST}<item icon=\"${ONEITEM}\">${ONEITEM}</item>"
	fi
	LISTHEIGHT=$(( $LISTHEIGHT + 25 ))
done

[ $LISTHEIGHT -gt 400 ] && LISTHEIGHT=400

CURITEMS='
<tree>
  <label>'$(gettext 'Cursor theme')'</label>
  '${FIRSTITEM}'
  <item icon="default_left_ptr">'$(gettext 'ORIGINAL THEME')'</item>
  '${CURLIST}'
  <variable>CHOOSECUR</variable>
  <height>'${LISTHEIGHT}'</height>
  <action>func_switch "$CHOOSECUR"</action>
</tree>'
  
if [ "$CURLIST" = "" -a "$FIRSTITEM" = "" ]; 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 20 "$(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`"

### END ###
