#!/bin/bash
# $Id: themer,v 1.11 2004/01/14 19:47:56 rexdieter Exp $
#
# kde-redhat Themer tool
#
VERSION='1.50'

usage="\
Usage:	themer --version
	themer --install themename priority
	themer --remove  themename 
"

if [ $# -eq 0 ]; then 
  echo "${usage}" 1>&2
  exit 0
fi

RETVAL=0
VERBOSE=0

QTDIR=$(unset QTDIR ||:; source /etc/profile.d/qt.sh 2>/dev/null; echo ${QTDIR:-/usr/lib/qt-3.3} )
QTVER=$(basename $QTDIR | cut -d- -f2)
QTLOC=$QTDIR/etc/settings
QTLINK=$QTLOC/qtrc

KDEDIR=$(kde-config --prefix 2> /dev/null || echo -n /usr )
KDELOC=$KDEDIR/share/config
KDELINK=$KDELOC/kdeglobals.defaults

GTKDIR=/usr
GTKLOC=$GTKDIR/share/themes
GTKLINK=$GTKLOC/Default


theme_install() {
	theme_install_gtk
	RETVAL=$?
	theme_install_qt
	theme_remove_crud_qt
	RETVAL2=$?
	theme_install_kde
	RETVAL3=$?
	[ $RETVAL -eq 0 -a $RETVAL2 -eq 0 -a $RETVAL3 -eq 0 ] || RETVAL=1
        return $RETVAL
}

theme_install_gtk() {
theme_remove_crud_gtk
# exit if target theme doesn't exist
[ ! -d $GTKLOC/${THEME} ] && return
[ $VERBOSE != 0 ] && echo "installing $GTKLOC/${THEME}"
# alternatives can't update non-symlink target
[ -d $GTKLINK -a ! -L $GTKLINK ] && \
	mv $GTKLINK $GTKLINK.rpmorig
/usr/sbin/alternatives --install \
        $GTKLINK gtk-theme \
        $GTKLOC/${THEME} ${PRIORITY} 
}

theme_install_qt() {
theme_remove_crud_qt
# exit if target theme doesn't exist
[ ! -f $QTLINK.${THEME} ] && return 
[ $VERBOSE != 0 ] && echo "installing $QTLINK.${THEME}"
# alternatives can't update non-symlink target
[ -f $QTLINK -a ! -L $QTLINK ] && \
        mv $QTLINK ${QTLINK}.rpmorig
[ -f $QTLOC/kstylerc -a ! -L $QTLOC/kstylerc ] && \
        mv $QTLOC/kstylerc $QTLOC/kstylerc.rpmorig
/usr/sbin/alternatives --install \
	$QTLINK qt${QTVER}-theme \
	$QTLINK.${THEME} ${PRIORITY} 
#--slave $QTLOC/kstylerc kstylerc \
#	$QTLOC/kstylerc.${THEME}
}

theme_install_kde() {
theme_remove_crud_kde
# exit if target theme doesn't exist
[ ! -f $KDELINK.${THEME} ] && return 
[ $VERBOSE != 0 ] && echo "installing $KDELINK.${THEME}"
# alternatives can't update non-symlink target
[ -f $KDELINK -a ! -L $KDELINK ] && \
	mv $KDELINK ${KDELINK}.rpmorig
[ -f $KDELOC/kwinrc.defaults -a ! -L $KDELOC/kwinrc.defaults ] && \
	mv $KDELOC/kwinrc.defaults $KDELOC/kwinrc.defaults.rpmorig
/usr/sbin/alternatives --install \
	$KDELINK kde-theme \
	$KDELINK.${THEME} ${PRIORITY} \
--slave	$KDELOC/kwinrc.defaults kwinrc.defaults \
	$KDELOC/kwinrc.defaults.${THEME}
}

theme_remove() {
	theme_remove_gtk
	RETVAL=$?
	theme_remove_qt
	RETVAL2=$?
	theme_remove_kde
	RETVAL3=$?
	[ $RETVAL -eq 0 -a $RETVAL2 -eq 0 -a $RETVAL3 -eq 0 ] || RETVAL=1
	return $RETVAL
}

theme_remove_gtk() {
#[ ! -d $GTKLOC/${THEME} ] && return 
[ $VERBOSE != 0 ] && set -x 
/usr/sbin/alternatives --remove gtk-theme $GTKLOC/${THEME} 2> /dev/null
set -
}

theme_remove_crud_gtk() {
if [ "$(/usr/sbin/alternatives --display gtkrc 2>/dev/null)" != "" ];then
  for alternative in $GTKLOC/* } ; do
    /usr/sbin/alternatives --remove gtkrc $alternative >& /dev/null || :
  done
fi
}

theme_remove_qt() {
#[ ! -f $QTLINK.${THEME} ] && return 
[ $VERBOSE != 0 ] && set -x 
/usr/sbin/alternatives --remove qt${QTVER}-theme $QTLINK.${THEME} 2> /dev/null 
set -
}

theme_remove_crud_qt() {
if [ "$(/usr/sbin/alternatives --display qtrc 2>/dev/null)" != "" ];then
  for alternative in /usr/lib/qt-3.1/etc/settings/qtrc.* } ; do
    /usr/sbin/alternatives --remove qtrc $alternative >& /dev/null || :
  done
fi

if [ "$(/usr/sbin/alternatives --display qt-theme 2>/dev/null)" != "" ];then
  for alternative in /usr/lib/qt-3.1/etc/settings/qtrc.* } ; do
    /usr/sbin/alternatives --remove qt-theme $alternative >& /dev/null || :
  done
fi

if [ "${QTVER}" != "3.2" ]; then
if [ "$(/usr/sbin/alternatives --display qt3.2-theme 2>/dev/null)" != "" ];then
  for alternative in /usr/lib/qt-3.2/etc/settings/qtrc.* } ; do
    /usr/sbin/alternatives --remove qt3.2-theme $alternative >& /dev/null || :
  done
fi
fi

}

theme_remove_kde() { 
#[ ! -f $KDELINK.${THEME} ] && return 
[ $VERBOSE != 0 ] && set -x 
/usr/sbin/alternatives --remove kde-theme $KDELINK.${THEME} 2> /dev/null
set -
}

theme_remove_crud_kde() {
if [ "$(/usr/sbin/alternatives --display kdeglobals.defaults 2>/dev/null)" != "" ];then
  for alternative in $KDELINK.* } ; do
    /usr/sbin/alternatives --remove kdeglobals.defaults $alternative >& /dev/null || :
  done
fi
}


while test $# -gt 0; do
  case $1 in
    --version)
	echo $VERSION 
	exit 0
	;;
    --install)
	THEME=$2
	PRIORITY=$3
	shift 2
	theme_install
	;;
    --remove)
	THEME=$2
	shift
	theme_remove
	;;
    --verbose|-v)
	VERBOSE=1
	;;
    --help|*)
	echo "${usage}" 1>&2
	exit 1
	;;
  esac
  shift
done

exit $?
