#!/bin/bash
#
# choose_cursor
# (c) Buchan Milne <bgmilne@linux-mandrake.com> 2003 
# License: GPLv2
# 
# choose_cursor is a simple script to allow users to choose from the mouse
# cursor themes installed on their system (/usr/share/icons/*/cursors) or in
# their home diretory ($HOME/.icons/*/cursor).
#
# Changelog
# v0.0.1 - 20030329
# First version used gmessage (gchooser isn't consistent :-()
# We now use Xdialog, because we have too many themes to fit a gmessage
# on a reasonable size screen

ICONS=/usr/share/icons
USERICONS=~/.icons
INDEXFILE=default/index.theme

c=(core `ls $ICONS/*/cursors $USERICONS/*/cursors -d 2>/dev/null|cut -f5 -d'/'|grep -v default`)

BUTTONS=`for i in $(seq 0 "$[${#c[*]}-2]");do echo -n "${c[i]}:$[$i+1],";done``echo -n "${c[$[${#c[*]}-1]]}:$[${#c[*]}]"`
LIST=`for i in $(seq 0 "$[${#c[*]}-2]");do echo -n "${c[i]} ";done``echo -n "${c[$[${#c[*]}-1]]}"`
MESSAGE="Please choose a cursor (only new applications will use it)"

#gtkdialogs, user either gmessage or gchooser
#gmessage -center -buttons $BUTTONS $MESSAGE
#gchooser "$MESSAGE" $LIST
#THEME=${c[$[$?-1]]}

#Xdialog
THEME=`Xdialog -center --title "Choose a cursor" --combobox "$MESSAGE" 0 0 $LIST 2>&1`

if [ $? -eq 0 ]; then

	# Root sets the global cursor
	if [ `id -u` -eq 0 ];then
		INDEX=$ICONS/$INDEXFILE
	else
		INDEX=$USERICONS/$INDEXFILE
		if [ ! -d $USERICONS/default ]; then
		    mkdir -p $USERICONS/default
		    touch $INDEX
		fi
	fi

	grep -q "\[Icon Theme\]" $INDEX 2>/dev/null
	if [ $? -eq 0 ]
	then
		cp -f $INDEX $INDEX.backup 2>/dev/null
		cat $INDEX.backup |sed -e "s/^Inherits=.*$/Inherits=$THEME/g" >$INDEX
	else
		echo "[Icon Theme]" > $INDEX
		echo "Inherits=$THEME" >> $INDEX
	fi
fi
