#! /bin/csh -f

#
# determine installation directory and make sure its created and has enough
# space.
#
source scripts/aliases


	E ""
	E "Selecting where to install $product"
	E "---------------------------------------"
	E "$product requires approximately $size KB of free disk space."
	E "Additional space is recommended for logging information."

getdestdir:
	E ""
	E -n "Enter destination directory [$FWDIR]): "
	get dir
	if ("X$dir" == X) set dir = $FWDIR
	set tmp = ($dir) ; if ($#tmp != 1) goto getdestdir

	exists $dir ; if ($status) goto okdir
	if (`ls -1 $dir | wc -l` == 0) goto okdir

	E ""
	test -f $dir
	if ($status == 0) then
		E "$dir already exists and is not a directory"
		set isfile = 1
	else
		E "Destination directory $dir already exists and is not empty"
		set isfile = 0
	endif
	E ""
	E "Do you wish to:"
	E "(1) Enter a new directory name"
	E "(2) Backup $dir as $dir.$now and continue"
	if ($isfile == 0) then
		E -n "(3) Continue with the installation in current $dir"
		if ("X$ans" != "Xu") then
			E " (removing its current content)"
		else
			E ""
		endif
	endif
	E ""
	E -n "Enter your selection (1-3/a) [1]: "
	get dans
	if ("X$dans" == "Xa") goto abort
	if ("X$dans" == "X3") then
		/bin/rm -rf $dir
		mkdir $dir
	endif
	if ("X$dans" == "X2") then
		E ""
		E "Moving $dir to $dir.$now"
		mv $dir $dir.$now
	else if ($isfile || ("X$dans" != "X3")) then
		goto getdestdir
	endif

okdir:
	setenv FWDIR $dir
	set parent = $dir:h
	if ($parent == "") then
		set parent = "/"
	endif

	exists $dir
	if ($status) then
		if (! -d /$parent) then
			E "Parent directory $parent does not exist, Please re-enter"
			goto getdestdir
		endif
	else
		set dir_fs = `df $dir | tail -1 | awk '{print $NF}'`
		set parent_fs = `df $parent | tail -1 | awk '{print $NF}'`
		if ("X$dir_fs" != "X$parent_fs") set parent = $dir
	endif

	E ""
	E "Checking disk space availability..."
	if ($aix) then
		set avail = `df -k $parent | awk '$3 ~ /[0-9][0-9]*/ { print $3 }'`
	else
		set avail = `df $parent | awk '$4 ~ /[0-9][0-9]*/ { print $4 }'`
	endif
	if ($avail < $size) then
		E "$parent has only $avail KB free, $size required by FW"
		E "Please choose another directory"
		goto getdestdir
	endif

havedir:
	E ""
	E "Installing FW under $FWDIR ($avail KB free)"
	E -n "Are you sure (y/n) [y] ? "
	get yn
	if ("X$yn" != Xy && "X$yn" != X) goto getdestdir

	exit 0

abort:
	exit 1
