#! /bin/sh -e

# generate a new dcc_conf file from an existing file

# Copyright (c) 2005 by Rhyolite Software, LLC
#
# This agreement is not applicable to any entity which sells anti-spam
# solutions to others or provides an anti-spam solution as part of a
# security solution sold to other entities, or to a private network
# which employs the DCC or uses data provided by operation of the DCC
# but does not provide corresponding data to other users.
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# Parties not eligible to receive a license under this agreement can
# obtain a commercial license to use DCC and permission to use
# U.S. Patent 6,330,590 by contacting Commtouch at http://www.commtouch.com/
# or by email to nospam@commtouch.com.
#
# A commercial license would be for Distributed Checksum and Reputation
# Clearinghouse software.  That software includes additional features.  This
# free license for Distributed ChecksumClearinghouse Software does not in any
# way grant permision to use Distributed Checksum and Reputation Clearinghouse
# software
#
# THE SOFTWARE IS PROVIDED "AS IS" AND RHYOLITE SOFTWARE, LLC DISCLAIMS ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RHYOLITE SOFTWARE, LLC
# BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
# OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
# ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
# SOFTWARE.

#	Rhyolite Software DCC 1.3.42-1.18 $Revision$

FNAME=dcc_conf

OLD=
PROTOTYPE=$FNAME
SUFFIX=new
TGT=
DCC_HOMEDIR=

USAGE="$0: [-x] [-i old] [-s new-suffix] [-p prototype] [-o tgt] -h homedir"
while getopts "xi:s:p:o:h:" c; do
    case $c in
	x) set -x; DEBUG=-x;;
	i) OLD="$OPTARG";;
	s) SUFFIX="$OPTARG";;
	p) PROTOTYPE="$OPTARG";;
	o) TGT="$OPTARG";;
	h) DCC_HOMEDIR="$OPTARG";;
	*) echo "$USAGE" 1>&2; exit 1;;
    esac
done
shift `expr $OPTIND - 1 || true`
if test "$#" -ne 0 -o -z "$DCC_HOMEDIR" -o -z "$SUFFIX"; then
    echo "$USAGE" 1>&2
    exit 1
fi

if test ! -s "$PROTOTYPE"; then
    echo "prototype $PROTOTYPE does not exist" 1>&2
    exit 1
fi

if test -z "$OLD"; then
    OLD="$DCC_HOMEDIR/$FNAME"
fi
if test ! -f "$OLD"; then
    echo "$OLD does not exist" 1>&2
    exit 1
fi

if test -z "$TGT"; then
    TGT="$OLD-$SUFFIX"
fi

# Use [ABCDEFGHIJKLMNOPQRSTUVWXYZ] patterns because some idiot has decided
# that GNU Awk 3.1.3 (at least on x86_64) should have /^[A-Z]/ match "fi"
# even with IGNORECASE=0 and in POSIX mode.

rm -f $TGT
awk '(file1 == "") {
	    file1 = FILENAME;
	}
	/^[ABCDEFGHIJKLMNOPQRSTUVWXYZ]/ {
	    inx = index($0, "=");
	    if (inx == 0) {
		    inx = length($0)+1;
	    }
	    key = substr($0, 1, inx-1);
	    val = substr($0, inx+1);
	    if (file1 == FILENAME) {
		defined[key] = 1;
		mem[key] = val;
	    } else {
		# on first line of second file, make compatibility adjustments
		if (!adjusted) {
		    adjusted = 1;
		    if (mem["DCCD_ENABLE"] == "") {
			defined["DCCD_ENABLE"] = 1;
			if (mem["SRVR_ID"] == "") {
			    mem["DCCD_ENABLE"] = "off";
			} else {
			    mem["DCCD_ENABLE"] = "on";
			}
		    }
		    # fix DCC_RUNDIR=@dcc_rundir@ bug in 1.2.14 and preceding
		    if (defined["DCC_RUNDIR"]) {
			    if (mem["DCC_RUNDIR"] == "@dcc_rundir@") {
				defined["DCC_RUNDIR"] = 0;
			    }
		    }
		    # use new values of some if old values were defaults
		    if (mem["Configure_DCC_LIBEXEC"] == mem["DCC_LIBEXEC"]) {
			    defined["DCC_LIBEXEC"] = 0;
		    }
		    if (mem["Configure_DCC_RUNDIR"] == mem["DCC_RUNDIR"]) {
			    defined["DCC_RUNDIR"] = 0;
		    }
		    if (mem["Configure_DCCUID"] == mem["DCCUID"]) {
			    defined["DCCUID"] = 0;
		    }
		    if (mem["Configure_DCC_LOGGER"] == mem["DCC_LOGGER"]) {
			    defined["DCC_LOGGER"] = 0;
		    }
		    # use or save configured values of some parameters
		    defined["DCC_CONF_VERSION"] = 0;
		    defined["Configure_DCC_LIBEXEC"] = 0;
		    defined["Configure_DCC_RUNDIR"] = 0;
		    defined["Configure_DCCUID"] = 0;
		    defined["Configure_DCC_LOGGER"] = 0;
		}
		if (defined[key]) {
		    print key "=" mem[key];
		} else {
		    print $0;
		}
	    }
	}
	!/^[ABCDEFGHIJKLMNOPQRSTUVWXYZ]/ && (FILENAME != file1) {
	    print $0;
	}' $OLD $PROTOTYPE >$TGT
