#!/bin/sh

prefix=/usr
SYSCONFDIR=/etc
TEXTDOMAIN=gnomemeeting
export TEXTDOMAIN

# NB.: Avoid !\n in the translatable strings!
ACCEPT=`gettext "YES"`

clean () {

	echo
	gettext "  WARNING!!!"; echo
	echo    "  ---------------------------------------------------------------"
	printf "`gettext "  This script will delete all user GnomeMeeting configuration,\n  including preferences that may be important! \n  The GConf daemon will be shut down, so make sure you have no\n  applications depending on it running.\n\n  Please make sure that GnomeMeeting is not running!\n\n  Do you really want to do this?\n\n  If so, say $ACCEPT.\n\n"`";

	read WARNINGANSWER
	
	echo
	
	if [ -n "$WARNINGANSWER" ] && [ "$WARNINGANSWER" = $ACCEPT ]; then
	    
	    gettext "Removing $HOME/.gconf/apps/gnomemeeting"; echo
	    rm -rf $HOME/.gconf/apps/gnomemeeting
	    
	    gettext "Shutting down GConf daemon"; echo
	    gconftool-2 --shutdown
	    
	    gettext "...Done."; echo
	    
	else
	    gettext "Leaving configuration alone."; echo
	    echo
	    gettext "Have a nice day."; echo
	    
	fi
	
	echo
}

shutdownwarning () {
    echo
    gettext "  WARNING!!!"; echo
    echo    "  ---------------------------------------------------------------"
    printf "`gettext "  The GConf daemon will be shut down, so make sure you have no\n  applications depending on it running.\n\n  Please make sure that GnomeMeeting is not running!\n\n  Do you really want to do this?\n\n  If so, say $ACCEPT.\n\n"`";
}

cleanschemas () {
    shutdownwarning;

    read WARNINGANSWER

    if [ -n "$WARNINGANSWER" ] && [ "$WARNINGANSWER" = $ACCEPT ]; then

	echo ""

	SOURCE=`gconftool-2 --get-default-source`
	    
	gettext "Shutting down GConf daemon"; echo
	gconftool-2 --shutdown

	gettext "Removing schemas"; echo
	SOURCE=`echo $SOURCE | sed "s/xml:://"`
	SCHEMAS=$SOURCE/schemas/apps/gnomemeeting
	SOURCE=$SOURCE/apps/gnomemeeting
	echo $SCHEMAS
	echo $SOURCE
	rm -rf $SOURCE
	rm -rf $SCHEMAS
    fi
}

installschemas () {
    shutdownwarning;

    read WARNINGANSWER

    if [ -n "$WARNINGANSWER" ] && [ "$WARNINGANSWER" = $ACCEPT ]; then

	echo

	SOURCE=`gconftool-2 --get-default-source`
	    
	gettext "Shutting down GConf daemon"; echo
	gconftool-2 --shutdown

	gettext "Installing schemas"; echo
	GCONF_CONFIG_SOURCE=$SOURCE  gconftool-2 \
	--makefile-install-rule $SYSCONFDIR/gconf/schemas/gnomemeeting.schemas
    fi
}

pkginstallschemas ()
{
    SOURCE=xml::/etc/gconf/gconf.xml.defaults

    GCONF_CONFIG_SOURCE=$SOURCE  gconftool-2 \
    --makefile-install-rule $SYSCONFDIR/gconf/schemas/gnomemeeting.schemas \
    2>/dev/null >/dev/null \
    || installfailure;
}

installfailure ()
{
    echo
    printf "`gettext "CRITICAL ERROR -- Schema installation failed.\nMost probably you have the GConf daemon running as root.\n\nRun \"gnomemeeting-config-tool --install-schemas\" as root.\n"`";

    exit 1
}

fixgconfperm ()
{
    killall gconfd-1 2>/dev/null >/dev/null
    chmod -R o+rX $SYSCONFDIR/gconf
}

case "$1" in
  *--clean)
        clean;
        ;;
  *--install-schemas)
        installschemas;
        ;;
  *--pkg-install-schemas)
        pkginstallschemas;
        ;;
  *--clean-schemas)
	cleanschemas;
        ;;
  *--fix-permissions)
        fixgconfperm;
        ;;
  *)
        gettext "Usage:  gnomemeeting-config-tool OPTION"; echo
	gettext "Fixes problems with the GnomeMeeting settings"; echo; echo
	gettext "  --clean                remove all user settings"; echo
	gettext "  --install-schemas      install schemas with settings defaults (run as root)"; echo
	gettext "  --clean-schemas        remove schemas with settings defaults (run as root)"; echo
	gettext "  --fix-permissions      fix permissions of GConf repository (run as root)"; echo
        exit 1
        ;;
esac
