#!/bin/sh

#
#    update_pkgok pdtinfofl 
#
#	update_pdtok is used to update packages ok files associated with
#	products
#	
#############################################################################
#############################################################################
#
#	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
#
#############################################################################

test -z "$Rm" && Rm="/bin/rm -f";            export Rm

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

tmp=$Proot/install/tmp
pkgs=$Proot/install/pkgs
pdts=$Proot/install/pdts
pdtinfofl=$1 ; shift
tmpfile=/tmp/pkg$$
tmpfl1=$tmp/tmpfl1_$$
while read pdtdesc pkgname pdtname pdtsize platform FMrelease remainder ; do
	pdtRoot="${pdts}/${pdtname}_${platform}_${FMrelease}"
	#either has ok or nok file, it should not have both
	flname=`/bin/ls ${pdtRoot}.*`
	packages=`sed -n '2,$p' ${flname} | awk '{ print $1}' | sort -u`
	for package in $packages ; do
	    echo $package >> $tmpfile
	    pkgRoot="${pkgs}/${package}"
	    test -s ${pkgRoot}.ok && {
	      /bin/cat ${pkgRoot}.ok > $tmpfl1
	      awk '$1 != "'$pdtname'" || $2 != "'$platform'" { print }' $tmpfl1 > ${pkgRoot}.ok
	      test `wc -l < ${pkgRoot}.ok` -gt 1 || \
		  head -1 ${pkgRoot}.ok 
	      $Rm $tmpfl1
	    }
	done    

	$Rm ${pdtRoot}.*
done < $pdtinfofl
# display what products share with these undeletable packages
#test -s $tmpfile && {
# cat $tmpfile | \
# while read package ; do
#    pkgRoot="${pkgs}/${package}"
#    if [ -s ${pkgRoot}.ok -a `wc -l < ${pkgRoot}.ok` -gt 1 ]; then 
#       pdts=`sed '1d' $pkgRoot.ok | awk '{ print $1 }'`
#       echo "Not delete package $package "
#       echo "due to the following products share with this package"
#       echo ""	
#       for pdt in $pdts ; do
#          echo "	$pdt"
#       done
#       echo ""
#    fi
# done
#}
$Rm $tmpfile
