#!/bin/sh
#								
#  delete_fltr -pkg [-M mesg] outfile [+pkgPatFl ...] [pkgstr ...] 
#
#	Create a file of installed product lines and then have user select
#	the products or packages to delete. 
#	The <pkgstr> and <pkgPatFl> arguments can be used to restrict the 
#	installed packages to choose from (as explained	in pkg_fltr).
#	
#
#############################################################################
#############################################################################
#
#	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
#
#############################################################################
BOURNE=/bin/sh
XTERMPATH=/usr/openwin/bin
XTERMBIN=xterm
XTERMOPTS="-T 'SoftLoad 6.0 Console' -fn 9x15  -sl 200 -g 80x40+0+0 -bg \#e0e0e0 -fg \#800000 -sb -e ${BOURNE} softload_GUI"
sysPATH=/bin:/usr/bin:/usr/ucb:$XTERMPATH

jsr=''
OSvendor=sun4v
OSarchs=$OSvendor
Exit() {

    status=$1; shift
    files="$*"
    for file in $files; do
        test -f $file && rm $file
    done
    exit $status

}

Rm="/bin/rm -f"; export Rm

NL="
"
if [ -z "$Proot" ]; then
    echo "$0 Environment not initialized.
$0 Can only be used when called form SoftLoad."                               1>&2
    exit 1
fi
export Proot

if [ -z "$InstPlat" ]; then
  echo "$0 Installation platform not specified"  1>&2
  exit 1
fi

export InstPlat

useGUI=${useGUI?'useGUI environment not initialized'}

tmp=$Proot/install/tmp
pkgs=$Proot/install/pkgs
pdts=$Proot/install/pdts
bundle=$tmp/bundles.$InstPlat
allbundle=$tmp/allbundles.$InstPlat
sortbundles=$tmp/sortbundles.$InstPlat
sortallbundles=$tmp/sortallbundles.$InstPlat
process_file=$bin_dir/process_file

if [ -z "$bin_dir" ]; then  #{
  bin_dir=$Proot/install/bin.$OSvendor
fi  #}


if [ "X$1" = "X-pkg" ]; then
    pkgflag=$1
    shift	
fi

#If this flag is set then delete fltr will not ask to select the
#packages to delete

if [ "X$1" = "X-quite" ]; then
    quiteflag=$1
    shift	
fi

msg="Select the packages to be deleted:"
if [ "X$1" = "X-M" ]; then
    shift
    msg="$1"; shift;
fi

outfile=$1; shift
pkgstr="$@"
showflag=
case $pkgstr in
  +*) showflag=yes;;
   *)  ;;
esac


#
#determine packages substr to search for
#
tmpfl1=$tmp/tmpfl1_$$
tmpfl2=$tmp/tmpfl2_$$

> $outfile
if [ ! -z "$pkgflag" -a ! -z "$pkgstr" ]; then
   $BOURNE $bin_dir/pkg_fltr -pkg -ok "$pkgstr" > $outfile
else
   if [ ! -z "$pkgstr" ]; then
     $BOURNE $bin_dir/pkg_fltr -ok -nok "$pkgstr" > $outfile
     /bin/cat $outfile > $tmpfl2	
   else
     echo "${NL}Retrieving product status..."

     if [ "X$InstPlat" = "Xsun4" -o "X$InstPlat" = "Xsun" ]; then
        pf_plat="sun4_"
     else
        pf_plat=$InstPlat
     fi

     $process_file 6 p $Proot i $sortbundles I $sortallbundles v $pf_plat S | sort -u +0 -1 | sort -n > $tmpfl1 
     #$BOURNE pkg_fltr -ok -nok | $BOURNE inv_pdts -S > $tmpfl1

     $Rm $sortallbundles 

     test -s $tmpfl1 || {
       echo "${NL}No products are installed in previous session!"
       Exit 0 $tmpfl1
     }

     if $useGUI; then
        # $bin_dir/waitMsg&
        #waitMsgPid=$!
        $BOURNE $bin_dir/toplevel.sh 9 $tmpfl1 $tmpfl2 || Exit $? $tmpfl1 $tmpfl2
        #kill $waitMsgPid >/dev/null
     else
        $BOURNE $bin_dir/choice_fltr -F -M "
Select the products to be deleted:" \
                -T "product" $tmpfl1 $tmpfl2 || Exit $? $tmpfl1 $tmpfl2
     fi	
     $Rm $tmpfl1
   fi
   if [ -s $tmpfl2 ]; then
     $BOURNE $bin_dir/update_pkgok $tmpfl2 | sort -u > $outfile
   else
     > $outfile
   fi	
   $Rm $tmpfl2
fi
if [ -s $outfile ]; then
#  test ! -z "$pkgflag" -a ! -z "$showflag" && {
  test ! -z "$pkgflag" && {
    if [ ! -z "$quiteflag" ]; then #{
      :  #Let the outfile be as it is. do not let choice_fltr modify it
    else
      /bin/cat $outfile > $tmpfl1	
      $BOURNE $bin_dir/choice_fltr -M "$msg" $tmpfl1 $outfile || Exit $? $tmpfl1	
    fi #}
  }
  /bin/cat $outfile > $tmpfl1
  cat $tmpfl1 | \
   while read pkgdesc pkgname remainder; do
	if [ -f $pkgs/$pkgname.inv  -a   -f $pkgs/$pkgname.exe ]; then
	   echo $pkgdesc $pkgname "$remainder"
	else
	   echo "${NL}SL-9: Could not locate data files for $pkgname (exe,inv)" 1>&2
        fi
   done > $outfile
   $Rm $tmpfl1
fi
test -f $tmpfl1 && $Rm $tmpfl1
exit 0
