#!/bin/csh -f
#
#      $Id: spaceok,v 1.2 1994-05-09 16:40:55 haley Exp $
#
#########################################################################
#									#
#			   Copyright (C)  1992				#
#	     University Corporation for Atmospheric Research		#
#			   All Rights Reserved				#
#									#
#########################################################################
#
#	File:		spaceok
#
#	Author:		John Clyne
#			National Center for Atmospheric Research
#			PO 3000, Boulder, Colorado
#
#	Date:		Fri Oct 2 13:56:28 MDT 1992
#
#	Description:	spaceok checks to see if there is enough disk space
#			on the system presumably to install software. 
#			spaceok takes as list of directory-size pairs, 
#			where $directory is the intended installation directory
#			and $size is the amount of space in KBytes needed
#			in $directory. If multiple directory-size pairs
#			point to the same file system spaceok is smart
#			enougth to total the size part of the pairs for
#			that file system. 
#
#			If *all* the specified directory-size
#			pairs will "fit" spaceok exits with zero exit status.
#			If there is not enough space spaceok prints a
#			error message to /dev/tty and exits with exit status
#			of one. spaceok exits with exit status 2 and prints
#			a diagnostic if an error occurs
#
#			Directories named "/dev/null" are skipped.
#			
#
#	Usage:		spaceok [ <directory> <size> ]+
#
#	Environment:
#
#	Files:
#
#
#	Options:

onintr cleanup

if ($#argv < 2) then
	echo "Usage: $0 [ <directory> <size> ]+" > /dev/tty
	exit 2
endif

@ rem = $#argv % 2	# need even number of args
if ($rem == 1) then
	echo "Usage: $0 [ <directory> <size> ]+" > /dev/tty
	exit 2
endif

#
#	this simply allocates enough memory for the wordlists
#
set fs_array = ($argv);
set dirs_array = ($argv);
set fs_size_array = ($argv);
set dir_size_array = ($argv);

@ num = $#argv / 2
@ i = 1
while ($i <= $num)
	set fs_array[$i] = ""
	set dirs_array[$i] = ""
	@ fs_size_array[$i] = 0
	@ dir_size_array[$i] = 0
	@ i++
end

@ j = 1
@ next = 1
while ($#argv) 
	set dir = $argv[1]
	shift
	set dir_size = $argv[1]
	shift

	if ("$dir" == "/dev/null") continue

	if (! -e "$dir") then
		echo "Directory <$dir> does not exist" > /dev/tty
		exit 2
	endif

	set fs = `$LOCALDIR/whichfs $dir`
	if ($status != 0) exit 2

	set  fs_size = `$LOCALDIR/freespace $fs`
	if ($status != 0) exit 2

	#
	# See if the file system for this directory is already in the list
	#
	@ i = 1
	set found = 0
	while ($i <= $num) 
		if ("$fs_array[$i]" == "$fs") then
			set dirs_array[$i] = "$dirs_array[$i] $dir"
			@ dir_size_array[$i] = $dir_size_array[$i] + $dir_size
			set found = 1
			break;
		endif
		@ i++
	end
	if (! $found) then
		set fs_array[$next] = $fs
		set fs_size_array[$next] = $fs_size
		set dirs_array[$next] = $dir
		set dir_size_array[$next] = $dir_size
		@ next = $next + 1
	endif
end

@ i = 1
while ($i < $next)

	echo "" > /dev/tty
	echo -n "	$dir_size_array[$i] KBytes required for " > /dev/tty
	echo "file system $fs_array[$i]." > /dev/tty

	if ($fs_size_array[$i] < $dir_size_array[$i]) then
		cat > /dev/tty <<EOF

Only $fs_size_array[$i] KBbytes available in $fs_array[$i].
Need at least $dir_size_array[$i] KBbytes for directory(s):

	$dirs_array[$i]

EOF
		exit 1
	else
		echo "	There are $fs_size_array[$i] KBytes available"> /dev/tty
		echo "" > /dev/tty
		$LOCALDIR/pause
	endif
	
	@ i++
end

exit 0

cleanup:
exit 1
