#!/bin/sh
#+++
#  RCS $id$
#  title:	mergeactive.sh
#  abstract:	Merge two (or more) active files.
#  author:	T.R.Hageman, Groningen, The Netherlands
#  created:	December 1994
#  modified:	(see RCS Log at end)
#  copyright:
#
#		Copyright (C) 1994,1995  Tom R. Hageman.
#
#	This is free software; you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation; either version 2 of the License, or
#	(at your option) any later version.
#
#	This software is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with this software; if not, write to the Free Software
#	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#  description:
#
#	All fields from the first active file are retained.
#	Additional lines for new groups from subsequent active files have their
#	second and third fields cleared in the final output.
#---
if [ $# != 2 ]
then
	echo "Usage: $0 primary-active secondary-actives" >&2
	exit 2
fi

# Kludge to let ancient awk see dummy file (would otherwise ignore /dev/null).
case "$1" in #(
/dev/null)
	nullfile=/tmp/null$$
	trap "trap 0; rm -f $nullfile" 0
	echo >$nullfile
	shift
	set - $nullfile "$@"
esac

awk '
FILENAME != Filename { NFiles++; Filename = FILENAME }

/^$/	{ next }

NFiles == 1 {
	seen[$1] = 1
	printf("%s %010d %05d %s\n", $1, $2, $3, $4)
	next
}

{
	if (!seen[$1]) {
		print $1, "0000000000", "00001", $4
	}
}
' "$@" | sort
