#! /bin/sh -e

# generate a new dcc_conf file from an existing file

# Copyright (c) 2004 by Rhyolite Software
#
# 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.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND RHYOLITE SOFTWARE DISCLAIMS ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RHYOLITE SOFTWARE
# 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.2.74-1.14 $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 [ABCDEFGHIJKLMNOPQRSTUVWYZ] 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.

awk -F= '(file1 == "") {
	    file1 = FILENAME;
	}
	/^[ABCDEFGHIJKLMNOPQRSTUVWYZ]/ {
	    if (file1 == FILENAME) {
		defined[$1] = 1;
		mem[$1] = $2;
	    } 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[$1]) {
		    print $1 "=" mem[$1];
		} else {
		    print $0;
		}
	    }
	}
	!/^[ABCDEFGHIJKLMNOPQRSTUVWYZ]/ && (FILENAME != file1) {
	    print $0;
	}' $OLD $PROTOTYPE >$TGT
