#!/bin/sh

prefix=/usr/local

clean () {

    if ps hu -C galeon-bin | grep `id -un` >/dev/null 2>&1; then
    echo "Sorry, this script won't work while galeon is running"

    else

	    echo ""
	    echo "     WARNING!!!"
	    echo "     ---------------------------------------------------------------"
	    echo "     This script will delete all user galeon configuration including"
	    echo "     BOOKMARKS, HISTORY, and preferences that may be important!"
	    echo "     Gconf daemon will be shutted down, so make sure you have no apps"
	    echo "     depending on it running"
	    echo ""
	    echo "     Do you really want to do this?"
	    echo ""
	    echo "     If so, say \"YES\""
	    echo ""

	    read WARNINGANSWER

	    echo ""

	    if [ -n "$WARNINGANSWER" ] && [ "$WARNINGANSWER" = "YES" ]; then
		    echo "Removing old configuration..."

		    echo "Removing $HOME/.galeon"
		    rm -rf $HOME/.galeon

		    echo "Removing $HOME/.gconf/apps/galeon"
		    rm -rf $HOME/.gconf/apps/galeon

		    echo "Shutting down gconf daemon"
		    gconftool-2 --shutdown

		    echo "Removing $HOME/.gnome/galeon"
		    rm -rf $HOME/.gnome/galeon

		    echo "Removing $HOME/.gnome/accels/galeon"
		    rm -rf $HOME/.gnome/accels/galeon

		    echo "...Done."

	    else
		    echo "Leaving configuration alone."
		    echo ""
		    echo "Have a nice day."

	    fi

	    echo ""

	fi
}

shutdownwarning () {
	    echo ""
	    echo "     WARNING!!!"
	    echo "     ---------------------------------------------------------------"
	    echo "     Gconf daemon will be shutted down, so make sure you have no apps"
	    echo "     depending on it running"
	    echo ""
	    echo "     Do you really want to do this?"
	    echo ""
	    echo "     If so, say \"YES\""
	    echo ""
}

cleanschemas () {
shutdownwarning;

    read WARNINGANSWER

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

	echo ""

	SOURCE=`gconftool-2 --get-default-source`
	    
	echo "Shutting down gconf daemon"
	gconftool-2 --shutdown

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

installschemas () {

    shutdownwarning;

    read WARNINGANSWER

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

	echo ""

	SOURCE=`gconftool-2 --get-default-source`
	    
	echo "Shutting down gconf daemon"
	gconftool-2 --shutdown

	echo "Installing schemas"
	GCONF_CONFIG_SOURCE=$SOURCE  gconftool-2 \
	--makefile-install-rule /etc/gconf/schemas/galeon.schemas
    fi
}

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

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

installfailure ()
{
    echo ""
    echo "CRITICAL ERROR -- Schema installation is failed."
    echo "Most probably you have gconf daemon running as root."
    echo ""
    echo "Run \"galeon-config-tool --install-schemas\" as root"

    exit 1
}

fixgconfperm ()
{
    gconftool-2 --shutdown
    chmod -R o+rX /etc/gconf
}

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