#!/bin/csh -f
#
#      $Id: Configure,v 1.15 2000/09/13 15:00:28 haley Exp $
#
#########################################################################
#									#
#			   Copyright (C)  1994				#
#	     University Corporation for Atmospheric Research		#
#			   All Rights Reserved				#
#									#
#########################################################################
#
#	File:		Configure		
#
#	Author:		Jeff W. Boote
#			National Center for Atmospheric Research
#			PO 3000, Boulder, Colorado
#
#	Date:		Thu Apr 7 19:56:09 MDT 1994
#
#	Description:	I took pieces of the old configure script to
#			build this one.

onintr cleanup

set cmd = $0
set cmddir = ${cmd:h}
set cmdname = ${cmd:t}
if (($cmddir != $cmdname) && ($cmddir != ".")) then
	echo "Must be at top of NCARG source tree."
	echo "cd to $cmddir and try again."
	goto cleanup
endif

if ((! -d config) || (! -x config/ymkmf)) then
	echo "Where is the config/ymkmf program???"
	echo "It should be at ./config/ymkmf"
	goto cleanup
endif

set ymkmfcmd=./config/ymkmf

if ($#argv > 0) then
	while ($#argv)
		switch($argv[1])
		case "-debug":
			set verbose
			breaksw
		case "-v":
			set verbose_option
			breaksw
		case "-ncar":
			set ncar_option
			breaksw
		default:
			echo "Error: Unknown command-line arguement."
			exit 1
			breaksw
		endsw
		shift
	end
endif

foreach dir ($path)
	if (-x $dir/make) then
		set makecmd=$dir/make
		break
	endif
end

if (! $?makecmd) then
	echo "This Configure Script depends upon the 'make' program."
	echo "It doesn't appear to be available.  Please check your PATH"
	echo "and restart this script."
	goto cleanup
endif

foreach dir ($path)
	if (-x $dir/makedepend) then
		set makedependcmd=$dir/makedepend
		break
	endif
end

if(! $?makedependcmd) set nomakedepend

if (-e ./config/Site.local) then
	echo "	*** WARNING:There is a previous configuration saved."
	echo ""
	echo "Overwrite existing configuration? (n)"
	echo -n "Enter Return(default), y(yes), n(no), or q(quit) > "
	set answer="$<"
	if ("$answer" == "q") goto cleanup
	if ("$answer" == "y") then
		if ($?ncar_option) then
			rm ./config/Site.local
		endif
		goto overwrite
	endif
	goto shortcut
endif

overwrite:

if ($?ncar_option) goto shortcut

onintr extra_cleanup

echo "/* Default Config */" > config/Site.local
echo "#define EmptySite" >> config/Site.local
if ($?nomakedepend) echo "#define NoMakeDepend" >> config/Site.local

echo ""
echo "Building top-level Makefile to determine System defaults"
echo ""
$ymkmfcmd
if ($status) then
	echo "Unable to build Makefile - fix above errors and re-run."
	goto cleanup
endif
echo ""
echo "Makefile built, checking defaults..."
echo ""
echo ""

set bindir=`$makecmd BROOT`
echo -n "..."
set libdir=`$makecmd LROOT`
echo -n "..."
set incdir=`$makecmd IROOT`
echo -n "..."
set mandir=`$makecmd MROOT`
echo -n "..."
set tmpdir=`$makecmd TROOT`
echo -n "..."
set loclib=`$makecmd LSEARCH`
echo -n "..."
set locinc=`$makecmd ISEARCH`
echo -n "..."
set buildxlibstuff=`$makecmd XSTUFF`
echo -n "..."
set buildhdf=`$makecmd HDFSTUFF`
echo -n "..."
set buildf90=`$makecmd F90STUFF`
set f90comp="f90"
echo -n "..."
set ncargversion=`$makecmd VERS`
echo ""
/bin/rm Makefile

onintr cleanup

QuestionAndAnswer:
echo ""
echo "*** Configuration Procedure for NCAR Graphics version $ncargversion ***"
echo ""

if ($?verbose_option) then
  cat <<"EOF"

	This question and answer session will allow you to configure
	NCAR Graphics for installation.  It creates a file "Site.local"
	in the "config" directory that defines Site specific information.
	This procedure will also check that the
	directories specified are accessible and that the required
	system support is available.

"EOF"

  echo -n "Enter Return to continue, or q(quit) > "
  set answer="$<"
  if ("$answer" == "q") goto cleanup

  cat <<"EOF"

	Informational text (like the paragraph you are reading) is
	indented by one tab stop.  Questions are left justified;
	usually, the default answer is in parentheses at the end of
	the question.  At the prompt, you may select the default by
	entering a Return (carriage return) or you may enter any of
	the other valid responses.

"EOF"

  echo -n "Enter Return to continue, or q(quit) > "
  set answer="$<"
  if ("$answer" == "q") goto cleanup

endif

cat <<"EOF"

	As of version 3.2 of NCAR Graphics, every user will need to
	set at least one environment variable in order to use the
	package.  Depending on how you install NCAR Graphics on
	your system, users may also need to set additional environment
	variables.  To keep from having to do this, it is a good
	idea to install NCAR Graphics libraries, binaries, and
	include files under one root directory; for example,
	/usr/local/lib, /usr/local/bin, and /usr/local/include.
	The root directory in this case is /usr/local.

	If you install everything to one root directory, then users
	will only need to setenv NCARG_ROOT to this directory.
	Otherwise, they will need to set the environment variables
	NCARG_BIN, NCARG_LIB, and NCARG_INCLUDE to point to the
	respective directories.

"EOF"

echo -n "Enter Return to continue, or q(quit) > "
set answer="$<"
if ("$answer" == "q") goto cleanup

#
# Binary directory.
#

proc_bin:
if ($?verbose_option) then
  cat <<"EOF"

	The executables must be installed in a directory ending in
	'bin'. It is a good idea to install them to a directory that
	will be on the "search path" of the NCAR Graphics user.
	This procedure will check to make sure that the directory
	specified is writable by the installation process.

"EOF"
endif

echo ""
echo "Executable installation directory : $bindir"
echo -n "Enter Return (default), new directory, or q(quit) > "
set answer="$<"
if ("$answer" == "q") goto cleanup
if ("$answer" != "") then
  set bindir="$answer"
  set altered
endif

set tail="$bindir:t"

if ("$tail" != "bin") then
  echo ""
  echo "  *** Warning: <$bindir> does not end in 'bin'."
  goto proc_bin
endif

if (-d $bindir) then
  if (! -w "$bindir") then
    echo "<$bindir> is not writable."
    echo "New directory name? (y)"
    echo -n "Enter Return(default), y(yes), n(no), or q(quit) > "
    set answer="$<"
    if ("$answer" == "q") goto cleanup
    if ("$answer" == "y" || "$answer" == "") goto proc_bin
  endif
else
  set parent="$bindir:h"
  if (! -w "$parent") then
    echo "  *** Warning: <$parent> is not writable."
    echo "New directory name? (y)"
    echo -n "Enter Return(default), y(yes), n(no), or q(quit) > "
    set answer="$<"
    if ("$answer" == "q") goto cleanup
    if ("$answer" == "y" || "$answer" == "") goto proc_bin
  endif
endif


#
# Library directory.
#

proc_lib:

if ($?verbose_option) then
  cat <<"EOF"

	NCAR Graphics libraries must be installed in a directory
	ending in 'lib'.  It is a good idea, if possible, to install
	them to a directory that is on the default search path for
	the loader.  This procedure will check to make sure that the
	directory specified is writable by the installation process.

"EOF"
endif

echo ""
echo "Library installation directory    : $libdir"
echo -n "Enter Return (default), new directory, or q(quit) > "
set answer="$<"
if ("$answer" == "q") goto cleanup
if ("$answer" != "") then
  set libdir="$answer"
  set altered
endif

set tail="$libdir:t"
if ("$tail" != "lib") then
  echo ""
  echo "  *** Warning: <$libdir> does not end in 'lib'."
  goto proc_lib
endif

if (-d $libdir) then
  if (! -w "$libdir") then
    echo "<$libdir> is not writable"
    echo "New directory name? (y)"
    echo -n "Enter Return(default), y(yes), n(no), or q(quit) > "
    set answer="$<"
    if ("$answer" == "q") goto cleanup
    if ("$answer" == "y" || "$answer" == "") goto proc_lib
  endif
else
  set parent="$libdir:h"
  if (! -w "$parent") then
    echo "  *** Warning: <$parent> is not writable"
    echo "New directory name? (y)"
    echo -n "Enter Return(default), y(yes), n(no), or q(quit) > "
    set answer="$<"
    if ("$answer" == "q") goto cleanup
    if ("$answer" == "y" || "$answer" == "") goto proc_lib
  endif
endif


#
# Include directory.
#

proc_inc:

if ($?verbose_option) then
  cat <<"EOF"

	NCAR Graphics include files must be installed in a directory
	ending in 'include'. This procedure will check to make sure
	that the directory specified is writable by the installation
	process.

"EOF"
endif

echo ""
echo "Include file installation directory    : $incdir"
echo -n "Enter Return (default), new directory, or q(quit) > "
set answer="$<"
if ("$answer" == "q") goto cleanup
if ("$answer" != "") then
  set incdir="$answer"
  set altered
endif

set tail="$incdir:t"
if ("$tail" != "include") then
  echo ""
  echo "  *** Warning: <$incdir> does not end in 'include'."
  goto proc_inc
endif

if (-d $incdir) then
  if (! -w "$incdir") then
    echo "<$incdir> is not writable"
    echo "New directory name? (y)"
    echo -n "Enter Return(default), y(yes), n(no), or q(quit) > "
    set answer="$<"
    if ("$answer" == "q") goto cleanup
    if ("$answer" == "y" || "$answer" == "") goto proc_inc
  endif
else
  set parent="$incdir:h"
  if("$parent" == "") set parent="/"
  if (! -w "$parent") then
    echo "  *** Warning: <$parent> is not writable"
    echo "New directory name? (y)"
    echo -n "Enter Return(default), y(yes), n(no), or q(quit) > "
    set answer="$<"
    if ("$answer" == "q") goto cleanup
    if ("$answer" == "y" || "$answer" == "") goto proc_inc
  endif
endif


#
# Man page directory.
#

proc_man:

if ($?verbose_option) then
  cat <<"EOF"

	NCAR Graphics man pages must be installed in a directory
	ending in 'man'.  The directory should be one that the "man"
	program automatically searches.  This procedure will check
	to make sure that the directory specified is writable by the
	installation process.

"EOF"
endif

echo ""
echo "Man page installation directory   : $mandir"
echo -n "Enter Return (default), new directory, or q(quit) > "
set answer="$<"
if ("$answer" == "q") goto cleanup
if ("$answer" != "") then
  set mandir="$answer"
  set newman
  set altered
endif

set tail="$mandir:t"
if ("$tail" != "man") then
  echo ""
  echo "  *** Warning: <$mandir> does not end in 'man'."
  goto proc_man
endif

if (-d "$mandir") then
  if (! -w "$mandir") then
    echo "<$mandir> is not writable"
    echo "New directory name? (y)"
    echo -n "Enter Return(default), y(yes), n(no), or q(quit) > "
    set answer="$<"
    if ("$answer" == "q") goto cleanup
    if ("$answer" == "y" || "$answer" == "") goto proc_man
  endif
else
  set parent="$mandir:h"
  if (! -w "$parent") then
    echo "  *** Warning: <$mandir:h> is not writable"
    echo "New directory name? (y)"
    echo -n "Enter Return(default), y(yes), n(no), or q(quit) > "
    set answer="$<"
    if ("$answer" == "q") goto cleanup
    if ("$answer" == "y" || "$answer" == "") goto proc_man
  endif
endif

#
# Temp space directory.
#

proc_tmp:

if ($?verbose_option) then
  cat <<"EOF"

	A couple of the translators make use of temporary
	file space.  On most systems, the directory "/tmp" is the
	logical choice.  You may specify a different directory to
	use for this purpose.  The directory must be writable by
	NCAR Grahpics users.  This procedure will check only to
	make sure that the directory specified is writable by the
	installation process.

"EOF"
endif

echo ""
echo "System temp space directory   : $tmpdir"
echo -n "Enter Return (default), new directory, or q(quit) > "
set answer="$<"
if ("$answer" == "q") goto cleanup
if ("$answer" != "") then
  set tmpdir="$answer"
  set newtmp
  set altered
endif

if (-d "$tmpdir") then
  if (! -w "$tmpdir") then
    echo "<$tmpdir> is not writable"
    echo "New directory name? (y)"
    echo -n "Enter Return(default), y(yes), n(no), or q(quit) > "
    set answer="$<"
    if ("$answer" == "q") goto cleanup
    if ("$answer" == "y" || "$answer" == "") goto proc_tmp
  endif
else
  if ("$tmpdir:h" == "") then
    set parent="/"
  else
    set parent="$tmpdir:h"
  endif
  if (! -w "$parent") then
    echo "  *** Warning: <$tmpdir:h> is not writable"
    echo "New directory name? (y)"
    echo -n "Enter Return(default), y(yes), n(no), or q(quit) > "
    set answer="$<"
    if ("$answer" == "q") goto cleanup
    if ("$answer" == "y" || "$answer" == "") goto proc_tmp
  endif
endif

#
# F90 stuff
#
if ($?verbose_option) then
  cat <<"EOF"

	The default Fortran compiler used to build NCAR Graphics is 
	Fortran 77.  You may specify that you want to use Fortran 90
	instead. If you don't want to use Fortran 90, answer "n" to 
	the next question."

"EOF"
endif

echo ""
echo "Use Fortran 90 to build Fortran libraries? ($buildf90)"
echo -n "Enter Return (default), y(yes), n(no), or q(quit) > "
set answer="$<"
if ("$answer" == "q") goto cleanup
if (("$answer" != "") && ("$answer" != "$buildf90")) then
	set buildf90 = $answer
	set newbuildf90
	set altered
endif

if ("$buildf90" == "y") then
  echo ""
  echo "What is the name of your Fortran 90 compiler? ($f90comp)"
  echo -n "Enter Return (default), name of Fortran 90 compiler, or q(quit) > "
  set answer="$<"
  if ("$answer" == "q") goto cleanup
  if (("$answer" != "") && ("$answer" != "$buildf90")) then
	set f90comp = $answer
	set newf90comp
	set altered
  endif
endif

#
# NCAR View stuff.
#

if ($?verbose_option) then
  cat <<"EOF"

	The next few questions concern some of the applications in
	NCAR Graphics that allow the user to manipulate and view
	NCAR Graphics Computer Graphics	Metafiles (NCGM's).  For
	example, "ctrans" is an NCGM translator that can support the
	X Window System, PostScript, and many other graphics
	devices, and "idt" is an X window interactive image display
	tool which allows the user to do animation among other
	things.  There are also several raster applications which
	allow you to convert from one raster format to another,
	split raster files, and view raster files.

	In order to build pieces of these applications, you need the
	basic X11 libraries.  If you don't have these libraries,
	answer "n" to the next question.

"EOF"
endif

#
# X stuff - ctrans/ictrans
#
echo ""
echo "Build X support into ctrans/ictrans? ($buildxlibstuff)"
echo -n "Enter Return (default), y(yes), n(no), or q(quit) > "
set answer="$<"
if ("$answer" == "q") goto cleanup
if ("$answer" != "") then
  if ("$answer" != "$buildxlibstuff") then
     set buildxlibstuff = $answer
     set newbuildxlibstuff
     set altered
  endif
endif

#
# HDF
#
if ($?verbose_option) then
  cat <<"EOF"

	The raster library supports several different raster
	formats, some which may require external non-NCAR Graphics
	libraries in order to compile.  If you don't want support
	for a particular raster format or else you don't have the
	required external library, answer "n" to the following
	question(s)."

"EOF"
endif

echo ""
echo "Build HDF support into raster library? ($buildhdf)"
echo "(requires the external library 'libdf.a') "
echo -n "Enter Return (default), y(yes), n(no), or q(quit) > "
set answer="$<"
if ("$answer" == "q") goto cleanup
if (("$answer" != "") && ("$answer" != "$buildhdf")) then
	set buildhdf = $answer
	set newbuildhdf
        set altered
endif

#
#  Library Search Path
#

if ($?verbose_option) then
  cat <<"EOF"

	Please enter the list of local libraries you wish to have
	on your library search path.  If building X applications,
	include the pathname of your X11 libraries.  Enter each
	library pathname with a space between.

"EOF"
endif

set loclibdir = `echo "$loclib" | sed -e 's/-L//g'`

proc_loclibdir:
echo ""
echo "Enter local library search path(s) : $loclibdir"
echo -n "Enter Return (default), new directories, or q(quit) > "
set answer="$<"

if ("$answer" == "q") goto cleanup
if ("$answer" != "") then
  set loclibdir = "$answer"
  set nloclib
  set altered
endif
if ("$loclibdir" != "") then
  set libs = ($loclibdir:x)
  set newloclib=""
  while ($#libs > 0)
    if (! -d $libs[1]) then
      echo "  *** Warning: <$libs[1]> does not exist"
      echo ""
      goto proc_loclibdir
    else 
      set newloclib = ($newloclib "-L$libs[1]")
    endif
    shift libs
  end
  set loclib = "$newloclib"
endif

#
# X11 include files.
#

if ($?verbose_option) then
  cat <<"EOF"

	Please enter the list of local include paths you wish to
	have on your include search path.  If building X
	applications, include the pathname to your X11 include
	files.  Enter each include pathname with a space between.

"EOF"
endif



set locincdir = `echo "$locinc" | sed -e 's/-I//g'`

proc_locincdir:
echo ""
echo "Enter local include search path(s) : $locincdir"
echo -n "Enter Return (default), new directories, or q(quit) > "
set answer="$<"
if ("$answer" == "q") goto cleanup
if ("$answer" != "") then
  set locincdir = "$answer"
  set nlocinc
  set altered
endif
if ("$locincdir" != "") then
  set incs = ($locincdir:x)
  set newlocinc=""
  while ($#incs > 0)
    if (! -d $incs[1]) then
      echo "  *** Warning: <$incs[1]> does not exist"
      echo ""
      goto proc_locincdir
    else 
      set newlocinc = ($newlocinc "-I$incs[1]")
    endif
    shift incs
  end
  set locinc = "$newlocinc"
endif

#
# Summarize configuration
#
if ($libdir:h != $bindir:h || $libdir:h != $incdir:h || $bindir:h != $incdir:h ) then
  cat <<EOF

	Since you did not specify to have the libraries, binaries, and
	include files installed under one root directory, every user of
	NCAR Graphics will need to set the following environment
	variables:

        setenv NCARG_LIB $libdir
        setenv NCARG_BIN $bindir
        setenv NCARG_INCLUDE $incdir

EOF
	
else
    set commonroot = ($libdir:h)
    cat <<EOF

	Since you specified to have the libraries, binaries, and
	include files installed under one root directory, every user
	of NCAR Graphics will only need to set the following
	environment variable:

        setenv NCARG_ROOT $commonroot

EOF
	
endif

echo ""
echo "Go back and make more changes or review? (n) "
echo -n "Enter Return(default), y(yes), n(no), or q(quit) > "
set answer="$<"
if ("$answer" == "q") goto cleanup
if ("$answer" == "y") goto QuestionAndAnswer

#
# Save the configuration
#

proc_save:
echo ""
echo "Save current configuration ? (y) "
echo -n "Enter Return(default), y(yes), or q(quit) > "
set answer="$<"
if ("$answer" == "q") goto cleanup

if ("$answer" != "y" && "$answer" != "") then
	echo "Invalid response"
	goto proc_save
endif

if (! $?altered) goto shortcut

cd config

echo "/*" > Site.local
echo " *	This file was created by the Configure script." >> Site.local
echo " */" >> Site.local
echo "" >> Site.local
echo "#ifdef FirstSite" >> Site.local
echo "" >> Site.local
echo "#endif /* FirstSite */" >> Site.local
echo "" >> Site.local
echo "" >> Site.local
echo "#ifdef SecondSite" >> Site.local
echo "" >> Site.local

if ($?commonroot) then
	echo "#define YmakeRoot $commonroot" >> Site.local
else
	echo "#define BinRoot $bindir" >> Site.local
	echo "#define IncRoot $incdir" >> Site.local
	echo "#define LibRoot $libdir" >> Site.local
endif

if ($?newman) then
	echo "#define ManRoot $mandir" >> Site.local
endif
if ($?newtmp) then
	echo "#define TmpRoot $tmpdir" >> Site.local
endif

echo "" >> Site.local

if ($?nloclib) then
	echo "#define LibSearch $loclib" >> Site.local
endif
if ($?nlocinc) then
	echo "#define IncSearch $locinc" >> Site.local
endif

echo "" >> Site.local

if ($?newbuildf90) then
	if ("$buildf90" == "y") then
		echo "#define BuildWithF90 TRUE" >> Site.local
		echo "#define FCompiler $f90comp" >> Site.local
	else
		echo "#define BuildWithF90 FALSE" >> Site.local
	endif
endif

echo "" >> Site.local

if ($?newbuildxlibstuff) then
	if ("$buildxlibstuff" == "y") then
		echo "#define BuildXlibDependentCode TRUE" >> Site.local
	else
		echo "#define BuildXlibDependentCode FALSE" >> Site.local
	endif
endif

if ($?newbuildhdf) then
	if ("$buildhdf" == "y") then
		echo "#define BuildRasterHDF TRUE" >> Site.local
	else
		echo "#define BuildRasterHDF FALSE" >> Site.local
		echo "#define HDFlib" >> Site.local

	endif
endif
echo "" >> Site.local
if ($?nomakedepend) echo "#define NoMakeDepend" >> Site.local
echo "" >> Site.local
echo "#endif /* SecondSite */" >> Site.local

cd ..

shortcut:
if ($?ncar_option) then
	set conf_type = "ncar local"
else
	set conf_type = "current"
endif
echo ""
echo "Building Top-level Makefile with $conf_type configuration options"
echo ""
$ymkmfcmd
if ($status) then
	echo "Unable to build Makefile - fix above errors and re-run."
	goto cleanup
endif

#
# Installation step
#

proc_install:
cat <<"EOF"

	You have completed the configuration process. Once you exit this
	program you may initiate the installation procedure by typing:

		make Everything >&make-output &

"EOF"

if ($?verbose_option) then
  cat <<"EOF"
	If this is not your first installation attempt, and you simply 
	made some changes in the configuration, restart the process using:

		make All >&make-output &
	
	This command does not erase object code that has already
	been compiled.

	Both of these commands run the installation procedure
	in the background, and you can track its progress
	by periodically examining the file "make-output" while 
	the installation proceeds.

"EOF"
endif

exit 0

extra_cleanup:
rm -f Makefile
rm -f config/Site.local

cleanup:
echo ""
echo "Terminating configuration procedure"
exit 0
