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

. $BBROOT/etc/opt/SUNWbb/blackbox.rc 
   
BBSOLVER=/bin/bbsolver
                                
usage() {
	echo "`basename $0` [-m|-c] <mime def file>"
	echo "extracts mimetypes, mimetype file content or mailcap"
	echo "mailcap file content from <mime def file> to stdout"
	echo "Default is extracting the mime types only"
	echo "-m extract Netscape compatible mimetype content to stdout"
	echo "-c extract Netscape compatible mailcap content to stdout"
}

FLAG=0

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

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

nawk -F';' -v F=$FLAG -v BS=$BBSOLVER '{
	if ( match($0,"#") == 0 && NF > 1 ) {

		if ( F == 0 ) {
			split($4,a,"=");
			printf("%s\n",a[2]);
		} else {
			if ( $1 != "native" ) {
				if ( F == 1 ) {
                			printf("%s %s %s\n",$3,$4,$5);
        			} else {
					split($4,a,"=");
					if ( $1 == BS ) {
						printf("%s;%s %s",a[2],$1,a[2]);
					} else {
						printf("%s;%s",a[2],$1);
					}
					if ( length($2) > 0 )
						printf(";%s\n",$2);
					else
						printf("\n");
        			}
			}
		}
	}
}' $1       
