#!/bin/sh
#
# Copyright (C) 2007 Nokia Corporation. All rights reserved. 
#
# Licensed under the GNU Lesser General Public License, version 2.1
# See COPYING for details
#
# This is basically a port of Debian's gconf-schemas tool to shell script.
# It always behaves as if --no-signal was given.

args=$*

usage() {
    cat <<EOF
usage: $0 --[un]register[-all] schemas.file1 [schemas.file2 [...]]
EOF
}

if [ -z "$args" ]; then
    usage
    exit 1
fi

case "$1" in
    (--register|--register-all|--unregister)
        ;;
    (*)
        usage
        exit 1
        ;;
esac

# check that at least one schemas file is given
if [ $# -lt 2 ]; then
    usage
    exit 1
fi

if [ `id -u` -ne 0 ]; then
    echo "You must be root to launch this program."
    exit 1
fi

action=$1

# take out the action from the arguments
shift

# Go through the given schema files and construct a path with the
# schema location.
# TODO: isabs($SCHEMA)
schema_location="/usr/share/gconf/schemas"
schema_files=$*

for SCHEMA in $schema_files; do
    case "$SCHEMA" in
        (--no-signal)
            # ignore it, to be compatible with Debian's implementation
            continue
            ;;
        (*.schemas)
            # OK - this is the canonical extension used in Debian
            ;;
        (*.xml)
            echo "*** $SCHEMA should end with .schemas (see NB#277442)." >&2
            echo "*** Accepting it anyway, to avoid regressions." >&2
            ;;
        (*)
            # not going to work - fail, loudly
            echo "*** Since gconf 2.16.0-2osso21, GConf schemas must end with either" >&2
            echo "*** .schemas (preferred) or .xml. '$SCHEMA' isn't going to work." >&2
            echo "***" >&2
            echo "*** Please report this as a bug in the package containing that schema. See" >&2
            echo "*** NB#277442 for more details." >&2
            exit 2
    esac

    if [ -e $schema_location/$SCHEMA ]; then
	schemas="$schemas $schema_location/$SCHEMA"
    else
	echo "Warning: $schema_location/$SCHEMA could not be found."
    fi
done

if test "${DPKG_RUNNING_VERSION+set}" = set && test "$action" != --register-all; then
    # This is what happens when we are called in an obsolete postinst/prerm
    # script. Do nothing, it will be done in the trigger
    echo "Not [un]registering schema, it will be done in the gconf2 trigger"
    exit 0
fi

# create a temp directory and try to ensure that the directory does
# not already exist
tmp_home="/tmp/gconf2-regunreg-$$"
until [ ! -d $tmp_home ]; do
    tmp_home="$tmp_home"_x
done

mkdir -p $tmp_home

if [ $action = "--register-all" ]; then
    # The standard extension used in Debian is .schemas, but for backwards
    # compat with packages like libmusicsuitedolbysettings, we also need to
    # look for .xml. (We don't just look at all files regardless of extension,
    # because we want to avoid cruft like .dpkg-old.)
    #
    # dh_gconf does assume that schemas have the correct extension, so
    # using .schemas is preferred.
    schemas=`find /usr/share/gconf/schemas -maxdepth 1 '(' -name '*.schemas' -or -name '*.xml' ')'`
fi

if [ $action = "--register" ] || [ $action = "--register-all" ]; then
    HOME=$tmp_home GCONF_CONFIG_SOURCE="xml:readwrite:/var/lib/gconf/defaults"\
	gconftool-2 --makefile-install-rule $schemas
#    kill -s HUP `pidof gconfd-2` >/dev/null 2>&1 || true
fi

if [ $action = "--unregister" ]; then
    HOME=$tmp_home GCONF_CONFIG_SOURCE="xml:readwrite:/var/lib/gconf/defaults"\
	gconftool-2 --makefile-uninstall-rule $schemas
fi

# Debian's version of gconf-schemas would signal the running gconfd-2 at
# this point, but on Maemo we never actually want to do that.

# remove the temp directory
rm -rf $tmp_home

# vim:set sw=4 sts=4 et:
