0
 +g    #!/bin/sh
#                               -*- Mode: Sh -*-
# ucfr ---
# Author           : Manoj Srivastava ( srivasta@glaurung.internal.golden-gryphon.com )
# Created On       : Tue Apr 11 11:09:15 2006
# Created On Node  : glaurung.internal.golden-gryphon.com
# Last Modified By : Manoj Srivastava
# Last Modified On : Tue Apr 11 13:50:58 2006
# Last Machine Used: glaurung.internal.golden-gryphon.com
# Update Count     : 43
# Status           : Unknown, Use with caution!
# HISTORY          :
# Description      :
#
# Register a configuration file as belonging to a package
#
# arch-tag: 6e1d33fe-a930-41ce-8d0f-c87f87b19918
#

# make sure we exit on error
set -e

# set the version and revision
progname=$(basename "$0")
pversion='Revision 3.00'
######################################################################
########                                                     #########
########              Utility functions                      #########
########                                                     #########
######################################################################
setq() {
    # Variable Value Doc_string
    if [ "x$2" = "x" ]; then
	echo >&2 "$progname: Unable to determine $3"
	exit 1;
    else
	if [ "x$VERBOSE" != "x" ]; then
	    echo >&2 "$progname: $3 is $2";
	fi
	eval "$1=\"\$2\"";
    fi
}

withecho () {
        echo "$@" >&2
        "$@"
}


purge_from_registry () {
    if [ ! -e "$statedir/registry" ]; then
        echo >&2 "$progname: Internal error: $statedir/registry does not exist";
        exit 6;
    fi

    if [ "$count" -eq 0 ]; then
        if [ "X$VERBOSE" != "X" ]; then
            echo >&2 "$progname: Association already purged. No changes.";
        fi
        exit 0;
    fi
    old_pkg=$(grep -E "[[:space:]]${real_conf_file_re}$" "$statedir/registry" | \
        awk '{print $1;}' );
    if [ "$pkg" != "$old_pkg"  ]; then
        echo >&2 "ucfr: Association belongs to $old_pkg, not $pkg";
        if [ "X$FORCE" = "X" ]; then
            echo >&2 "ucfr: Aborting";
            exit 5;
        fi
    fi

    # OK, so we have something to purge.
    for i in $(/usr/bin/seq 6 -1 0); do
	if [ -e "${statedir}/registry.${i}" ]; then
	    if [ "X$docmd" = "XYES" ]; then
		cp -f "${statedir}/registry.${i}"  "${statedir}/registry.$(($i + 1))"
	    else
		echo cp -f "${statedir}/registry.${i}" "${statedir}/registry.$(($i + 1))"
	    fi
	fi
    done
    if [ "X$docmd" = "XYES" ]; then
	cp -f "$statedir/registry"  "$statedir/registry.0"
    else
	echo cp -f "$statedir/registry"  "$statedir/registry.0"
    fi
    if [ "X$docmd" = "XYES" ]; then
	set +e
	if [ "X$VERBOSE" != "X" ]; then
	    echo "grep -Ev [[:space:]]${real_conf_file_re}$ $statedir/registry >\\"
            echo "	$statedir/registry.tmp || true";
	fi
	#echo "grep -Ev [[:space:]]${real_conf_file_re}$ $statedir/registry"
	grep -Ev "[[:space:]]${real_conf_file_re}$" "$statedir/registry" > \
	    "$statedir/registry.tmp" || true;
	if [ "X$docmd" = "XYES" ]; then
	    mv -f "$statedir/registry.tmp"  "$statedir/registry"
	else
	    echo mv -f "$statedir/registry.tmp"  "$statedir/registry"
	fi
	set -e
    fi
}

replace_in_registry () {
    if [ ! -e "$statedir/registry" ]; then
        echo >&2 "$progname: Internal error: $statedir/registry does not exist";
        exit 6;
    fi
    if [ "$count" -eq 1 ]; then
        old_pkg=$(grep -E "[[:space:]]${real_conf_file_re}$" "$statedir/registry" | \
            awk '{print $1;}' );

        if [ "$pkg" != "$old_pkg" ]; then
            divert_package=$(dpkg-divert --listpackage "$conf_file")
            if [ -n "$divert_package" ]; then
                if [ "X$VERBOSE" != "X" ]; then
                    echo >&2 "$progname: Package $pkg will not take away diverted ${conf_file} from package $divert_package";
                fi
                exit 0;
            else
                if [ "X$FORCE" = "X" ]; then
                    echo >&2 "$progname: Attempt from package $pkg  to take ${real_conf_file} away from package $old_pkg";
                    echo >&2 "ucfr: Aborting