#!/bin/sh
#
#ident  "@(#)i.ttmapsdir	1.2	00/08/18 SMI"
#
# Copyright (c) 1996,2000 by Sun Microsystems, Inc.
#

PATH="/usr/bin:/usr/sbin:${PATH}"
export PATH

while read src dest
do
	if [ ! -f $dest ] ; then
		cp $src $dest
	else
	    # Merge new entries into ttmaps.dir while
	    # removing corrupted & duplicate entries
		awk '
    /FROM-ENCODING/	{ from = $2 ; to = "" } 
    /TO-ENCODING/ 	{ if (to == "") {to = $2} else { $from = "" } } 
    /FILENAME/	 	{ if ( (from != "") && (to != "") ) {
			    tuple = sprintf "%s/%s/%s", from, to , $2 ;
			    if ( alreadyseen[tuple] != 1 ) {
			      printf "FROM-ENCODING %s\n", from;
			      printf "TO-ENCODING %s\n", to;
			      printf "FILENAME %s\n", $2;
			      alreadyseen[tuple] = 1;
			    }
			  }
			  from = ""; to = "";
			} ' $src $dest > /tmp/$$ttmaps.dir
		count=`grep '^FILENAME' /tmp/$$ttmaps.dir | wc -l`
		echo $count > $dest
		cat /tmp/$$ttmaps.dir >> $dest
		rm /tmp/$$ttmaps.dir 
	fi
done

exit 0

