#!/bin/sh
#
#  load_fltr  [-pkg] [-a] [-f] [-r] infile outfile [pkgstr ...]
#
#
#  -f :  Do not perform disk checks
#  -a :  Do not exclude packages which have already been successfully installed
#  -r :  Limit packages to those which failed (recovery)
#
#	Determine which packages from infile the user can, and desires to load.
#	Put corresponding package lines from <infile> into <outfile>
#
#############################################################################
#############################################################################
#
#	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/bin/X11
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=''

Clear() {
    clear		2>/dev/null	#beware of bad termcap entry
}

Page() {
    MORE=""  more
}

EchoN() {
    echo -n "$*"
}

Beep() {
    tput bel		2>/dev/null
}


Affirmative() {
    while : ; do
	read Affirm
	case $Affirm in
	    [yY]*) return 0 ;;
	    [nN]*) return 1 ;;
	    *)     $jsr EchoN "Please press y (yes) or n (no) : " ;;
	esac
    done
}

Proceed() {
    test "X$1" = 'X-q' || $jsr EchoN "Press [Return] to proceed, q to quit : "
    while : ; do
	read proc < /dev/tty
	case $proc in
	    "")    return 0 ;;
	    [qQ]*) return 1 ;;
	    *) 	   $jsr EchoN "Press [Return] to proceed, q to quit : " ;;
	esac
    done
}

OSvendor=lnx86
OSarchs=$OSvendor
Exit() {

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

}

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

if [ -z "$Proot" ]; then  #{ 
    $jsr EchoN "${NL} $0 environment not initialized. ${NL} $0 can only be used when called from 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


tmp=$Proot/install/tmp
pkgs=$Proot/install/pkgs
pdts=$Proot/install/pdts
tmpfl=$tmp/tmpfl$$

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

process_file=$bin_dir/process_file
bundles=$Proot/install/tmp/bundles.$InstPlat
sortbundles=$Proot/install/tmp/sortbundles.$InstPlat

selprodfile=$tmp/selprodfile
 
doexist_chk="yes"
dodisk_chk="yes"
dorecover="no"

infile=
outfile=
pkgstrs=
pkgflag=
object="product"
for arg in "$@"; do  #{
  case "$arg" in  #{
    -a) doexist_chk="no" ;;
    -f) dodisk_chk="no"  ;;
    -r) dorecover="yes" ;;
    -pkg) pkgflag="-pkg"
          object="package" ;;
    -*) echo unknown flag: $arg ;;
    "") ;;	#throw out empties
    ${infile}${arg}) infile=$arg ;;
    ${outfile}${arg}) outfile=$arg ;;
    *) pkgstrs="$pkgstrs $arg" ;;
  esac  #}
done  #}

echo "${NL}Loading appropriate ${object} installation information..."

export dodisk_chk  #why Get rid of this

if [ -s $infile ]; then  #{
  /bin/cat $infile > $outfile
fi  #}
  
if [ $dorecover = "yes" ]; then  #{
  if [ ! -z "$pkgflag" -a ! -z "$pkgstrs" ]; then  #{
    #Replaced this code to use process_file
    #We get the package entries out of the infile (which is the
    #pkginfo file in this case.

    for pkg in $pkgstrs; do
      awk '{ if ( $2 == "'$pkg'" ) print }' $infile
    done > $outfile

    #This will print out the lines (from outfile) for the packages that
    #have a nok file in the pkgs directory

    $process_file 2 p $Proot i $outfile k pkgs nok > $tmpfl
    mv $tmpfl $outfile
  else
    #Prints out lines from sorted bundles file that pertain products 
    #that have a nok file. A sorted file is ok because we are
    #just interested in products and not in its constituent
    #packages. We are assuming that sortbundles exists (It is
    #generated in vld
    
    $process_file 2 p $Proot i $sortbundles k pdts nok > $outfile
  fi  #}
  if [ ! -s $outfile ]; then  #{
    echo "${NL}No ${object}s to reinstall"
    exit 0
  fi  #}
#}
elif [ $doexist_chk != "no" ]; then #{
  # get installable products/packages
  if [ -s $infile ]; then #{
    if [ ! -z "$pkgflag" -a ! -z "$pkgstrs" ]; then  #{
      #Replaced code to use process_file instead of status_fltr
      #This will get the pakg lines from outfile for those packages
      #that do not have an ok file
      $process_file 2 p $Proot i $outfile k pkgs ok > $tmpfl
      mv $tmpfl $outfile
    else
      $process_file 2 p $Proot i $infile k pdts ok > $outfile
    fi  #}
  else
    if [ ! -z "$pkgflag" -a ! -z "$pkgstrs" ]; then  #{
      $BOURNE pkg_fltr -pkg -nok > $outfile
    else
      $BOURNE pkg_fltr -nok > $outfile
    fi  #}
  fi  #}

  if [ ! -s $outfile ]; then  #{
    echo "${NL}Your ${object}s are already installed."
    exit 0
  fi #}
fi #}

#We need the lines associated with products
#for the load_pkgs file. It is either
#generated here or in bundle_fltr

> $selprodfile

if [ ! -z "$pkgstrs" -a -z "$pkgflag" ]; then #{
  for pdt in $pkgstrs; do
    awk '{ if ( $3 == "'$pdt'" ) print }'  $infile
  done > $selprodfile
fi #}

if [ ! -z "$pkgstrs" -o $dorecover = "yes" ]; then  #{
  ./overwrite $outfile \
  $BOURNE pkg_fltr ${pkgflag} $outfile $pkgstrs
  if [ ! -s $outfile ]; then  #{
    for obj in $pkgstrs; do  #{
      $BOURNE pkg_fltr ${pkgflag} -ok $obj > $outfile
      if [ -s $outfile ]; then  #{
        echo "${NL}Specified $object $obj already installed."
      else
        echo "Unknown $object:  $obj."
      fi #}
    done  #}
    echo "${NL}No ${object}s installed."
    exit 1
  fi #}
  if [ -z "$pkgflag" ]; then  #{
    $BOURNE pdtMappkg $tmp/pdt$$  -f $outfile
    if [ ! -z "$pkgstrs" ]; then  #{
      okfile=$tmp/okfile$$
      > $okfile
      $jsr EchoN "${NL}Updating pre-load product history"
      > $tmp/pdtpkg$$
      awk '{print $1}' $outfile | \
        while read prodStr ; do  #{
          grep "^$prodStr " $infile >> $tmp/pdtpkg$$
        done  #}
      cat $tmp/pdtpkg$$ | \
      while read pdtdesc pkgname pdtname pdtsize platform FMrelease ; do #{
        pdtRoot="$Proot/install/pdts/${pdtname}_${platform}_${FMrelease}"
        pkgRoot="$Proot/install/pkgs/${pkgname}"
        test -s ${pkgRoot}.ok && {
          # If the pkgname.ok file exists (it's already been loaded
          # successfully) and this product isn't listed as depending
          # on that package, establish that relationship
          test `egrep -c "^$pdtname $platform " ${pkgRoot}.ok` -eq 0 && \
          echo $pdtname $platform $FMrelease >> ${pkgRoot}.ok
        }
        # If the product.nok file doesn't exist, create it and add the
        # initialization info

        test -s ${pdtRoot}.nok || {
         echo "$pdtdesc pkgname $pdtname $pdtsize $platform $FMrelease" > ${pdtRoot}.nok
         # New product.nok generated - output a dot to let them know
         # we're still here and running
         $jsr EchoN "."
        }
        # Update processing files to reflect the products/packages to
        # load
        echo $pdtRoot >> $okfile
        # If this package doesn't yet appear in the product.nok file,
        # add it in
        test `egrep -c "^$pkgname$" ${pdtRoot}.nok` -eq 0 && \
          echo $pkgname >> ${pdtRoot}.nok
      done  #}
      rm -f $tmp/pdtpkg$$
      echo ""
    fi #}

    #get installable packages
    #All those packages that do not have an ok file in the
    #pkgs file

    $process_file 2 p $Proot i $tmp/pdt$$ k pkgs ok > $tmpfl
    mv $tmpfl $tmp/pdt$$

    # all the corresponding packages have been installed
    test -s $tmp/pdt$$ || { \
      $jsr EchoN "${NL}Updating$pkgstrs installation history..."
      for nokfile in `/bin/cat $okfile | sort -u` ; do  #{
         mv ${nokfile}.nok ${nokfile}.ok
      done  #}
      > $outfile ; exit 0
    }
  else
    /bin/cat $outfile > $tmp/pdt$$
  fi #}

  test -s $tmp/pdt$$ && {
    if [ $dodisk_chk != "no" ]; then #{
      $BOURNE disk_chk -N $tmp/pdt$$
      mystatus=$?
      case $mystatus in  #{
        0) ;;         #proceed installation
        *) Exit $mystatus $tmp/pdt$$ ;; 
      esac  #}
    else
      echo "${NL}Disk checks ignored"
    fi  #}
  }
  $Rm $tmp/pdt$$	
  if [ -z "$pkgflag" ]; then  #{
    /bin/cat $outfile > $tmpfl
    $BOURNE make_pdtnok $tmpfl | sort -u > $outfile
    $Rm $tmpfl 
  fi  #}
#}
elif [ $dorecover != "yes" ]; then  #{
  /bin/cat $outfile > $tmpfl
  $BOURNE bundle_fltr ${pkgflag} $tmpfl $outfile || Exit $? $tmpfl
  mystatus=$?
  case $mystatus in  #{
    222) exit 222;; #go back to the select products
    255) exit 255;;
    250) exit 250;;
  esac  #}
  $Rm $tmpfl
fi  #}

test -s $outfile || exit 0  # no packages...no checks or verifications

## what packages should be deleted prior to loading selected packages?
#
#deleteData=$Proot/install/tmp/mapinfo.$InstPlat
#if [ -s $deleteData ]; then  #{
#  tmpExprs=$tmp/pkgExpr$$
##
## generate and run awk program with lines of the following form
## to collect expressions for pkgs which may need to be deleted
##   $1 == "pkgname" { print $2; next }
##
#  if [ ! -z "$pkgflag" -a ! -z "$pkgstrs" ]; then #{
#    sed  's@^[^ ]* \([^ ]*\).*$@$1 == "\1" { print $2; next}@' $outfile \
#	    >  $tmp/awke$$
#  else
#    sed  's@\([^ ]*\).*$@$1 == "\1" { print $2; next}@' $outfile \
#    >  $tmp/awke$$
#  fi  #}
#  awk -f $tmp/awke$$  $deleteData > $tmpExprs
#  $Rm    $tmp/awke$$
#
#  if [ -s $tmpExprs ]; then  #{
#    tmpDels=$tmp/dels$$
#    $BOURNE delete_fltr -pkg -M "
#The following packages should be deleted before new versions are installed.
#Confirm the packages to be deleted:" $tmpDels +$tmpExprs
#
#    if [ -s $tmpDels ]; then  #{
#      $BOURNE delete_pkgs $tmpDels || Exit $? $tmpDels $tmpExprs
#    fi  #}
#    test -s $tmpDels && $Rm $tmpDels
#    echo ""
#  fi  #}
#  $Rm $tmpExprs
#fi #}

exit 0

