#!/bin/sh
#
#  softload -i [-pkg][pkgstr ...] 
#  softload -d [-pkg][pkgstr ...]
#  softload -l [-pkg][host:]tapeDev[,[host:]dir ...] [-f] [-a] [-r] [pkgstr ...]
#
#  -i:	display inventory of products 
#      -pkg: list inventory of packages
#  -d:  delete product(s) (interactively)
#      -pkg: delete package(s)
#  -l:  load product(s) from media
#      -pkg: load package(s) from media
#      -f:  omit disk resource checking before loading 
#      -a:  allow user to (re)load all products/packages from tape 
#      -r:  attempt to reload corrupted products/packages
#
##############################################################################
#
#  vld software requires:
#	/bin/{sh,rm,ls,cat}
#  and needs the following in the path:
#       awk dd df egrep expr fgrep more mv sed sort tar tr wc
#
#############################################################################
#
#	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
#
#############################################################################
OSvendor=lnx86
OSarchs=$OSvendor

Dirname() {					#print argument minus leaf, or .
    case $1 in
	*/* )	echo $1 | sed 's@/[^/]*$@/@' ;;
	*)	echo "." ;;
    esac
}

Leafname() {					#print leaf
    case $1 in
	*/* )	echo $1 | sed 's@.*/@@' ;;
	*)	echo $1 ;;
    esac
}

Fullpath() {					#print rooted path to arg dir
    ( cd $1 && /bin/pwd |
	sed 's@^/tmp_mnt/@/@' )			#print automount access dir
}

Dirpath() {					#print rooted path to arg - leaf
    $jsr Fullpath `$jsr Dirname $1`
}

Suffix() {
    echo $1 | sed -n 's@.*\.@@p'
}

Prefix() {
    echo $1 | sed 's@\.[^.]*$@@'
}

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
}


SetExport() {
    file=$1
    variable=$2
    entry=$3

    test -f $file || touch $file
    grep -s ${variable} ${file} 2>&1 > /dev/null

    if [  "$?" = 0 ]; then
        grep -v ${variable} ${file} > /tmp/infile$$
        cp /tmp/infile$$ ${file}
        rm /tmp/infile$$
    fi

    echo "${variable}=\"${entry}\" ; export ${variable}" >> $file
}

Exit() {

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

}
#This did not work for the -v flag. Fixed it so that it works fine
#now



Fgrepf() {		# fgrep -v filename
    vset=false
    Util="fgrep"
    opts=
    while test $# -gt 0 ; do
       case $1 in
          -egrep)
               Util="egrep"
               shift;;
          -v)
               opts="$opts `expr X$1 : '.\(.*\)'`"
               shift
               vset=true;;
          -*)
               opts="$opts `expr X$1 : '.\(.*\)'`"
               shift;;
           *)  break;;
       esac
    done

    patFl=$1
    srcFl=$2

    split -75 $patFl /tmp/split$$_

    for pat in /tmp/split$$_* ; do
      if $vset; then
       count=0
       $Util $opts -f $pat $srcFl > /tmp/srcTFL$$
       srcTFL="/tmp/srcTFL$$"
       for pat1 in /tmp/split$$_* ; do
        count=`expr $count + 1`
        $Util $opts -f $pat1 $srcTFL > /tmp/srcFL${count}$$
        srcTFL="/tmp/srcFL${count}$$"
        echo $count > /tmp/count$$
       done
       if [ -s /tmp/count$$ ]; then
        read count < /tmp/count$$
       fi  
       cat /tmp/srcFL${count}$$ | sort -n | uniq >> /tmp/resFL$$
       while [ "$count" -gt 0 ]; do
         rm -f /tmp/srcFL${count}$$
         count=`expr $count - 1`
       done
       rm -f /tmp/srcTFL$$
      else
        $Util $opts -f $pat $srcFl
      fi
    done | sort -n | uniq
    if $vset; then
      cat /tmp/resFL$$ | sort -u | sort -n | uniq
    fi
    rm -f /tmp/resFL$$ /tmp/count$$ /tmp/split$$_* 
    
}

GetMedia() {

   	InFile=$1; shift
	OutFile=$1; shift
	MFile=$1; 

        DeviceTAPE=$tapeDev
        DeviceCD=/cdrom
        test -s $recordFile && . $recordFile
        sed  's@\([^ ]*\).*$@$2 == "\1" { print $4":"$5 }@' $InFile > $tmp/awkb$$
        tapesets=`awk -f $tmp/awkb$$ $pkginfo | sort -u`
        $Rm $tmp/awkb$$
        for tape in $tapesets ; do
          awk '{if ($1 == "'$tape'") print $2 " " $3 " " $1}' $mediaMap | tee -a $MFile
        done | awk 'BEGIN { Ofile="'$OutFile'" }
                    { nm[$1]=nm[$1]+1 }
                    END {
                     for (i in nm) {
                      if ( i == "CDROM" ) { print i " " nm[i] " " "'$DeviceCD'" >> Ofile }
                      if ( i == "TAPE" ) { print i " " nm[i] " " "'$DeviceTAPE'" >> Ofile }
                      if ( i != "TFILE" ) { printf(" %d %s", nm[i], i) }
                      }
                      printf("\n")
                    }'  2>/dev/null
	return 0
}
 

if [ -z "$1" ]; then
    echo "${NL}${usagestr}${NL}"
    exit 1
fi

tapeDev=/noDefaultSpecified


#This number must be incremented every release. This is to
#prevent the customer from using a 9403 softload to install
#9404 products


pkgflag=
object="product"
majOption=$1; shift
optArgs="$*"
test $# -gt 0 && shift $#	#precaution against interaction w/sh fnxn args

#
# figure out were install bin is, and thereby the pseudo-root
#
if [ -z "$bin_dir" ]; then  #{
  bin_dir=`pwd`; 	readonly bin_dir;	export bin_dir
fi  #}

#Proot is being exported by sofltoad. We do not set it again

test -z "$Rm" && Rm="/bin/rm -f"; 	export Rm	# for the paranoid...
NL="
"

CALLCRC="${NL} Call the Cadence Customer response Center (CRC) at 1-800-Cadenc2 ${NL} (1-800-223-3622) to get the right files"
useGUI=${useGUI?'useGUI environment not initialized'}

if [ -z "$InstPlat" ]; then
  echo "Error: Installation Platform not specified"
  exit 1;
else
  export InstPlat
fi

tmp=$Proot/install/tmp
pkgs=$Proot/install/pkgs
pdts=$Proot/install/pdts
pkginfo=$tmp/pkginfo.$InstPlat
bundles=$tmp/bundles.$InstPlat
allbundles=$tmp/allbundles.$InstPlat
allpkginfo=$tmp/allpkginfo.$InstPlat
sortallbundles=$tmp/sortallbundles.$InstPlat
sortbundles=${tmp}/sortbundles.$InstPlat
mediaMap=$tmp/mediaMap.$InstPlat
mapinfo=$tmp/mapinfo.$InstPlat
recordFile=$Proot/install/.cdsloadrc.$OSvendor
process_file=$bin_dir/process_file
selprodfile=$tmp/selprodfile
$Rm $selprodfile


#Merge the all* and the ctl files only if the check control flag
#is set to "yes" and only if we are not using Cadence Catalog
#or the Documentation Catalog to install

      DeviceCD=/cdrom
    test -s $recordFile && . $recordFile
    case $MediaType in  #{
      CDROM)   device=$DeviceCD;;
      TAPE)    device=$DeviceTAPE;;
    esac  #}

if [ "X$checkControl" = "Xyes" ]; then  #{
  ctl_softloadVersion=
  allsoftloadVersion=
  
  #It can be 1, 2, 3, 0r 6 for workorder, email, tape or update
  if [ "X$sselection" != "X4" -a "X$sselection" != "X5" ]; then #{

    if [ -s $tmp/ctl_bundles.$InstPlat -a -s $tmp/allbundles.$InstPlat ]; then  #{
      $bin_dir/genControl pdts $tmp/ctl_bundles.$InstPlat $tmp/allbundles.$InstPlat $bundles

      if [ $? = "11" ]; then  #{
        msg="${NL}SL-1: Installation information mismatch (bundles).$CALLCRC "         
        if $useGUI; then  #{
          $BOURNE $bin_dir/msgDialog.sh "$msg" 
        else
          echo "$msg" 1>&2
        fi  #}
        exit 1     
      fi #}Return status from genControl.c
    fi  #}
    
    if [ -f $tmp/ctl_pkginfo.$InstPlat -a -s $tmp/allpkginfo.$InstPlat ]; then  #{
      $bin_dir/genControl pkgs $tmp/ctl_pkginfo.$InstPlat $tmp/allpkginfo.$InstPlat $tmp/pkg$$
      awk '$2 == "" { print $1 }' $tmp/ctl_bundles.$InstPlat > $tmp/noover_pkg$$
      if [ -s $tmp/noover_pkg$$ ]; then #{
        $jsr Fgrepf $tmp/noover_pkg$$ $tmp/allbundles.$InstPlat | awk '{ print $2 }' | sort -u > $tmp/bund_pkg$$
        $jsr Fgrepf $tmp/bund_pkg$$ $tmp/allpkginfo.$InstPlat >> $tmp/pkg$$
      fi  #}
      sort -u $tmp/pkg$$ > $pkginfo
      $Rm  $tmp/noover_pkg$$ $tmp/bund_pkg$$ $tmp/pkg$$
    fi  #}
    
    if [ -s $tmp/ctl_mediaMap.$InstPlat ]; then  #{
      cat $tmp/ctl_mediaMap.$InstPlat > $mediaMap
    fi  #}

    if [ -s $tmp/ctl_mapinfo.$InstPlat ]; then  #{
      cat $tmp/ctl_mapinfo.$InstPlat > $mapinfo
    fi #}

    ctl_softloadVersion=`head -1 $tmp/ctl_softloadVersion.$InstPlat`
    #allsoftloadVersion=`head -1 $tmp/allsoftloadVersion.$InstPlat`

    if [ "X$softloadVersion" = "X$ctl_softloadVersion" ]; then  #{
      cat $tmp/ctl_softloadVersion.$InstPlat > $tmp/softloadVersion.$InstPlat
    else
      msg="${NL}SL-1: Installation information mismatch (softload version). $CALLCRC"

      if $useGUI; then   #{ 
        $BOURNE $bin_dir/msgDialog.sh "$msg" 
      else
        echo "$msg"           1>&2
      fi  #} 

      exit 1
    fi  #}
  else #sselection !=4 & !=5
    if [ -s $tmp/allbundles.$InstPlat ]; then  #{
      cat $tmp/allbundles.$InstPlat > $bundles
    fi  #}
 
    if [ -s $tmp/allpkginfo.$InstPlat ]; then  #{
      cat $tmp/allpkginfo.$InstPlat > $pkginfo
    fi  #}
    
    if [ -s $tmp/allmediaMap.$InstPlat ]; then  #{
      cat $tmp/allmediaMap.$InstPlat > $mediaMap
    fi  #}

    if [ -s $tmp/allmapinfo.$InstPlat ]; then  #{
      cat $tmp/allmapinfo.$InstPlat > $mapinfo
    fi  #}

    #allsoftloadVersion=`head -1 $tmp/allsoftloadVersion.$InstPlat`
    
    #if [ "X$softloadVersion" != "X$allsoftloadVersion" ]; then  #{
    #  msg="${NL}SL-1: Installation information mismatch (softload version). $CALLCRC"
    #  if $useGUI; then  #{  
    #    $BOURNE $bin_dir/msgDialog.sh "$msg" 
    #  else
    #    echo "$msg" 1>&2
    #  fi  #} 

    #  exit 1

    #else
    #  cat $tmp/allsoftloadVersion.$InstPlat > $tmp/softloadVersion.$InstPlat 
    #fi  #}

  fi  #}sselection

  if [ "X$sselection" = "X7" ]; then
    sselection=5; export sselection
  fi
   
  if [ ! -s "$pkginfo" ]; then  #{
    msg="${NL}SL-2: Missing installation information (pkginfo). $CALLCRC"
    if $useGUI; then  #{
      $BOURNE $bin_dir/msgDialog.sh "$msg" 
    else
      echo "$msg" 1>&2
    fi  #}
    exit 1     
  fi  #}
    
  if [ ! -s "$bundles" ]; then  #{
    msg="${NL}SL-2: Missing installation information (bundles). $CALLCRC"
    if $useGUI; then  #{
       $BOURNE $bin_dir/msgDialog.sh "$msg" 
    else
       echo "$msg"   1>&2
    fi  #}
    exit 1     
  else
    sort -u +2 -3 $allbundles | sort -n > $sortallbundles
    sort -u +2 -3 $bundles | sort -n >  $sortbundles
  fi  #}
    
  if [ ! -s $mediaMap ]; then  #{
    msg="${NL}SL-2:Missing installation information (mediamap). $CALLCRC"
    if $useGUI; then  #{
      $BOURNE $bin_dir/msgDialog.sh "$msg" 
    else
      echo "$msg"          1>&2
    fi  #}
       
    exit 1
  fi  #}
fi #}checkControl = yes test

case $majOption in  #{
  -i) # inventory option
    Plat=
    for arg in $optArgs; do  #{
      case "$arg" in  #{
        -pkg)
          object="package"
          pkgflag=$arg ;;
        -*)
          echo "${NL}unrecognized option $arg ${NL}$usagestr" 1>&2
          exit 1 ;;
        $arg$Plat)
          Plat=$arg ;;
        *)
          pkgstrs="$pkgstrs $arg" ;;
      esac  #}
    done  #}
  
    set -f   #disable filename generation
  
    echo "${NL}Retrieving $object status..."
  
    tmpfl1=$tmp/tmpfl1_$$

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

    if $useGUI; then  #{
      $process_file 6 p $Proot i $sortbundles I $sortallbundles v $pf_plat S | sort -u +0 -1 | sort -n > $tmpfl1
      $BOURNE $bin_dir/toplevel.sh 8 $tmpfl1 ||  Exit $? $tmpfl1 
      case $? in  #{
        255) exit 255;;
        250) exit 250;;
        *) ;;
      esac  #}
  
      $Rm $tmpfl1
    else
      if [ -z "$pkgflag" ]; then  #{
        if [  -z "$pkgstrs" ]; then  #{
          $process_file 6 p $Proot i $sortbundles I $sortallbundles  v $pf_plat | sort -u +0 -1 | sort -n > $tmpfl1
          if [  -s $tmpfl1 ]; then  #{
            cat < $tmpfl1
  	  $Rm $tmpfl1
          else
            echo "${NL}No products with status Installed or Failed found.${NL}"
          fi  #}
        else
          $BOURNE pkg_fltr $bundles -ok -nok $pkgstrs | $BOURNE inv_pdts | $jsr Page
        fi  #}
      else
        $BOURNE pkg_fltr -pkg $pkginfo -ok $pkgstrs | $BOURNE inv_pkgs
      fi  #}
    fi  #}
    ;;

  -d) # delete option
      #determine packages to delete
    Plat=""
    for arg in $optArgs; do  #{
      case "$arg" in  #{
        -pkg)
          pkgflag=$arg ;;
        -*)
          echo "${NL}unrecognized option $arg ${NL}$usagestr" 1>&2
          exit 1 ;;
        $arg$Plat)
          Plat=$arg ;;
        *)
          pkgstrs="$pkgstrs $arg" ;;
      esac  #}
    done  #}
    set -f			#disable filename generation
    $BOURNE delete_fltr ${pkgflag} $tmp/delpkgs$$ $pkgstrs || Exit $? $tmp/delpkgs$$
    test ! -s $tmp/delpkgs$$ && Exit 0 $tmp/delpkgs$$
    #
    #do the deletions
    #
    $BOURNE delete_pkgs $tmp/delpkgs$$  || Exit $? $tmp/delpkgs$$
    $BOURNE update_pdtok $tmp/delpkgs$$ || Exit $? $tmp/delpkgs$$
    $Rm  $tmp/delpkgs$$
    ;;

  -l | -c | -cd | -u ) # load  or update option
 
    #We check the release once again here
    if [ -s $Proot/.FMrelease ]; then  #{
      CDS_Release=`head -1 $Proot/.FMrelease`
      if [ "X$CDS_Release" != "X$Release" ]; then #{
        $jsr EchoN "${NL}Error:The current installation hierarchy is for $CDS_Release release.${NL}CD#1 is for $Release release. ${NL}Cannot install $Release release in this hierarchy.${NL}"
        $jsr Exit 1
      fi #}
    else
      test -z "$Release" || echo "$Release" > $Proot/.FMrelease
    fi #}


    if [ -z "$pkginfo" ]; then
      echo "${NL}Unable to load without $pkginfo file"   1>&2
      exit 1
    fi
    if [ ! -s $bundles ]; then #{
      echo "${NL}Unable to load without $bundles file"   1>&2
      exit 1
    fi #}


    options=
    pkgstrs=
    NoConfirm=
    tapedev=
    Plat=
    set -f   #disable filename generation
    for arg in $optArgs; do #{
      case "$arg" in  #{
        -[far])
          options="$options $arg" ;;
        -pkg)
          pkgflag=$arg ;;
        -*)
          echo "${NL}unrecognized option $arg ${NL}$usagestr" 1>&2
          exit 1 ;;
        $arg$Plat)
          Plat=$arg ;;
        $arg$tapedev)	
          case $arg in  #{
            /*) ;;
            *:/*) ;;
            *) 
              echo "${NL}Error: Media specification ($arg) must be fully rooted" 1>&2 
              exit 1 
              ;;
          esac   #}
          NoConfirm="yes"; export NoConfirm
          tapedev=$arg ;;
        *)
          pkgstrs="$pkgstrs $arg" ;;
      esac #}
    done #}
        
    # have user select packages, optionally verify disk space

    set -f  #disable filename generation
    if [ ! -z "$pkgflag" -a ! -z "$pkgstrs" ]; then  #{
      object=package
      infile=$pkginfo
    else
      infile=$bundles
    fi  #}
    
    $Rm $tmp/mdsk_chk
    if [ "X$majOption" != "X-u" ]; then #{
           
      #We remove this file now. If we detect this file in
      #load_pkgs after the exe inv files have been 
      #extracted we will do the disk checks again
      #If this file is created between now and
      #before we start loading packages it will mean
      #that disk checks were postponed. So we will check disk space once
      #again in load_pkgs
  
      $BOURNE load_fltr ${pkgflag} $options $infile $tmp/loadpdts$$ $pkgstrs || Exit $? $tmp/loadpdts$$
      mystatus=$?
      case $mystatus in  #{
        222) exit 222;;
        255) exit 255;;
        250) exit 250;;
      esac  #}
    else
       #We do the disk checks later in load_pkgs
       echo "postponed" >$tmp/mdsk_chk
    fi #}

    #If majOption is not the documentation catalog then
    #Add the unselected .ok products
    #WE ARE NOT WORRYING ABOUT THE PKG OPTION FOR NOW 
    #If the user uses the pkg option we shoudl warn them in big 
    #bold letters

    if [ "X$majOption" != "X-cd"  -a -z "$pkgflag" ]; then #{ 
      #Get the list of okprods
      #and nok products

      okprods=/tmp/okprods$$
      nokprods=/tmp/nokprods$$
      unselokprods=/tmp/unselokprods$$
      unselnokprods=/tmp/unselnokprods$$
      extras=$tmp/extras$$

      /bin/ls ${pdts}  2> /dev/null | \
        awk -F. '{ if ( $NF == "ok" ) print $1 }' | \
        awk -F_ '{ if ( $2 == "'$InstPlat'" && $3 == "'$Release'" ) print $1 " " $2 " " $3 }' | sort -u | sort -n > $okprods

      if [ -s $okprods ]; then #{
        if [ -s $selprodfile ]; then #{
          awk '
            FILENAME == "'$selprodfile'" {
              if ( Selected[$3] == $3 ) {
                next;
              }
              Selected[$3]=$3;
            }
    
            FILENAME == "'$okprods'" {
              if ( Selected[$1] == $1 ) {
                next;
              }
              print $1 " " $2 " " $3 >> "'$unselokprods'"
            }
          ' $selprodfile $okprods
        else #If selprodfile is empty then
          /bin/cat $okprods > $unselokprods
        fi #} selprodfile is not empty
      fi #} okprods file is not empty

      /bin/ls ${pdts}  2> /dev/null | \
        awk -F. '{ if ( $NF == "nok" ) print $1 }' | \
        awk -F_ '{ if ( $2 == "'$InstPlat'" && $3 == "'$Release'" ) print $1 " " $2 " " $3 }' | sort -u | sort -n > $nokprods

      if [ -s $nokprods ]; then #{
        if [ -s $selprodfile ]; then #{
          awk '
            FILENAME == "'$selprodfile'" {
              if ( Selected[$3] == $3 ) {
                next;
              }
              Selected[$3]=$3;
            }
    
            FILENAME == "'$nokprods'" {
              if ( Selected[$1] == $1 ) {
                next;
              }
              print $1 " " $2 " " $3 >> "'$unselnokprods'"
            }
          ' $selprodfile $nokprods
        else #If selprodfile is empty then
          /bin/cat $nokprods > $unselnokprods
        fi #} selprodfile is not empty
      fi #} nokprods file is not empty

      > $extras
      if [ -s $unselnokprods  ]; then #{
        #If some .nok products exist and it is not the command line
        #interface then
        #We prepare to show the user the list of nok products

        awk ' 
              FILENAME == "'$bundles'" {
                if ( ProdName[$3] == $3 ) {
                  next;
                }
                ProdName[$3]=$3;
                ProdDesc[$3]=$1;
                ProdPlat[$3]=$5;
                ProdRel[$3]=$6
              }
              #We get the rest of the descriptions from the allbudles file
      
              FILENAME == "'$allbundles'" {
                if ( ProdName[$3] == $3 ) {
                  next;
                }
                ProdName[$3]=$3;
                ProdDesc[$3]=$1
                ProdRel[$3]=$6
                ProdPlat[$3]=$5
              }
      
              FILENAME == "'$unselnokprods'" {
                if ( ProdName[$1] == $1 ) {
                  printf ( "%-50s (%s,%s)\n", ProdDesc[$1] , ProdPlat[$1] , ProdRel[$1] );
                  next;
                }
              }
              
          ' $bundles  $allbundles $unselnokprods | tr '~' ' ' | sort -n > /tmp/showfile$$
        if [ -s /tmp/showfile$$ ]; then #{
          if $useGUI; then #{
            msg="${NL}According to the information currently available, SoftLoad does not ${NL}think that the above products are properly installed.${NL}${NL}Do you want to install these products?${NL}Click OK to select these products for installation.${NL}Click Cancel to continue installation without these products.${NL}"
            $BOURNE $bin_dir/toplevel.sh 24 /tmp/showfile$$ "$msg"
            if [ $? -eq 51 ]; then #{
              incProds="yes"
            fi #}
          else
            $jsr EchoN "${NL}${NL}####################################################${NL}According to the information currently available, ${NL}SoftLoad does not think that the following ${NL}products are properly installed.${NL}####################################################${NL}${NL}" > /tmp/showfile1$$
            /bin/cat /tmp/showfile$$ >> /tmp/showfile1$$

            $jsr EchoN "${NL}${NL}Do you want to install these products?${NL}${NL}" >> /tmp/showfile1$$
            cat /tmp/showfile1$$ | $jsr Page

            $jsr EchoN "${NL}Please press y (yes) or n (no) : "
            if $jsr Affirmative; then #{
              incProds="yes"
            fi #}
          fi #}
          $Rm /tmp/showfile$$ /tmp/showfile1$$
        fi #}
        if [ "X$incProds" = "Xyes" ]; then #{
          cat $unselnokprods > $extras
        fi #}
      fi #}If unselnokprods is not empty
      
      if [ -s $unselokprods ]; then #{  
        /bin/cat $unselokprods >> $extras
      fi #}

      $Rm $okprods $nokprods $unselokprods $unselnokprods
      if [ -s $extras ]; then #{
        #We need to add to loadpdts$$, selprodfile
        #Maybe even to the bundles file
        #Line in the awk file looks like:
        #$3 == "972" { print }

        sed  's@^\([^ ]*\).*$@$3 == "\1" { print }@' $extras > /tmp/awk$$
        awk -f /tmp/awk$$ $bundles > /tmp/bundles$$

        if [ `awk '{print $3}' /tmp/bundles$$ | sort -u | wc -l` -ne `wc -l < $extras` ]; then #{ 
          #Some products in $extras file are not defines in $bundles
         
          awk '
              FILENAME == "'/tmp/bundles$$'" {
                if ( ProdFound[$3] == $3 ) {
                  next;
                }
                ProdFound[$3]=$3;
              }
              FILENAME == "'$extras'" {
                if ( ProdFound[$1] == $1 ) {
                  next;
                }
                print $1 >> "'/tmp/extrasNF$$'"
              }
          ' /tmp/bundles$$ $extras
          sed  's@^\([^ ]*\).*$@$3 == "\1" { print }@' /tmp/extrasNF$$ > /tmp/awk$$
          #Now we look in the allbundles file
          awk -f /tmp/awk$$ $allbundles > /tmp/bundles1$$

          #Adding to the bundles file

          /bin/cat /tmp/bundles1$$ >> $bundles
          /bin/cat /tmp/bundles1$$ >> /tmp/bundles$$
          $Rm /tmp/extrasNF$$ /tmp/bundles1$$ 
        fi #}
 
        $Rm  /tmp/awk$$


        #The following line gets the list of pkgs not already
        #installed
        $bin_dir/process_file  2 p $Proot i /tmp/bundles$$ k pdts ok > /tmp/uninst$$
        if [ -s /tmp/uninst$$ ]; then #{
          /bin/cat /tmp/uninst$$ >> $selprodfile #Rest of the products
         
          #We redo the disk checks in load_pkgs
          #as we are selecting more packages  for installation

          echo "postponed" >$tmp/mdsk_chk
          
          #Checking if all pkg lines are there in the pkginfo file
  
          awk '{print $2 }' /tmp/uninst$$  | sort -u >  /tmp/pkgs$$
          sed -e 's@^@ $2 == "@'  /tmp/pkgs$$ |\
            sed -e 's@$@" { print } @' >  /tmp/awk$$
  
          awk -f /tmp/awk$$ $pkginfo > /tmp/pkglines$$
         
          if [ `wc -l < /tmp/pkgs$$` -gt `wc -l < /tmp/pkglines$$` ]; then #{
            awk '
                FILENAME == "'/tmp/pkglines$$'" {
                  if ( PkgsFound[$2] == $2 ) {
                    next;
                  }
                  PkgsFound[$2]=$2;
                }
                FILENAME == "'/tmp/pkgs$$'" {
                  if ( PkgsFound[$1] == $1 ) {
                    next;
                  }
                  print $1 >> "'/tmp/pkgsNF$$'"
                }
            ' /tmp/pkglines$$ /tmp/pkgs$$
  
            if [ -s /tmp/pkgsNF$$ ]; then #{
  
              sed -e 's@^@ $2 == "@'  /tmp/pkgsNF$$ |\
                sed -e 's@$@" { print } @' >  /tmp/awk$$
  
              #Now we look in the allpkginfo file 
              awk -f /tmp/awk$$ $allpkginfo > /tmp/pkglines1$$
              cat /tmp/pkglines1$$ >> $pkginfo
              $Rm /tmp/pkgsNF$$ /tmp/pkglines1$$
            fi #}
          fi #}#If all pkglines are in the pkginfo file

          #Adding to the loadpdts$$ file
          #Now we cannot add everything that is in uninst$$ to the
          #loadpdts file. Some of the packages in uninst$$ are
          #already installed. We make sure that if we add in a pkg
          #line, that pkg is not installed.

          sed  's@^[^ ]* \([^ ]*\).*$@$2 == "\1" { print }@' /tmp/uninst$$ > /tmp/awkb$$
          awk -f /tmp/awkb$$ $pkginfo  > /tmp/pkgs$$
          #Get the list of installable packages
          $process_file 2 p $Proot i /tmp/pkgs$$ k pkgs ok > /tmp/pkgs1$$
          if [ -s /tmp/pkgs1$$ ]; then #{
            sed  's@^[^ ]* \([^ ]*\).*$@$2 == "\1" { print $2, $3, $5, $6 }@'  /tmp/pkgs1$$ > /tmp/awk$$
            $Rm /tmp/awkb$$
            awk -f /tmp/awk$$ /tmp/uninst$$  | sort -u >> $tmp/loadpdts$$
            $Rm /tmp/awk$$
            $Rm /tmp/pkgs1$$
          fi #}
          $Rm /tmp/pkgs$$
        fi #}

        $Rm /tmp/bundles$$ $extras
        $Rm /tmp/pkglines$$ /tmp/pkgs$$ /tmp/awk$$ /tmp/uninst$$
      fi #}
      #At this point we should be all set
      #The loadpdts, bundles, pkginfo and selprod files have
      #been updated (If they needed any updating)
    fi #}
      

    if [ ! -s $tmp/loadpdts$$ ]; then  #{
      if [ -s $selprodfile ]; then #{
        tmpfl=$tmp/tmpfl$$
        while read pdtdesc pkgname pdtname pdtsize platform FMrelease ; do #{
          echo "$pkgname $pdtname $platform $FMrelease"
        done < $selprodfile  > $tmpfl #}
        if [ -s $tmpfl ]; then
          $jsr EchoN "${NL}Updating status of installed products...${NL}"
          $BOURNE $bin_dir/check_pdtok $tmpfl
        fi #}
      fi #}
 
      if [ "X$majOption" = "X-u" ]; then #{
        msg="No updates available for installation. All the ${NL}corresponding packages of selected products${NL}have been installed in a previous session.${NL}"
      else
        msg="All corresponding packages of selected products${NL}have been installed in a previous session.${NL}"
      fi #}

      if $useGUI; then #{
        $BOURNE $bin_dir/msgDialog.sh "$msg" 
      else
        $jsr EchoN "${NL}$msg"
      fi #}

      Exit 0 $tmp/loadpdts$$
    else
      ctl_files_xtract=$tmp/ctl_files_xtract$$
      export ctl_files_xtract
      if [ ! -z "$pkgflag" -a ! -z "$pkgstrs" ]; then  #{
        awkfield=2
      else
        awkfield=1
      fi  #}

      #For the Doc Catalog
      if [ "X5" = "X$sselection" ]; then 
        awk '$'"$awkfield"' ~ /^.*Doc.[0-9.]*-[dabps][0-9]*.*$/ { print $'"$awkfield"' }' $tmp/loadpdts$$ | sort -u 
      else
        awk '{ print $'"$awkfield"' }' $tmp/loadpdts$$ | sort -u 
      fi |\

      while read pkgname; do  #{
        if [ ! -f $tmp/${pkgname}.exe -o ! -f $tmp/${pkgname}.inv ] && \
            [ ! -f $pkgs/${pkgname}.exe -o ! -f $pkgs/${pkgname}.inv ]; then
          for need in exe inv att; do  #{
            echo "./install/tmp/${pkgname}.${need}"  >> $ctl_files_xtract
          done  #}
        fi
      done #}

      #If it is a doc only install we make sure that we install 
      #product9000
      #The Algorithm:
      #Get definition of product 9000 from bundles file
      #else get definition of product 9000 from allbundles file
      #If definition cannot be found give a warning message
      #About doc only installs not working
      #Add the install lines for kits of 9000 into the loadpdts file.
      #For the Doc Catalog

      if [ "X5" = "X$sselection"  -a -z "$pkgflag"  ]; then #{
        if [ -s "$bundles" ]; then #{
	  awk '{ if ( $3 == "9000" ) print $2 " " $3 " " $5 " " $6 }' $bundles > /tmp/loaddoc$$
          awk '{ if ( $3 == "9000" ) print }' $bundles >> $selprodfile
        elif [ -s $allbundles ]; then
	  awk '{ if ( $3 == "9000" ) print $2 " " $3 " " $5 " " $6 }' $allbundles > /tmp/loaddoc$$
          awk '{ if ( $3 == "9000" ) print }' $allbundles >> $selprodfile
        else
          msg="${NL}Could not locate definition of product 9000.${NL}Installation of documents only will not work.${NL}Install product 9000.${NL}"
          if $useGUI; then  #{
            $BOURNE $bin_dir/msgDialog.sh "$msg" 
          else
            echo "$msg" 1>&2
          fi  #}
        fi #}

        #If we got the definition of product 9000, we extract
        #The exe inv files
        if [ -s /tmp/loaddoc$$ ]; then #{
          awk '{ print $'"$awkfield"' }' /tmp/loaddoc$$ | sort -u  |\
          while read pkgname; do  #{
            if [ ! -f $tmp/${pkgname}.exe -o ! -f $tmp/${pkgname}.inv ] && \
                [ ! -f $pkgs/${pkgname}.exe -o ! -f $pkgs/${pkgname}.inv ]; then
              for need in exe inv att; do  #{
                echo "./install/tmp/${pkgname}.${need}"  >> $ctl_files_xtract
              done  #}
            fi
          done #}
          cat /tmp/loaddoc$$ >> $tmp/loadpdts$$
          $Rm /tmp/loaddoc$$
        else
          msg="${NL}Could not locate definition of product 9000.${NL}Installation of documents only will not work.${NL}Install product 9000.${NL}"
          if $useGUI; then  #{
            $BOURNE $bin_dir/msgDialog.sh "$msg" 
          else
            echo "$msg" 1>&2
          fi  #}
        fi #}
      fi #}

      if [ -z "$NoConfirm" -o "X$majOption" = "X-u" ]; then # {
        DeviceTAPE=$tapeDev
        DeviceCD=/cdrom
        test -s $recordFile && . $recordFile
        > /tmp/mediab$$; > /tmp/media$$
  
        if [  -z "$firsttime" ]; then  #{
          request=0
          firsttime=set; export firsttime
        else
          request=1
        fi  #}
        Media=`GetMedia $tmp/loadpdts$$ /tmp/mediab$$ /tmp/media$$`

        if [ -s $ctl_files_xtract ]; then  #{
          # if exe in att files need to be extracted include
          # media 1 label
          read source_mediaID source_mediaType source_label < $tmp/allsourceMedia.$InstPlat
          grep "$source_mediaID"  /tmp/media$$ 1>/dev/null 2>&1
          if [ $? -ne 0 ]; then  #{
            if [ "$source_mediaType" = "CDROM" ]; then  #{
              cdlabel="`echo $source_label | tr '~' ' '`"
            elif [ "$source_mediaType" = "TAPE" ]; then
              tapelabel="`echo $source_label | tr '~' ' '`"
            fi #}
            awk '{ 
              if ( $1 ~ /'"$source_mediaType"'/ ) {
              $2=$2+1
              }
              printf("%s\n", $0)
              }' /tmp/mediab$$ > /tmp/newmediab$$
            mv /tmp/newmediab$$ /tmp/mediab$$
          fi #}
        fi  #}
        mediaTypes=`awk '{ print $1 }' /tmp/media$$ |sort -u`

        cdlabel="${cdlabel}`awk '$1 ~ /CDROM/ { print "+" $2 }' /tmp/media$$ | tr '~' ' '`"
        tapelabel="${tapelabel}`awk '$1 ~ /TAPE/ { print "+" $2 }' /tmp/media$$ | tr '~' ' '`"
        cdlabel=`echo \"$cdlabel\" | tr '+' '\12' | sed 's@^"~@@g' | tr -d '"'`
        tapelabel=`echo \"$tapelabel\" | tr '+' '\12'| sed 's@^"~@@g' | tr -d '"'`
        $Rm $prod_list /tmp/media$$ /tmp/mediab$$

        prod_list=/tmp/prods$$
        if [ ! -z "$pkgflag" -a ! -z "$pkgstrs" ]; then #{
          awk '{print $2}' $tmp/loadpdts$$ > $prod_list
        else
          awk '{ printf ( " %-50s (%s,%s)\n", $1, $5, $6)}' $selprodfile | tr '~' ' ' | sort -u > $prod_list
        fi #}
  
        if $useGUI; then  #{
          msg="${NL}The ${object}s mentioned above will be installed or updated.${NL}${NL}The following media is required:${NL}${cdlabel}${NL}${tapelabel}."
          $BOURNE $bin_dir/toplevel.sh 24  $prod_list "$msg" 
          mystatus=$?
          case $mystatus in  #{
            #Exit code changed here to pop up the loading products 
            #menu instead of the installation information menu
            51) ;;
            52) Exit 255 $tmp/loadpdts$$ ;;
            *) Exit 255 $tmp/loadpdts$$
                ;;
            esac  #}
        else   #non-GUI		
          $jsr EchoN "${NL}${NL}####################################################${NL}The following products will be installed or updated.${NL}####################################################${NL}${NL}" > /tmp/show$$
          cat $prod_list >> /tmp/show$$
          $jsr EchoN "${NL}The following media is required: ${NL} ${cdlabel} ${NL} ${tapelabel} ${NL}" >> /tmp/show$$
          cat /tmp/show$$ | $jsr Page
          if $jsr Proceed; then #{
            $Rm /tmp/show$$ 
          else
            Exit 222 /tmp/show$$ $prod_list  
          fi #}
        fi  #}UseGUI if stmt
        $Rm $prod_list 
      fi #}  #if -z NoConfirm
    fi  #} If loadpdts file exists

  


    export DeviceCD DeviceTAPE
    if [ ! -z "$pkgflag" -a ! -z "$pkgstrs" ]; then  #{
      $BOURNE load_pkgs -pkg $tmp/loadpdts$$ $tapedev || Exit $? $tmp/loadpdts$$
    else	
      $BOURNE load_pkgs $tmp/loadpdts$$ $tapedev || Exit $? $tmp/loadpdts$$
    fi  #}
    $Rm $tmp/loadpdts$$ 
    $Rm  $ctl_files_xtract
    ;;
  *)
     echo "$usagestr"
     exit 1
     ;;
esac #}

exit 0
