#!/bin/ksh
#
# ident "@(#)bbmergemime.sh	1.2 01/02/01 SMI"
#
# Copyright (c) 2000 by Sun Microsystems, Inc.
# All rights reserved.
#

. $BBROOT/etc/opt/SUNWbb/blackbox.rc 
                                   
usage() {
	echo "`basename $0` [-c] <file1> <file2>"
	echo "\tmerges the mime definitions from <file1> and <file2> to" 
	echo "\tstdout, the definitions from <file2> take precedence over"
	echo "\tthose from <file1>."
	echo "\tBy default a Netscape compatible mimetype content is"		
	echo "\twritten ot stdout. 
	echo "\tIf option -c is given, then Netscape compatible mailcap"
	echo "\tcontent is written to stdout. 
}

FLAG=-m

while getopts mc c
do
        case $c in
		c)  FLAG=-c;;
                *)  usage
                    exit 1;;
        esac
done
shift `expr $OPTIND - 1` 

if [ $# != 2 ]; then
        usage
	exit 2
fi   

mimes=`bbdef2mime $2 | nawk 'BEGIN { start=1 } {

	if ( length($1) > 0) { 	
		if ( start == 1 ) {
              		start=0;
        	} else {
                	printf("|");
        	}
        	printf("%s",$1);
	}
}'`

MIME_DEFS=/tmp/mime.defs

if [ $FLAG = "-m" ]; then
	echo '#--Netscape Communications Corporation MIME Information'
	echo '#Do not delete the above line. It is used to identify the file type.'
fi

if [ "$mimes" != "" ]; then
	egrep -v -e "$mimes" $1 > $MIME_DEFS
	cat $2 >> $MIME_DEFS
	bbdef2mime $FLAG $MIME_DEFS
	rm -f $MIME_DEFS
else
	bbdef2mime $FLAG $1
fi

exit

