#!/bin/sh

#
#    pkg_fltr [-pkg] [infile] [-SUFFIX ...] [+pkgpatFl ...] [pkgstr ...]
#
#	-pkg: if turn this on, then process package information otherwise
#	      it should be products information
#	pkg_fltr is used to generate product/package lines based upon 
#	product/package names and/or filename suffixes.
#
#	-  The product/package lines put through pkg_fltr are explicitly 
#	   provided in a datafile <infile> (where - is stdin), and/or 
#	   implicitly provided through matching suffixes <SUFFIX> 
#	   (supported suffixes are "ok" and "nok") .
#	   
#	-  product/package names can be restricted using regular expressions.
#	   Expressions may be anchored using '^' and '$'.
#
#	output is sorted, without repetition
#	
#############################################################################
#
#	Copyright (C) Cadence Design Systems, Inc. All rights
#	reserved.  Unpublished -- rights reserved under the
#	copyright laws of the United States of America.
#
#			RESTRICTED RIGHTS LEGEND
#	Use, duplication, or disclosure by the Government is subject
#	to restrictions as set forth in subparagraph (c)(l)(ii) of 
#	the Rights in Technical Data and Computer Software clause 
#	at DFARS 52.227-7013.
#
#			Cadence Design Systems, Inc.
#			555 River Oaks Parkway
#			San Jose, CA 95134	USA
#
#############################################################################

if [ -z "$Proot" ]; then  
    echo "$0 environment not initialized" 				1>&2
    exit 1 
fi
export Proot

pkgflag=
if [ "X$1" = "X-pkg" ]; then
   pkgflag=$1
   Root="$Proot/install/pkgs"
   field="\$2"
   sortfield="+1 -2"
   shift 
else
   Root="$Proot/install/pdts"
   field="\$3"
   sortfield="+2 -3"
fi

infile=
suffixes=
pkgstrs=
pkgpats=
for arg in "$@"; do
    case "$arg" in
	-?*) suffixes="$suffixes $arg"	;;
	+?*) pkgpats="$pkgpats $arg" 	;;
	"" ) ;;						#throw out empties
	*) 
	    # pkgstrs are always after optional infile, suffixes
	    if [ -z "$infile"  -a   -z "$suffixes" ]; then
		infile="$arg"
	    else
		pkgstrs="$pkgstrs $arg"
	    fi
	    ;;
    esac
done

if [ -z "$pkgstrs" -a -z "$pkgpats" ]; then	#no regular expressions
    patfl=
    fltr='cat -'
else
    patfl=/tmp/patfl$$				#holds generated awk program
    fltr="awk -f $patfl"
    (
	set -f					#disable filename generation
	for pkgstr in $pkgstrs; do
	    echo "$pkgstr"
	done | sed -e 's@.*@'$field' == "&" {print; next}@'

	if [ ! -z "$pkgpats" ]; then
	    pkgpats=`echo $pkgpats | tr -d +`
	    cat $pkgpats	|| exit 1
	fi | sed -e 's@.*@'$field' ~ /&/ {print; next}@'
    ) > $patfl
fi

(
    test ! -z "$infile" && \
	test -s $infile && cat $infile
    if [ ! -z "$suffixes" ]; then
	suffixes=`echo "$suffixes" | sed "s@-@$Root/*.@g"` 
	set +f					#enable filename generation
  	for suffix in $suffixes ; do
	    #1st line of each file
	    #have to take out the empty line and file list
	    head -1 $suffix 2>/dev/null | sed -e '/==>/d' -e '/^[    ]*$/d'
	done
    fi
) | $fltr | sort -u $sortfield | sort  #beware of same pkgname w/diff descs
test ! -z "$patfl" && /bin/rm $patfl

exit 0

