#!/bin/sh
#		
#    disk_chk [ -T tag ] [ -N ] infile
#
#       -T : use following tag as the name of object being selected
#       -N : no reselect option
#	This program checks available disk resources against the packages in
#	infile.  If there is enough space, exit 0. Otherwise list available 
#	space and the space required for each package, exit 1.
#
#
#############################################################################
#############################################################################
#
#	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=sun4
OSarchs=$OSvendor
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=''

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

Page() {
    MORE=""  more
}

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

Beep() {
    $jsr EchoN ""	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
}


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


if [ -z "$Proot" ]; then  
    echo "$0 Environment not initialized.
$0 Can only be used when called from SoftLoad" 				1>&2

    exit 1 
fi
export Proot

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

if [ "X$1" = "X-T" ]; then
    shift
    object="$1"; shift
else
    object=package
fi

if [ "X$1" = "X-N" ]; then
   shift
   reselect="no"
   Menu="
        1) Repeat disk checking.
        2) Ignore disk checking and proceed with installation.

        r) Return to Installation Menu.

Type your choice: "
else
   Menu="
        1) Repeat disk checking.
        2) Ignore disk checking and proceed with installation.
        3) Reselect ${object}s.

        r) Return to Installation Menu.

Type your choice: "
fi

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

infile=$1; shift

NL="
"

while : ; do
    dfTbl=/tmp/df$$
    ovrflow=/tmp/ovrflw$$
    echo ${NL}	

    $BOURNE $bin_dir/disk_tbl  $infile $dfTbl
     
    stat=$?
    if [ "X$stat" = "X132" ]; then
    #Disk checks have been postponed 
      exit 0;
    fi
    echo ${NL}	
    awk '$2 < $3' $dfTbl > $ovrflow		#check for enough space
    if [ -s $ovrflow ]; then
      if $useGUI; then
        #$bin_dir/waitMsg&
        #waitMsgPid=$!
        one=`while read fs av req; do
             echo "$fs:$av"
             done < $dfTbl`

        two=`while read fs av req; do
             echo "$fs:$req"
             done < $dfTbl`

        $BOURNE $bin_dir/toplevel.sh 3  "$one" "$two"
        sstatus=$?
        #kill $waitMsgPid >/dev/null
        case $sstatus in
          255) choice=3; exit 222 ;;
           30) choice=2 ;;
           31) choice=1 ;;
            *) exit $sstatus ;;
        esac
      else
  	echo "
SL-13: insufficient disk space to install selections."
	$Rm $ovrflow
        echo ${NL}	
        awk 'BEGIN { 
          printf( "%-50s %8s %11s\n", "mount point", "avail Kb", "required" )
	    }
	  { printf(  "%-50s %8d %11d\n", $1, $2, $3 ) }' $dfTbl
	$jsr EchoN "${NL} ${Menu}"
	choice=	
        while [ "$choice" = "" ] ; do
            read choice
            case $choice in
                1) ;;                   # recheck disk 
                2) echo "${NL}Disk checks ignored.${NL}" 
		   ;;             # skip disk check
		3) test -z "$reselect" && {
		     echo "${NL}Reselect ${object}s.${NL}"
		     exit 255 
                    }
                    $jsr EchoN \
"Type 1 or 2 : " ; choice=
                   ;;
		[rR]*) exit 250;;       # return to Installation Menu
                *) if [ -z "$reselect" ]; then
                     $jsr EchoN \
"Type 1, 2 or 3 : " ; choice=
                   else
                     $jsr EchoN \
"Type 1 or 2 : " ; choice=
                   fi
                   ;;
            esac
        done
      fi
    else
	$jsr EchoN "${NL}Disk checks successful${NL}"	
	$Rm $ovrflow
	break					#success
    fi
    $Rm $dfTbl
    test "$choice" -ne 1 && break
done				

exit 0
