#! /bin/sh
#-----------------------------------------------------------------------------
# filename: 
# 	INSTALL-Toolkit
# 
# description:
# 	Install script for the Toolkit
# 
# usage:
# 	cd <SDP-home-dir> 
# 	bin/INSTALL-Toolkit [-dbug] [-f90 [-NAG]] [-fc_path <path>] [-no_ftn]
#                           [-cc_path <path>] [-sgi32 | -sgi64] [-daac]
#                           [-no_hdf] [-no_hdfeos]
# 	                    [-css_inc1 <EcUtLoggerC.h-path>]
# 	                    [-css_inc2 <EcTypes.h-path>]
# 	                    [-css_lib <libEcUtLogC.a-path>]
# 	                    [-lib_only]
#			    [-cpp] [cpp_path <path>] [-cpp_lib_only]
# 	bin/INSTALL-Toolkit [-h] 
# 
#         (where <SDP-home-dir> is the full path of the toolkit home directory)
#
# 	options:
# 	  -dbug     : build debug version of toolkit
# 	  -f90      : build f90 version of the toolkit
# 	              optional flag -NAG specifies NAG f90 (see notes 1 & 4)
# 	  -fc_path  : set the path of the FORTRAN compiler to <path>
# 	  -no_ftn   : build toolkit without full FORTRAN support
# 	  -cc_path  : set the path of the C compiler to <path>
# 	  -sgi32    : build in -n32 mode (SGI Power Challenge only)
# 	  -sgi64    : build in -64  mode (SGI Power Challenge only)
# 	  -daac     : build the DAAC version of the toolkit library
# 	  -no_hdf   : build toolkit without HDF support
# 	  -no_hdfeos: build toolkit without HDF-EOS support
# 	  -css_inc1 : specify path for EcUtLoggerC.h (DAAC toolkit only)
# 	  -css_inc2 : specify path for EcTypes.h (IR-1 DAAC toolkit only)
# 	  -css_lib  : specify path for libEcUtLogC.a (IR-1 DAAC toolkit only)
# 	  -lib_only : just build the library, don't do anything else
# 	  -pgshome : full path of Toolkit top level directory
# 	             (e.g. /usr/local/TOOLKIT_MTD)
# 	             NOTE: this does NOT allow you to install the Toolkit in a
# 	             directory other than the one created from the Toolkit tar
# 	             file.
# 	  -h        : display this help message and exit
#         -cpp     : build C++ Version-only add if C++ version is wanted
#         -cpp_path: set the path of the C++ compiler to <path> 
# 	  -cpp_lib_only : just build the library, don't do anything else
# 
# notes:
# 	1)  The -fc_path and -cc_path options tell the script where to find
# 	    your compilers, in the event that they are in non-standard
# 	    locations.  If the -NAG flag is specified, you should use the
# 	    -fc_path option to specify the path where the NAG f90 compiler 
# 	    is located.
# 
# 	2)  Unless the -no_hdf flag is specified, the environment variable
# 	    HDFHOME must be set to point to the HDF home directory.  This is 
# 	    handled automatically when this script is called from INSTALL.
#
# 	3)  Unless the -no_hdfeos flag is specified, the environment variable
# 	    HDFEOS_HOME must be set to point to the HDF-EOS home directory. This
# 	    is handled automatically when this script is called from INSTALL.
#
# 	4)  On the SGI Challenge, the -NAG flag may only be specified for old-style 
# 	    32-bit mode installation.
#
# 	    Under IRIX 6.1 and earlier, the script will automatically set up to use
# 	    the NAG f90 compiler regardless of whether or not -NAG was specified.
# 	    Under IRIX 6.2 and later, the script will set up to use the SGI f90 compiler
# 	    unless -NAG was specified. 
# 	    
# 	    Except as noted above, the script will automatically set the f90 compiler 
# 	    on the SGI Challenge as follows:
# 	    
# 	    	library type    	f90 type 	option
# 		================	======== 	=========	    
# 	        old-style 32-bit	NAG f90		(default)
# 	        new-style 32-bit	SGI f90		-sgi32
# 	        64-bit        		SGI f90		-sgi64
# 
#  	    On the other SGI (pre-6.2) IRIX and SunOS 4.x platforms, the script 
# 	    will select the NAG compiler, as that is the only one currently supported.
#
# author:
# 	Mike Sucher / Applied Research Corp.
# 	Guru Tej S. Khalsa / Applied Research Corp.
# 
# history:
# 	20-Apr-1994 MES Initial version (INSTALL)
# 	26-Apr-1994 MES Source code frozen for PGS Toolkit delivery #2
# 	04-May-1994 MES Fixes to the code which generates files:
# 	                pgs-env.csh and pgs-env.ksh
# 	20-Sep-1994 MES Release 3 version, first cut
# 	22-Sep-1994 MES Added NAG f90 support
# 			Changed CBP database setup to use packed file (.z)
# 			instead of compressed file (.Z), because the Cray 
# 			doesn't have the compress/uncompress utilities.
# 	04-Oct-1994 MES Added code to set up PGS_TYPES.h for the Cray
# 	07-Oct-1994 MES Added code to set up DCW/vpftable.h for the DEC
# 	07-Oct-1994 MES Modified section that does the actual build to report
# 			failed compilations for each tool group.
# 	24-Oct-1994 MES Added code to build the orbit simulator
# 	14-Feb-1995 MES Updated for Toolkit Release 4:
# 			- added code to build the Level 0 file simulator
# 			- added code to build the PC command utilities
# 			- CBP database setup to no longer uses packed file
# 	19-May-1995 MES Renamed to INSTALL-Toolkit for Toolkit Release 5
# 			It is now called from a meta-installation script
# 			that also handles HDF installation options.
# 	19-May-1995 MES Revised logic for Toolkit Release 5:
# 			- revised option handling logic
# 			- revised platform swtiching logic using switch/case
# 			- added -no_hdf option, revised HDF variable logic
# 			- updated PGS environment variable logic
# 			- added -h (help) option, updated usage message
# 			- added check for HDF before building MET tools
# 			- added preliminary code for -gcc option
#	27-Jun-1995 MES/GTSK 
# 			Added support for IRIX64, the sgi 64 bit O/S.
#			This script will install the toolkit in 32 bit mode.
#	30-Jun-1995 MES/GTSK 
#  			Removed "-h stdc" from CFLAGS for Cray. 
#	10-Jul-1995 MES Added code to build the CSC and TD utilities.
#	20-Jul-1995 MES Added check for HDF before building AA tools.
#	19-Sep-1995 MES Auto-configure for formal track directory structure.
# 			Check for old library and remove it before building.
# 			Attempt to build the library-dependent utilities,
# 			even if there were errors building the library.
# 			Set the  flag in pgs-dev-env.*, if needed.
#	20-Sep-1995 MES Configure formal track distribution for architecture.
# 	31-Oct-1995 MES Updates for IR-1 release:
# 			 - add support for SGI compiled -n32 ($BRAND = sgi32)
# 			 - sgi mode now set by sgi_mode in env scripts
# 			 - dir structure now set by pgs_formal in env scripts
# 			 - revise PGSRUN: no longer architecture-specific
#  			 - set PGS_PC_INFO_FILE to $PGSRUN/$BRAND/PCF.ir1
# 	01-Nov-1995 MES Rework f90 option handling, add -daac option
# 	02-Nov-1995 MES Enhance DAAC version logic
# 			Add -css_inc1, -css_inc2 and  -css_lib options.
# 			Add -fc_path and -cc_path options.
# 	06-Nov-1995 GSK Added sanity check to make sure the CSS logger
# 			related files will be found if user specifies DAAC
#			version of Toolkit (-daac).  Fixed minor typo:
# 			libEcUtLoggerC.a => libEcUtLogC.a (several places).
# 	08-Nov-1995 MES Modify directory setup to copy from ./common rather
# 			than move, allowing multi-platform support at a single
# 			installation.   This script must be run once for each
# 			architecture to be supported.  Also, make symbolic 
# 			links for de200.eos, which was moved to avoid the 
# 			creation of multiple copies.
# 	14-Nov-1995 MES Bug fixes and enhancements:
# 			- fix bug that broke the -fc_path and -cc_path options
# 			- add the -lib_only option to only rebuild the library
# 			- add checks to avoid redoing $PGSDAT setup
# 			- add checks for proper type of f90 compiler
# 			- revise object cleanup to not rely on $PGSSRC/makefile
# 			- move tool groups compilation error message back to
# 			  installation exit section
# 	22-Nov-1995 MES Enhancements for f90 and DAAC support:
# 			- set env variable F77 to include f90 compiler path 
# 			  to avoid potential problems in systems with multiple 
# 			  f90 compilers
# 			- updated sections that initialize pgs-dev-env.[ck]sh 
# 			  to set the pgs_f90_comp and pgs_nag_flag variables 
# 			  that are used to implement f90 handling
# 			- updated sections that initialize pgs-dev-env.[ck]sh 
# 			  to set the pgs_daac, css_inc1, css_inc1, and css_lib 
# 			  variables that are used to implement f90 handling
# 	04-Apr-1996 MES Update setup of pgs-dev-env files to unset all
# 			conditional variables before doing anything else.
# 			This was done to avoid accidentally inheriting
# 			from any flags previously set.
# 	15-Apr-1996 MES Updates for Release A:
#  			- set PGS_PC_INFO_FILE to $PGSRUN/$BRAND/PCF.relA
# 	24-Apr-1996 MES Fixed bugs that affected the handling of the
# 			-cc_path, -fc_path, -f90 and -NAG options.
# 	11-Jun-1996 MES Disabled IR1 DAAC sections.
# 	24-Jul-1996 MES Updated f90 checking to handle IRIX 6.2 compiler.
# 	25-Jul-1996 MES Output warning message and exit if -sgi32 or -sgi64
# 			flag is used on 32-bit SGI platform.
# 	06-Aug-1996 MES Improve handling of the -NAG flag.
# 	26-Aug-1996 MES Update DAAC toolkit installation logic.
# 	27-Aug-1996 MES Update HDF setup logic.
# 	23-Sep-1996 MES Explicitly clear obsolete IR-1 DAAC variables.
# 	25-Sep-1996 MES More updates to HDF setup logic.
# 	22-Oct-1996 MES Allow 6.2 SGI and SGI Challenge to use native f90 in
# 			old-style 32-bit mode.
#       24-Jun-1998 AT  for sgi PATH is set first to /usr/bin so that 
#                       /usr/bin/ar is used, instaed of /usr/local/bin/ar,
#                       in library creation.  
#       12-Sep-1998 Subing change $PWD to $cwd because hp platform 
#                   not working.
# 	23-Jan-2000 AT  Updated for C++ versions of the library
#       15-Nov-2005 TR  Added CYGWIN capability. For cygwin, $status was
#                       changed to $? which is valid on all platforms
#
#-----------------------------------------------------------------------------
Help()
{
    echo ""
    if [ "$BRAND" = "winnt98" ] ; then
    sed -n '/filename:/,/author:/p' $0 | grep -v "author:" | cut -c3-300 | less
    else
    sed -n '/filename:/,/author:/p' $0 | grep -v "author:" | cut -c3-300 | more
    fi
    #this line must be here because it contains the string: "author:"
    exit
}

#
# Save starting directory, path
# Create error message header
#

#set start_dir=($PWD)
start_dir=$cwd
start_path=$path
script_error="`basename $0`: Error:"

#
# set default values for CSS file paths
#
css_inc1="/ecs/formal/CSS/IR1/include"
css_inc2="/ecs/formal/COMMON/include"
css_lib=""

#
# set default values for error tracking variables
#

bad_tools=""
bad_utils=""

#
# Get arguments from command line
#

help=0
use_gcc=0
no_hdf=0
no_hdfeos=0
sgi_mode=32
pgs_daac=0
f90_flag=0
fc_path=""
cc_path=""
nag_flag=0
lib_only=0
opt_flag=-O
build_ftn=1
#unsetenv PGSHOME
cpp_path=""
cpp=0
c_only=1
cpp_lib_only=0

while [ ! "$1" = "" ] ; do

    case "$1" in

      -h )
        help=1
        ;;

      -f90 )
        f90_flag = 1
        if [ "$2" = "" ] ; then
		echo ""
	else
            dash1=`echo $2 | cut -c1`
	    case "$dash1" in
              - )
                ;;
              * )
                 fc_path="$2"; shift 
                ;;
            esac
        fi
        ;;

      -NAG )
        nag_flag=1
        ;;

      -gcc )
        use_gcc=1
        ;;

      -no_hdf )
        no_hdf=1
        ;;

      -no_hdfeos )
        no_hdfeos=1
        ;;

      -sgi32 )
        sgi_mode=n32
        ;;

      -sgi64 )
        sgi_mode=64
        ;;

      -daac )
        if [ "$pgs_daac" = "IR1" ] ; then
          echo "-ir1 and -daac are mutually exclusive options,"
          echo "-ir1 builds the IR1 DAAC version of the Toolkit"
          exit
        fi
        pgs_daac=1
        ;;

      -ir1 )
        if [ "$pgs_daac" = "1" ] ; then
          echo "-ir1 and -daac are mutually exclusive options,"
          echo "-ir1 builds the IR1 DAAC version of the Toolkit"
          exit
        fi
        pgs_daac="IR1"  # IR1 DAAC support
        ;;

      -css_inc1 )
        css_inc1="$2"
        shift
        ;;

      -css_inc2 )
        css_inc2="$2"
        shift
        ;;

      -css_lib )
        css_lib="$2"
        shift
        ;;

      -fc_path )
        fc_path="$2"
        shift
        ;;
 
      -cc_path )
        cc_path="$2"
        shift
        ;;

      -lib_only )
        lib_only=1
        ;;

      -dbug )
        opt_flag=-g
        ;;

      -no_ftn )
        build_ftn=0
        ;;

      -pgshome )
        PGSHOME="$2"
        shift
	if [ ! -d $PGSHOME -o ! -w $PGSHOME ] ; then
	    echo "`basename $0`: error: invalid directory ($PGSHOME) specified with -pgshome switch"
	    help=1
	fi
	export PGSHOME
        ;;

      -cpp )
        cpp=1
	c_only=0
        ;;

      -cpp_path )
        cpp_path="$2"
        shift
        ;;

      -cpp_lib_only )
        cpp_lib_only=1
	c_only=0
        ;;

      * )
        echo "`basename $0`: error: invalid argument '$1'"
        help=1
        ;;

    esac

    shift

done

#help:
#if [ "$help" = "1" ] ; then
#    echo ""
#    sed -n '/filename:/,/author:/p' $0 | grep -v "author:" | cut -c3-300 | less
#    #this line must be here because it contains the string: "author:"
#    exit
#fi


#
# set the toolkit home directory environment variable to the startup directory
# *** this script must be run from the toolkit home directory ***
#

if [ ! $?PGSHOME ] ; then
    PGSHOME=$cwd
    #setenv PGSHOME $PWD
else
    cd $PGSHOME
fi

#
# make sure we are starting from a valid toolkit directory,
# by checking for the presence of all required subdirectories
# if any of them is missing then set PGSHOME to the null string
# and display the help message
# 

for dir in bin include lib runtime src test; do

    if [ -d $dir ] ; then
        continue
    else
        PGSHOME=""
        echo "$script_error not in toolkit home directory"
        help=1
        Help
    fi
export PGSHOME
done

#
# determine whether formal track directory structure is in use
#

pgs_formal=1
for dir in database obj objcpp; do

    if [ -d $dir ] ; then
        continue
    else
        pgs_formal=0
    fi

done


#
# If the -NAG option was specified:
# - enable the -f90 option
# - make sure that the -fc_path <compiler-path> was also specified
#

if [ $nag_flag = 1 ] ; then

    f90_flag=1
    if [ $fc_path = "" ] ; then
        echo "$script_error Must specify -f90 and -fc_path <compiler-path> with -NAG option."
          exit 1
    else 
        echo ""
        echo "NAG option enabled, using f90 compiler (in) $fc_path"
    fi

fi

#-----------------------------------------------------------------------------
#
#  Set up environment
#
#-----------------------------------------------------------------------------


# set path to a base subset of directories, allowing startup on unknown host
# note: once the host has been determined the path is appropriately customized

path=/usr/bin:/usr/local/bin:/bin:/etc:/usr/etc:/usr/ucb:/usr/bin/X11

# get operating system type, login name
# special cases: SCO and Cray  - uname works differently,

MACHINE=`uname -m | awk '{print $1}'`	# needed on Cray & SCO

case "$MACHINE" in

    i386 )        			# SCO box
	OSTYPE=sco386
        ;;

    i686 )        			# SCO box
	OSTYPE=sco686
        ;;

    CRAY )    				# CRAY
	OSTYPE=UNICOS
        ;;

    * )					# everybody else
	OSTYPE=`uname`
        ;;

esac

user=` id | awk -F\( '{print $2}' | awk -F\) '{print $1}' `
if [ $?LOGNAME = 0 ] ; then
 LOGNAME=$user	# make sure $LOGNAME is defined
fi
USER=$LOGNAME				# make sure $USER is defined

# set machine-dependent environment variables:
# 	HOST  the host name of this machine
# 	BRAND used by other achitecture-specific code
# 	path  the execution search path exported to PATH

case "$OSTYPE" in

    AIX ) 
	path=/usr/local/bin:/bin:usr/bin:etc:usr/etc:usr/ucb:usr/bin/X11:usr/ccs/bin:usr/sbin
	HOST=`hostname`
	BRAND=ibm
        ;;

    HP-UX ) 
	path=/usr/local/bin:bin:usr/bin:etc:usr/etc:usr/bin/X11
	HOST=`hostname`
	BRAND=hp 
        ;;

    IRIX )  
	path=/usr/bin:usr/local/bin:bin:etc:usr/etc:usr/bsd:usr/bin/X11:usr/sbin
	HOST=`hostname`
	BRAND=sgi
	if [ "$sgi_mode" = 32] ; then
	    echo ""
	else
	    echo "$script_error -sgi32 and -sgi64 options not supported on 32-bit SGI platform"
	    exit 1
	fi
        ;;

    IRIX64 )  
	path=/usr/bin:usr/local/bin:bin:etc:usr/etc:usr/bsd:usr/bin/X11:usr/sbin
	HOST=`hostname`
	if [ "$sgi_mode" = 32 ] ; then
	    BRAND=sgi
		fi
        if [ "$sgi_mode" = 64 ] ; then
	    BRAND=sgi64
		fi
        if [ "$sgi_mode" = n32 ] ; then
	    BRAND=sgi32
        else
	    BRAND=sgi
        fi
        ;;

    Linux ) 
	path=/usr/local/bin:bin:usr/bin:etc:usr/etc:usr/bin/X11
	HOST=`hostname -s`
	BRAND=linux
        ;;

    OSF1 )  
	path=/usr/local/bin:bin:usr/bin:etc:usr/etc:usr/ucb:usr/bin/X11:usr/ccs/bin:usr/sbin
	HOST=`hostname -s`
	BRAND=dec 
        ;;

    sco386 ) 
        if [ `uname` = "Linux" ] ; then
           path=/usr/local/bin:/bin:/usr/bin:/etc:/usr/etc:/usr/X11/bin
           HOST=`hostname -s`
           BRAND=linux
        else
           path=/usr/local/bin:/bin:/usr/bin:/etc:/usr/etc:/usr/bin/X11
           HOST=`hostname -s`
           BRAND=sco
        fi
        ;;

    sco686 ) 
        if [ `uname` = "Linux" ] ; then
           path=/usr/local/bin:/bin:/usr/bin:/etc:/usr/etc:/usr/X11/bin
           HOST=`hostname`
           BRAND=linux
        else
           if [ `uname|cut -d_ -f1` = "CYGWIN" ]; then
              path=/usr/local/bin:/bin:/usr/bin:/etc:/usr/etc:/usr/bin/X11
              HOST=`hostname`
              BRAND=winnt98
           fi
        fi
        ;;

    SunOS ) 
	# distinguish between SunOS 5.x and 4.x versions
	if [ `uname -r | awk -F. '{print $1}'` = "5" ] ; then 
	    BRAND=sun5			# release V5.x SunOS
	    path=/usr/local/bin:opt/SUNWspro/bin:bin:usr/bin:etc:usr/etc:usr/ucb:usr/openwin/bin:usr/openwin/demo:usr/ccs/bin:usr/sbin
	else                                
	    BRAND=sun4			# release V4.x SunOS
	    path=/usr/local/bin:usr/local/lang:usr/lang:bin:usr/bin:etc:usr/etc:usr/ucb:usr/openwin/bin:usr/openwin/demo
	fi
	HOST=`hostname`
        ;;

    UNICOS ) 
	path=/usr/local/bin:bin:usr/bin:etc:usr/ucb:usr/bin/X11
	HOST=`hostname`
	BRAND=cray 
        ;;

    * )
	echo "Operating system: $OSTYPE not supported"
	echo "This release of the toolkit supports: "
	echo "   Sun, SGI, HP-9000, IBM-6000, DEC-Alpha and Cray/Unicos "
	exit 1
        ;;

esac
export HOST BRAND 

# fix up css_lib following definition of BRAND

if [ "$css_lib" = "" ] ; then
 css_lib="/ecs/formal/CSS/IR1/lib/$BRAND"
fi

#
# set default values for the toolkit directory environment variables
#

PGSBIN=$PGSHOME/bin		# exectuable files
PGSDAT=$PGSHOME/database	# database files
PGSINC=$PGSHOME/include	# include (header) files
PGSLIB=$PGSHOME/lib 		# library files
PGSMSG=$PGSHOME/message	# SMF message files
PGSOBJ=$PGSHOME/lib/obj	# object files
PGSCPPO=$PGSHOME/lib/objcpp	# C++ object files
PGSRUN=$PGSHOME/runtime	# runtime work files
PGSSRC=$PGSHOME/src		# toolkit source files
PGSTST=$PGSHOME/test		# test source files

#
# set architecture dependencies for formal track directory structure
#

if [ "$pgs_formal" = 1 ] ; then

    PGSBIN=${PGSHOME}/bin/$BRAND		# exectuable files
    PGSDAT=${PGSHOME}/database/$BRAND	# database files
    PGSLIB=${PGSHOME}/lib/$BRAND  		# library files
    PGSOBJ=${PGSHOME}/obj/$BRAND		# object files
    PGSCPPO=${PGSHOME}/objcpp/$BRAND        # C++ object files
fi
export PGSBIN PGSDAT PGSINC PGSLIB PGSMSG PGSOBJ PGSCPPO PGSRUN PGSSRC PGSTST
# 
# Set up variables needed by Process Control (PC) tools. 
# 
pcf_ext=relB0 # extension of current PCF

#setenv PGS_PC_INFO_FILE   #$PGSRUN/$BRAND/PCF.$pcf_ext # default info file

#
# add PGSBIN to path
#
path=$PGSBIN":"$path


# set machine-dependent compilers and compilation switches:
#
#

case "$BRAND" in

    cray )
         CPP=CC                   # C++ compiler
	 CC=cc 			# C compiler
	 CFLAGS="$opt_flag" 	# default C flags (optimize, ansi)
	 C_CFH="-DCRAYFortran"    # C/cfortran.h called from FORTRAN
	 CFHFLAGS="$CFLAGS $C_CFH" # CFLAGS + C_CFH
         CPPFLAGS="$opt_flag"            # default C ++flags (optimize, ansi)
         CPP_CFH="-DCRAYFortran"  # C++ w/cfortran.h called from FORTRAN
         CPPFHFLAGS="$CPPFLAGS $CPP_CFH" # CFLAGS + C_CFH for C++
	 F77=cf77			# FORTRAN compiler
	 F77FLAGS="$opt_flag"	# common FORTRAN flags
	 HDFSYS=UNICOS		# system type as defined by HDF
	;;

    dec )
         CPP=CC                   # C++ compiler
	 CC=cc 			# C compiler
	 CFLAGS="$opt_flag -std"	# default C flags (optimize, ansi)
	 C_CFH="-DDECFortran"	# C w/ cfortran.h callable from FORTRAN
	 CFHFLAGS="$CFLAGS $C_CFH" # CFLAGS + C_CFH
         CPPFLAGS="-g -std"       # default C ++ flags (optimize, ansi)
         CPP_CFH="-DDECFortran" # C ++ w/cfortran.h callable from FORTRAN
         CPPFHFLAGS="$CPPFLAGS $CPP_CFH" # CFLAGS + C_CFH for C++
	 F77=f77 			# FORTRAN compiler
	 F77FLAGS="$opt_flag"	# common FORTRAN flags
	 HDFSYS=DEC_ALPHA		# system type as defined by HDF
	;;

    hp )
         CPP=CC                   # C++ compiler
	 CC=cc 			# C compiler
	 CFLAGS="$opt_flag -Ae"	# default C flags (optimize, ansi)
	 C_CFH="" 		# C w/ cfortran.h callable from FORTRAN
	 CFHFLAGS="$CFLAGS $C_CFH" # CFLAGS + C_CFH
         CPPFLAGS="-g -Aa +a1"    # default C ++ flags (optimize, ansi)
         CPP_CFH=""           # C ++ w/ cfortran.h callable from FORTRAN
 	 CFHFLAGS="$CFLAGS $C_CFH" # CFLAGS + C_CFH
	 F77=fort77		# FORTRAN compiler
	 F77FLAGS="$opt_flag"	# common FORTRAN flags
	 HDFSYS=HP9000		# system type as defined by HDF
	;;

    ibm )
         CPP=CC                   # C++ compiler
	 CC=cc 			# C compiler
	 CFLAGS="$opt_flag -qlanglvl=ansi" # default C flags (optimize, ansi)
	 C_CFH="" 		# C w/ cfortran.h callable from FORTRAN
	 CFHFLAGS="$CFLAGS $C_CFH" # CFLAGS + C_CFH
         CPPFLAGS="-g -qlanglvl=ansi" # default C ++ flags (optimize, ansi)
         CPP_CFH=""          # C ++ w/ cfortran.h callable from FORTRAN
         CPPFHFLAGS="$CPPFLAGS $CPP_CFH" # CFLAGS + C_CFH for C++
	 F77=xlf 			# FORTRAN compiler
	 F77FLAGS="$opt_flag"	# common FORTRAN flags
	 HDFSYS=IBM6000		# system type as defined by HDF
	;;

    winnt98 )

    CPP=CC                   # C++ compiler
	CC=gcc 			# C compiler
	CFLAGS="$opt_flag -O3 -Wall -W -Wundef -Wno-switch"	# default C flags (optimize, ansi)
	C_CFH="-Df2cFortran"	# C w/ cfortran.h callable from FORTRAN
	CFHFLAGS="$CFLAGS $C_CFH" # CFLAGS + C_CFH
    CPPFLAGS="-g -ansi"      # default C ++ flags (optimize, ansi)
    CPP_CFH="-Df2cFortran" # C ++ w/ cfortran.h callable from FORTRAN
    CPPFHFLAGS="$CPPFLAGS $CPP_CFH" # CFLAGS + C_CFH for C++
	F77=g77			# FORTRAN compiler
	F77FLAGS="$opt_flag"	# common FORTRAN flags
	HDFSYS=CYGWIN		# system type as defined by HDF
	;;

    linux )
         CPP=CC                   # C++ compiler
	 CC=gcc 			# C compiler
	 CFLAGS="$opt_flag -ansi"	# default C flags (optimize, ansi)
	 C_CFH="-Df2cFortran"	# C w/ cfortran.h callable from FORTRAN
	 CFHFLAGS="$CFLAGS $C_CFH" # CFLAGS + C_CFH
         CPPFLAGS="-g -ansi"      # default C ++ flags (optimize, ansi)
         CPP_CFH="-Df2cFortran" # C ++ w/ cfortran.h callable from FORTRAN
         CPPFHFLAGS="$CPPFLAGS $CPP_CFH" # CFLAGS + C_CFH for C++
	 F77=g77			# FORTRAN compiler
	 F77FLAGS="$opt_flag"	# common FORTRAN flags
	 HDFSYS=LINUX		# system type as defined by HDF
	;;

    sco )
         CPP=CC                   # C++ compiler
	 CC=cc 			# C compiler
	 CFLAGS="$opt_flag -posix"	# default C flags (optimize, ansi)
	 C_CFH="-Df2cFortran"	# C w/ cfortran.h callable from FORTRAN
	 CFHFLAGS="$CFLAGS $C_CFH" # CFLAGS + C_CFH
         CPPFLAGS="-g -posix"     # default C++ flags (optimize, ansi)
         CPP_CFH="-Df2cFortran"   # C++ w/ cfortran.h callable from FORTRAN
         CPPFHFLAGS="$CPPFLAGS $CPP_CFH" # CFLAGS + C_CFH for C++
	 F77=""			# FORTRAN compiler
	 F77FLAGS="$opt_flag"	# common FORTRAN flags
	 HDFSYS=SCO		# system type as defined by HDF
	;;

    sgi )
	if [ $OSTYPE = "IRIX64" ] ; then
		 CPP="CC -32"                  # C++ compiler
		 CC="cc -32"	# C compiler (32 bit)
		 F77="f77 -32"	# FORTRAN compiler (32 bit)
	else
		 CPP=CC                   # C++ compiler
                 CC=cc		# C compiler
                 F77=f77		# FORTRAN compiler
	fi
	 CFLAGS="$opt_flag -xansi -D_POSIX_SOURCE"	# default C flags (optimize, ansi)
	 C_CFH=""	 		# C w/ cfortran.h callable from FORTRAN
	 CFHFLAGS="$CFLAGS $C_CFH" # CFLAGS + C_CFH
         CPPFLAGS="-O -xansi -DSGI"             # default C++ flags (optimize, ansi)
         CPP_CFH=""                       # C++ w/ cfortran.h callable from FORTRAN
         CPPFHFLAGS="$CPPFLAGS $CPP_CFH" # CFLAGS + C_CFH for C++
	 F77FLAGS="$opt_flag"	# common FORTRAN flags
	 HDFSYS=IRIX		# system type as defined by HDF
	;;

    sgi32 )
         CPP="CC -n32"                  # C++ compiler
	 CC="cc -n32"		# C compiler (new-style 32 bit)
	 F77="f77 -n32"		# FORTRAN compiler (new-style 32 bit)
	 CFLAGS="$opt_flag -xansi -D_POSIX_SOURCE"	# default C flags (optimize, ansi)
	 C_CFH=""	 		# C w/ cfortran.h callable from FORTRAN
	 CFHFLAGS="$CFLAGS $C_CFH" # CFLAGS + C_CFH
         CPPFLAGS="-O -xansi " # default C++ flags (optimize, ansi)
         CPP_CFH=""               # C++ w/ cfortran.h callable from FORTRAN
         CPPFHFLAGS="$CPPFLAGS $CPP_CFH" # CFLAGS + C_CFH for C++
	 F77FLAGS="$opt_flag"	# common FORTRAN flags
	 HDFSYS=IRIX		# system type as defined by HDF
	;;

    sgi64 )
	set cpu_type=`hinv | fgrep CPU | head -1 | cut -d' ' -f3 | cut -b2`
	if [ "$cpu_type" = "4" ] ; then
	         CPP="CC -64 -mips3"                  # C++ compiler
		 CC="cc -64 -mips3"	# C compiler (R4?00 chip)
		 F77="f77 -64 -mips3"	# FORTRAN compiler (R4?00 chip)
	else
	         CPP="CC -64"                   # C++ compiler
                 CC="cc -64"	# C compiler
                 F77="f77 -64"	# FORTRAN compiler
	fi
	 CFLAGS="$opt_flag -xansi -D_POSIX_SOURCE"	# default C flags (optimize, ansi)
	 C_CFH=""	 		# C w/ cfortran.h callable from FORTRAN
	 CFHFLAGS="$CFLAGS $C_CFH" # CFLAGS + C_CFH
         CPPFLAGS="-O -xansi " # default C++ flags (optimize, ansi)
         CPP_CFH=""                       # C++ w/ cfortran.h callable from FORTRAN
         CPPFHFLAGS="$CPPFLAGS $CPP_CFH" # CFLAGS + C_CFH for C++
	 CFHFLAGS="$CFLAGS $C_CFH" # CFLAGS + C_CFH
	 F77FLAGS="$opt_flag"	# common FORTRAN flags
	 HDFSYS=IRIX		# system type as defined by HDF
	;;

    sun4 )
         CPP=CC                   # C++ compiler
	 CC=acc			# C compiler
	 CFLAGS="$opt_flag -Xa"	# default C flags (optimize, ansi)
	 C_CFH="-DsunFortran"	# C w/ cfortran.h callable from FORTRAN
	 CFHFLAGS="$CFLAGS $C_CFH" # CFLAGS + C_CFH
         CPPFLAGS="-O"            # default C++ flags (optimize, ansi)
         CPP_CFH=""                       # C++ w/ cfortran.h callable from FORTRAN
         CPPFHFLAGS="$CPPFLAGS $CPP_CFH" # CFLAGS + C_CFH for C++
	 F77=f77 			# FORTRAN compiler
	 F77FLAGS="$opt_flag"	# common FORTRAN flags
	 HDFSYS=SUN		# system type as defined by HDF
	;;

    sun5 )
         CPP=CC                   # C++ compiler
	 CC=cc			# C compiler
	 CFLAGS="$opt_flag -Xa"	# default C flags (optimize, ansi)
	 C_CFH="-DsunFortran"	# C w/ cfortran.h callable from FORTRAN
	 CFHFLAGS="$CFLAGS $C_CFH" # CFLAGS + C_CFH
         CPPFLAGS="$opt_flag"            # default C flags (optimize, ansi)
         CPP_CFH=""              # C w/ cfortran.h callable from FORTRAN
         CPPFHFLAGS="$CPPFLAGS $CPP_CFH" # CFLAGS + C_CFH for C++
	 F77=f77 			# FORTRAN compiler
	 F77FLAGS="$opt_flag"	# common FORTRAN flags
	 HDFSYS=SUN		# system type as defined by HDF
	;;

    * )
         CPP=CC                   # C++ compiler
	 CC=cc			# C compiler
	 CFLAGS="$opt_flag"	# default C flags (optimize)
	 C_CFH=""	        	# C w/ cfortran.h callable from FORTRAN
	 CFHFLAGS="$CFLAGS $C_CFH" # CFLAGS + C_CFH
         CPPFLAGS="-"             # default C ++ flags (optimize)
         CPP_CFH=""                       # C++ w/ cfortran.h callable from FORTRAN
         CPPFHFLAGS="$CPPFLAGS $CPP_CFH" # CFLAGS + C_CFH for C++
	 F77=f77 			# FORTRAN compiler
	 F77FLAGS="$opt_flag"	# common FORTRAN flags
	 HDFSYS=unknown		# system type as defined by HDF
	;;
esac

export CC F77 CFLAGS C_CFH CFHFLAGS CPPFLAGS CPP_CFH CPPFHFLAGS HDFSYS F77FLAGS

if [ $build_ftn = 0 ] ; then
  F77=none
  echo "WARNING: The toolkit is being built WITHOUT full FORTRAN support."
  echo ""
  f90_flag=0
  fc_path=""
  cc_path=""
  nag_flag=0
fi

#-----------------------------------------------------------------------------
#
#  Additional setup
#
#-----------------------------------------------------------------------------

echo ""
echo "Toolkit installation script: `basename $0`"
echo "Starting at: `date`"
echo ""

#
# check to see if DAAC build was requested
#

if [ "$pgs_daac" = "1" ] ; then    # yes

    echo "The DAAC version of the toolkit library libPGSTK.a will be built"

fi

if [ "$pgs_daac" = "0" ] ; then    # no

    echo "The SCF version of the toolkit library libPGSTK.a will be built"

fi

if [ "$pgs_daac" = "IR1" ] ; then    # yes

    echo "The IR1 DAAC version of the toolkit library libPGSTK.a will be built"

fi

echo "cpp_lib_only=" $cpp_lib_only
if [ "$cpp_lib_only" = "1" ] ; then    # no

    echo "The C++ version of the toolkit library libPGSTKcpp.a will be built"

fi

#
# Make sure old IR-1 DAAC environment variables are not set !
#

if [ $?ADD_IFLAGS  ] ; then
    ADD_IFLAGS=""
fi
if [ $?ADD_LFLAGS  ] ; then
    ADD_LFLAGS=""
fi
if [ $?ADD_LIBS  ] ; then
    ADD_LIBS=""
fi

#
# check to see if valid C compiler path was specified
#

cc=""

if [ "$cc_path" = ""  ] ; then
echo ""
else

    if [ -f $cc_path ] ; then

        cc=`basename $cc_path`
        cc_path=`echo $cc_path | sed 's%/[^/]*$%%'`
        CC=$cc			# user overrides C compiler name

    fi

    if [ -d $cc_path ] ; then		# sure cc_path is now a directory

        path=$cc_path $path	# prepend cc_path to search path

    else
        cc_path=""
        echo "Warning: C compiler directory $cc_path not found."
        echo  "Toolkit build may fail."

    fi

fi






echo ""


#
# check to see if gcc (GNU C compiler) build was requested
#

if [ "$use_gcc" = "1" ] ; then 

    #
    # make sure gcc is in the path
    #

    CC=""

    for dir in $path $start_path; do
        if [ -x ${dir}/gcc  ] ; then
            CC=${dir}/gcc	# found compiler: set variable CC
		fi
        				# append directory to path, if neeeded
        if [ "`echo $path | grep $dir`" = "" ] ; then
			path=$path $dir
            break
        fi
    done

    if [ "$CC" = "" ] ; then
        echo "$script_error Compiler 'gcc' was not found in the search path."
        echo "Please set your path to include the gcc compiler and retry."
        echo ""
        exit 1
    else
        echo "Using GNU C Compiler ($CC) for the installation."
    fi

    CFLAGS="$opt_flag -ansi"	# default C flags (optimize, ansi)
    CFHFLAGS="$CFLAGS $C_CFH"    # CFLAGS + C_CFH

fi

echo ""



echo ""
export CFLAGS CFHFLAGS
#
# check to see if DAAC build was requested
#

if [ "$pgs_daac" = "1" ] ; then    # modify build flags

    CFLAGS="$CFLAGS -DPGS_DAAC"
    CFHFLAGS="$CFHFLAGS -DPGS_DAAC"
    CPPFLAGS="$CPPFLAGS -DPGS_DAAC"
    CPPFHFLAGS="$CPPFHFLAGS -DPGS_DAAC"

fi
echo ""



#
# check to see if no-HDF build was requested
#

if [ "$no_hdf" = "1" ] ; then
    echo "The toolkit will be built  *** WITHOUT *** HDF support."
    echo "Some functionality may be missing - see documentation for details."
    echo ""
else
    echo "The toolkit will be built with HDF support."
    echo ""

    # NOTE: 
    # The toolkit environment files derive the HDF directory environment
    # variables from the value of the variable HDFHOME, so it MUST be 
    # properly set in order to install the toolkit with HDF support.
    # Normally, this value should be passed in from the parent process.
    # To override it, uncomment the next line and edit it for your site.

    # setenv HDFHOME <toolkit-path>/TOOLKIT_MTD/hdf/$BRAND/HDF4.1r5	# set HDF home directory

    #
    # Set other HDF environment variables based on the value of HDFHOME.
    # Toolkit compilations may depend on these.

    if [ $?HDFHOME  ] ; then
    
        if [ -d $HDFHOME/bin  ] ; then		# HDF utilities
            HDFBIN=$HDFHOME/bin
        else
            HDFBIN=$HDFHOME/hdf/bin
        fi

        if [ -d $HDFHOME/include  ] ; then	# HDF header files
            HDFINC=$HDFHOME/include
        else
            HDFINC=$HDFHOME/hdf/include
        fi

        if [ -d $HDFHOME/lib  ] ; then		# HDF libraries
            HDFLIB=$HDFHOME/lib
        else
            HDFLIB=$HDFHOME/hdf/lib
        fi
        
    else

        echo "$script_error Environment variable HDFHOME must be set to install with HDF."
        help=1
        Help
    fi
export HDFHOME HDFBIN HDFINC HDFLIB
fi

#
# check to see if no-HDF-EOS build was requested
#

if [ "$no_hdfeos" = "1" ] ; then
    echo "The toolkit will be built  *** WITHOUT *** HDF-EOS support."
    echo "Some functionality may be missing - see documentation for details."
    echo ""
else
    echo "The toolkit will be built with HDF-EOS support."
    echo ""

    # NOTE: 
    # The toolkit environment files derive the HDF-EOS directory environment
    # variables from the value of the variable HDFEOS_HOME, so it MUST be 
    # properly set in order to install the toolkit with HDF-EOS support.
    # Normally, this value should be passed in from the parent process.
    # To override it, uncomment the next line and edit it for your site.

    # setenv HDFEOS_HOME <toolkit-path>/TOOLKIT_MTD/hdfeos	# set HDF-EOS home directory

    #
    # Set other HDF-EOS environment variables based on the value of HDFEOS_HOME.
    # Toolkit compilations may depend on these.

    if [ $?HDFEOS_HOME  ] ; then
    
        # HDF-EOS utilities
	HDFEOS_BIN=$HDFEOS_HOME/bin/$BRAND

        # HDF-EOS header files
	HDFEOS_INC=$HDFEOS_HOME/include

        # HDF-EOS libraries
	HDFEOS_LIB=$HDFEOS_HOME/lib/$BRAND
        
    else

        echo "$script_error Environment variable HDFEOS_HOME must be set to install with HDF-EOS."
        help=1
        Help
    fi

fi


#-----------------------------------------------------------------------------
#
#  Install the Toolkit
#
#-----------------------------------------------------------------------------

echo "    Installing the Toolkit on host $HOST running $OSTYPE"
echo "    Toolkit home directory is: $PGSHOME"
echo ""

#
# If using formal track distribution, set up the architecture-dependent
# directories by copying the common directories to the appropriate 
# architecture.
#

if [ "$pgs_formal" = 1 ] ; then

    status_msg="set up architecture-dependent directories"
    echo "    "
    echo "    $status_msg ..."
    echo "    "

    for dir in $PGSBIN $PGSLIB $PGSOBJ $PGSCPPO; do
        echo "We are in directory ======> " $dir
        if [ -d $dir  ] ; then 

            continue 

        else

            if [ -d `dirname $dir`/common  ] ; then

                echo "Setting up $dir"

                # 
                # Cray patch: create .placeholder files in each
                # subdirectory of common so they won't get dropped
                # by Cray tar
                # 

                if [ "$BRAND" = "cray"  ] ; then

                    cd `dirname $dir`/common
                    dirs=`find . -type d -print`
                    for tmp_dir in $dirs; do
                        touch $tmp_dir/.placeholder
                    done

                fi

                cd `dirname $dir`

                tar cf - common | chdir $PGSTST ; tar xf - 
                mv $PGSTST/common $dir


                # 
                # Remove .placeholder files in each
                # subdirectory of common (created
                # by Cray patch above)
                # 

                if [ "$BRAND" = "cray"  ] ; then

                    cd `dirname $dir`/common
                    for tmp_dir in $dirs; do
                        \rm $tmp_dir/.placeholder
                    done

                    cd $dir
                    for tmp_dir in $dirs; do
                         \rm $tmp_dir/.placeholder
                    done

                fi

            else

                echo "Can't set up $dir"
                error_flag=1

            fi

        fi
    done

    # $PGSDAT is a special case
      
    if [ ! -d $PGSDAT  ] ; then
	mkdir $PGSDAT
	cd $PGSDAT

	mkdir CBP EPH PC

	if [ "$pgs_daac" = "1"  ] ; then
	    echo ""
	else


	    # For backward compatibility with older versions of the Toolkit.
	    # Do this ONLY for SCFs, the DAACs had better not need this!

	    mkdir CSC TD

	    ln -s ../../common/CSC/utcpole.dat CSC/utcpole.dat
	    ln -s ../../common/CSC/earthfigure.dat CSC/earthfigure.dat
	    ln -s ../../common/TD/leapsec.dat TD/leapsec.dat

	fi
	
    fi

    # $PGSRUN is also a special case

    cd $PGSRUN
	
    if [ ! -d $PGSRUN/$BRAND  ] ; then

	mkdir $PGSRUN/$BRAND

    fi

    tk_major_version=`egrep '#define[ \t]+PGSd_TOOLKIT_MAJOR_VERSION' $PGSINC/PGS_SMF.h | cut -d'"' -f2`

    # this comment is here because it contains the double quote (") character

    if [ $pgs_daac = 1  ] ; then
	tk_minor_version="DAAC"
    else
	tk_minor_version="SCF"
    fi

#    tk_version="$tk_minor_version $tk_major_version"

#    sed "s^TK_VERSION_STRING^$tk_version^" .PCF.$pcf_ext.template | \
#    sed "s^REMOVE THIS VERSION^EDIT OR REMOVE THIS^" | \
#    sed 's^$BRAND'"^$BRAND^" .PCF.$pcf_ext.template >! $BRAND/PCF.$pcf_ext
	
#    cp $PGSRUN/$BRAND/PCF.$pcf_ext $PGSDAT/PC

    if [ "$pgs_daac" = "1"  ] ; then
	    echo ""
	else


        # For backward compatibility with older versions of the Toolkit.
        # Do this ONLY for SCFs, the DAACs had better not need this!

        for real_file in ../database/common/CUC/* ; do
            bogus_file=`basename $real_file`
			done
	    if [ ! -f $bogus_file  ] ; then
		ln -s $real_file $bogus_file
	    fi
#	end
	unset bogus_file
	unset real_file
    fi

    # create a file in the Toolkit "home" directory indicating
    # the version of the Toolkit that is being installed
    
    touch ${PGSHOME}/VERSION
#    echo "${BRAND}:	$tk_version" | cat >>! ${PGSHOME}/VERSION
    echo "${BRAND}:	$tk_version" | cat >> ${PGSHOME}/VERSION
    
fi

cd $PGSHOME

#
# misc setup
#

umask 22		# set file permission mask
error_flag=0	# inititalize the error flag 

status_msg=""	# inititalize the status message

if [ $BRAND = "cray" ] ; then	# Cray uses different version of PGS_types.h

    cd $PGSINC
    status_msg="set up PGS_TYPES.h for the Cray platform"
    echo "    "
    echo "    $status_msg ..."
    echo "    "


    if [ -f PGS_TYPES.h.cray ] ; then
        if [ ! -f PGS_TYPES.h.non-cray ] ; then
            cp PGS_TYPES.h PGS_TYPES.h.non-cray # backup default PGS_TYPES.h
        fi
        cp PGS_TYPES.h.cray  PGS_TYPES.h   # use Cray version of PGS_TYPES.h
    else
        echo "    Can't find PGS_TYPES.h.cray"
        error_flag=1
    fi

    cd $PGSHOME

fi


#
# build preliminary utilities
#

#echo ""
#echo ""
#echo "-------------------------------------------------------------------------------"
#echo ""
#echo "Building utilities and SMF files"
#echo ""
#echo "-------------------------------------------------------------------------------"
#echo ""




status_msg="build the ephtobin utility"
echo "    "
echo "    $status_msg..."
echo "    "

cd $PGSSRC/CBP
if [ "$BRAND" = "winnt98" ] ; then
make -f makefile.win ephtobin
else
make ephtobin
fi

if [ "$?" = "0" ] ; then
echo ""
else
    error_flag=1
fi


#
# set appropriate data files for this machine: 
#

echo ""
echo ""
echo "-------------------------------------------------------------------------------"
echo ""
echo "Setting up installation-dependent database files"
echo ""
echo "-------------------------------------------------------------------------------"
echo ""

status_msg="set up database file CBP/de200.eos"
echo "    "
echo "    $status_msg ..."
echo "    "

cd $PGSDAT/CBP

if [ -f de200.eos ] ; then
 rm -f de200.eos    # get rid of old binary, if there
fi


#
# set appropriate scripts/utilities for this machine:
#

echo ""
echo ""
echo "-------------------------------------------------------------------------------"
echo ""
echo "Setting up installation-dependent scripts"
echo ""
echo "-------------------------------------------------------------------------------"
echo ""

cd $PGSBIN

if [ -f pgs-env.ksh ] ; then
/bin/rm -f pgs-env.ksh
touch pgs-env.ksh
fi

if [ -f pgs-env.csh ] ; then
/bin/rm -f pgs-env.csh
touch pgs-env.csh
fi

if [ -f pgs-dev-env.ksh ] ; then
/bin/rm -f pgs-dev-env.ksh
touch pgs-dev-env.ksh
fi

if [ -f pgs-dev-env.csh ] ; then
/bin/rm -f pgs-dev-env.csh
touch pgs-dev-env.csh
fi


if [ -f tmp/pgs-env.ksh.tmp ] ; then

    echo "    Setting up  Korn shell (ksh) script 'pgs-env.ksh' "
    echo "    Korn shell users should call this file from their .profile "
    echo "    file via the the line:"
    echo "    "
    echo "        . <PGS-home-dir>/bin/pgs-env.ksh  "
    echo "    "
    echo "    It sets up environment variables needed to run toolkit"
    echo "    code under ksh. HERE"
    echo "    "
    echo "# " >  pgs-env.ksh

    echo "# set the toolkit home directory environment variable" >> pgs-env.ksh
    echo "# Toolkit installation done on `date` " >> pgs-env.ksh
    echo "# " >> pgs-env.ksh
    echo " " >> pgs-env.ksh

    echo "PGSHOME=$PGSHOME	# set the toolkit home directory" >> pgs-env.ksh

    echo "pgs_formal=$pgs_formal		# formal directory structure (1=yes/0=no)" >> pgs-env.ksh 

    echo " " >> pgs-env.ksh

    cat tmp/pgs-env.ksh.tmp >> pgs-env.ksh
    sleep 5

fi

if [ -f tmp/pgs-dev-env.ksh.tmp ] ; then
    echo "    Setting up Korn shell (ksh) script 'pgs-dev-env.ksh' "
    echo "    Korn shell users may call this file from their .profile "
    echo "    file via the line:"
    echo "    "
    echo "        . <PGS-home-dir>/bin/pgs-dev-env.ksh  "
    echo "    "
    echo "    It sets up machine-specific environment variables needed "
    echo "    by the toolkit makefiles for compilation and linking."
    echo "    "

    echo "# " >  pgs-dev-env.ksh

    echo '# Clear all conditional flags'  	>> pgs-dev-env.ksh
    echo 'sgi_mode=""'  	>> pgs-dev-env.ksh
    echo 'pgs_daac=""'  	>> pgs-dev-env.ksh
    echo 'pgs_f90_comp=""' 	>> pgs-dev-env.ksh
    echo 'pgs_nag_flag=""' 	>> pgs-dev-env.ksh

    echo "# set the toolkit home directory environment variable" >> pgs-dev-env.ksh
    echo "# Toolkit installation done on `date` " >> pgs-dev-env.ksh
    echo "# " >> pgs-dev-env.ksh
    echo " " >> pgs-dev-env.ksh

    echo "PGSHOME=$PGSHOME	# set the toolkit home directory" >> pgs-dev-env.ksh

    echo " " >> pgs-dev-env.ksh

    if [ -n "$HDFHOME" ] &&  [ -n "$HDFEOS_HOME" ] ; then
	sed "s^OPT_LVL^$opt_flag^" tmp/pgs-dev-env.ksh.tmp                    | \
	sed "s^# HDFHOME=+++HDFHOME+++^HDFHOME=$HDFHOME^"                     | \
	sed "s^# HDF5HOME=+++HDF5HOME+++^HDF5HOME=$HDF5HOME^"                 | \
	sed "s^# HDFEOS_HOME=+++HDFEOS_HOME+++^HDFEOS_HOME=$HDFEOS_HOME^"     | \
	sed "s^# HDFEOS5_HOME=+++HDFEOS5_HOME+++^HDFEOS5_HOME=$HDFEOS5_HOME^" | \
	sed "s^# ZLIBHOME=+++ZLIBHOME+++^ZLIBHOME=$ZLIBHOME^"                 | \
	sed "s^# JPEGHOME=+++JPEGHOME+++^JPEGHOME=$JPEGHOME^"                 | \
	sed "s^# SZIPHOME=+++SZIPHOME+++^SZIPHOME=$SZIPHOME^"                >> \
	pgs-dev-env.ksh
    elif [ -n "$HDFHOME" ] ; then
	sed "s^OPT_LVL^$opt_flag^" tmp/pgs-dev-env.ksh.tmp                    | \
	sed "s^# HDFHOME=+++HDFHOME+++^HDFHOME=$HDFHOME^"                     | \
	sed "s^# HDF5HOME=+++HDF5HOME+++^HDF5HOME=$HDF5HOME^"                 | \
	sed "s^# HDFEOS5_HOME=+++HDFEOS5_HOME+++^HDFEOS5_HOME=$HDFEOS5_HOME^" | \
	sed "s^# ZLIBHOME=+++ZLIBHOME+++^ZLIBHOME=$ZLIBHOME^"                 | \
	sed "s^# JPEGHOME=+++JPEGHOME+++^JPEGHOME=$JPEGHOME^"                 | \
	sed "s^# SZIPHOME=+++SZIPHOME+++^SZIPHOME=$SZIPHOME^"                >> \
	pgs-dev-env.ksh
    elif [ -n "$HDFEOS_HOME" ] ; then
	sed "s^OPT_LVL^$opt_flag^" tmp/pgs-dev-env.ksh.tmp                    | \
	sed "s^# HDF5HOME=+++HDF5HOME+++^HDF5HOME=$HDF5HOME^"                 | \
	sed "s^# HDFEOS_HOME=+++HDFEOS_HOME+++^HDFEOS_HOME=$HDFEOS_HOME^"     | \
	sed "s^# HDFEOS5_HOME=+++HDFEOS5_HOME+++^HDFEOS5_HOME=$HDFEOS5_HOME^" | \
	sed "s^# ZLIBHOME=+++ZLIBHOME+++^ZLIBHOME=$ZLIBHOME^"                 | \
	sed "s^# JPEGHOME=+++JPEGHOME+++^JPEGHOME=$JPEGHOME^"                 | \
	sed "s^# SZIPHOME=+++SZIPHOME+++^SZIPHOME=$SZIPHOME^"                >> \
	pgs-dev-env.ksh
    else
	sed "s^OPT_LVL^$opt_flag^" tmp/pgs-dev-env.ksh.tmp >> \
	pgs-dev-env.ksh
    fi

    sleep 5
    
fi

if [ -f tmp/pgs-env.csh.tmp ] ; then

    echo "    Setting up C-shell (csh) script 'pgs-env.csh' "
    echo "    C shell users should call this file from their .cshrc "
    echo "    file via the the line:"
    echo "    "
    echo "        source <PGS-home-dir>/bin/pgs-env.csh  "
    echo "    "
    echo "    It sets up environment variables needed to run toolkit"
    echo "    code under csh."
    echo "    "

    echo "# " >  pgs-env.csh

    echo "# set the toolkit home directory environment variable" >> pgs-env.csh
    echo "# Toolkit installation done on `date` " >> pgs-env.csh
    echo "# " >> pgs-env.csh
    echo " " >> pgs-env.csh
    echo "setenv PGSHOME $PGSHOME	# set the toolkit home directory" >> pgs-env.csh

    echo "set pgs_formal=$pgs_formal	# formal directory structure (1=yes/0=no)" >> pgs-env.csh 

    echo " " >> pgs-env.csh

    cat tmp/pgs-env.csh.tmp >> pgs-env.csh
    sleep 5

fi

if [ -f tmp/pgs-dev-env.csh.tmp ] ; then

    echo "    Setting up C-shell (csh) script 'pgs-dev-env.csh' "
    echo "    This file may be called from your .cshrc file via the the line:"
    echo "    "
    echo "        source <PGS-home-dir>/bin/pgs-dev-env.csh  "
    echo "    "
    echo "    It sets up machine-specific environment variables needed "
    echo "    by the toolkit makefiles for compilation and linking."
    echo "    "

    echo "# " >  pgs-dev-env.csh

    echo '# Clear all conditional flags'  	>> pgs-dev-env.csh
    echo "unset sgi_mode" 	>> pgs-dev-env.csh
    echo "unset pgs_daac" 	>> pgs-dev-env.csh
    echo "unset pgs_f90_comp" 	>> pgs-dev-env.csh
    echo "unset pgs_nag_flag" 	>> pgs-dev-env.csh

    echo "# set the toolkit home directory environment variable" >> pgs-dev-env.csh
    echo "# Toolkit installation done on `date` " >> pgs-dev-env.csh
    echo "# " >> pgs-dev-env.csh
    echo " " >> pgs-dev-env.csh

    echo "setenv PGSHOME $PGSHOME	# set the toolkit home directory" >> pgs-dev-env.csh
    echo " " >> pgs-dev-env.csh

    if [ -n "$HDFHOME" ] &&  [ -n "$HDFEOS_HOME" ] ; then
	sed "s^OPT_LVL^$opt_flag^" tmp/pgs-dev-env.csh.tmp                              | \
        sed "s^# setenv HDFHOME +++HDFHOME+++^setenv HDFHOME $HDFHOME^"                 | \
        sed "s^# setenv HDFEOS_HOME +++HDFEOS_HOME+++^setenv HDFEOS_HOME $HDFEOS_HOME^" | \
        sed "s^# setenv ZLIBHOME +++ZLIBHOME+++^setenv ZLIBHOME $ZLIBHOME^"             | \
        sed "s^# setenv JPEGHOME +++JPEGHOME+++^setenv JPEGHOME $JPEGHOME^"             | \
        sed "s^# setenv SZIPHOME +++SZIPHOME+++^setenv SZIPHOME $SZIPHOME^"            >> \
	pgs-dev-env.csh
    elif [ -n "$HDFHOME" ] ; then
	sed "s^OPT_LVL^$opt_flag^" tmp/pgs-dev-env.csh.tmp                              | \
        sed "s^# setenv HDFHOME +++HDFHOME+++^setenv HDFHOME $HDFHOME^"                 | \
        sed "s^# setenv ZLIBHOME +++ZLIBHOME+++^setenv ZLIBHOME $ZLIBHOME^"             | \
        sed "s^# setenv JPEGHOME +++JPEGHOME+++^setenv JPEGHOME $JPEGHOME^"             | \
        sed "s^# setenv SZIPHOME +++SZIPHOME+++^setenv SZIPHOME $SZIPHOME^"            >> \
	pgs-dev-env.csh
    elif [ -n "$HDFEOS_HOME" ] ; then
	sed "s^OPT_LVL^$opt_flag^" tmp/pgs-dev-env.csh.tmp                              | \
        sed "s^# setenv HDFEOS_HOME +++HDFEOS_HOME+++^setenv HDFEOS_HOME $HDFEOS_HOME^" | \
        sed "s^# setenv ZLIBHOME +++ZLIBHOME+++^setenv ZLIBHOME $ZLIBHOME^"             | \
        sed "s^# setenv JPEGHOME +++JPEGHOME+++^setenv JPEGHOME $JPEGHOME^"             | \
        sed "s^# setenv SZIPHOME +++SZIPHOME+++^setenv SZIPHOME $SZIPHOME^"            >> \
	pgs-dev-env.csh
    else
	sed "s^OPT_LVL^$opt_flag^" tmp/pgs-dev-env.csh.tmp >> \
	pgs-dev-env.csh
    fi

    sleep 5

fi


#
#  Build the toolkit libraries
#

lib_only1:

echo ""
echo ""
echo "-------------------------------------------------------------------------------"
echo ""
echo "Creating the Toolkit Library"
echo ""
echo "-------------------------------------------------------------------------------"
echo ""
 
status_msg="compile toolkit source files"
echo "    "
echo "    $status_msg..."
echo "    "

cd $PGSBIN
if [ $BRAND = "winnt98" ] ; then
   if [ ! -e mkpgslib ]; then
      ln -s ../mkpgslib mkpgslib
   fi
fi
cd $PGSSRC

if [ -f $PGSLIB/libPGSTK.a ] ; then

    /bin/rm -f $PGSLIB/libPGSTK.a 
    if [ "$?" = "0" ] ; then
	echo ""
	else
        echo "Unable to remove old library: $PGSLIB/libPGSTK.a"
        echo "Please remove it manually and re-run the installation."
        error_flag=1
    fi
fi

for dir in `\ls`; do
    if [ -d $dir ] ; then

        case  "$dir" in 

	     MET )
            echo "$dir"
            if [ "$no_hdf" = "1" ] ; then

                echo "    "
                echo "        No HDF support: skipping $dir"
                echo "    "
	        else
	          cd $dir
              echo "    "
              echo "        Making all in $dir"
              echo "    "
		if [ "$BRAND" = "winnt98" ] ; then
		make -f makefile.win
		else
		make -f makefile
		fi
              if [ "$?" = "0" ] ; then
		echo ""
		else
                bad_tools="$bad_tools $dir"
              fi

              cd $PGSSRC
	      echo "$PGSSRC"
            fi
          ;;

          * )

            cd $dir
              echo "    "
              echo "        Making all in $dir"
              echo "    "
		if [ "$BRAND" = "winnt98" ] ; then
		make -f makefile.win 
		else
		make -f makefile
		fi
              echo "The status after building " $dir " is " $?
              if [ "$?" = "0" ] ; then
		echo ""
		else
                bad_tools="$bad_tools $dir"
              fi

              cd $PGSSRC

           ;;

        esac

    fi

done

if [ "$bad_tools" = "" ] ; then	# flag error in tool group compilation
echo ""
else
    error_flag=2
fi



#
# build additional utilities - these depend on libPGSTK.a
#

echo ""
echo ""
echo "-------------------------------------------------------------------------------"
echo ""
echo "Building additional utilites"
echo ""
echo "-------------------------------------------------------------------------------"
echo ""


#
#
#  Build the TD utilities
#

status_msg="build the TD utilities"
echo "    "
echo "    $status_msg ..."
echo "    "

cd $PGSSRC/TD
if [ "$BRAND" = "winnt98" ] ; then
make -f makefile.win  utilities
else
make -f makefile  utilities
fi
echo "The value of status after building the utilities is $?"
if [ "$?" = "0" ] ; then
echo ""
else
    error_flag=1
    bad_utils="$bad_utils TD"
fi


#
#  Build the EPH orbit simulator & s/c ephemeris check utilities
#



if [ "$error_flag" = "1"  ] ; then
    status_msg="build toolkit utilities"
fi


#
#  Clean up toolkit object files
#

status_msg="clean up toolkit object files"
echo "    "
echo "    $status_msg..."
echo "    "

cd $PGSSRC
echo "Removing all object files in PGS source directories..." 
find $PGSSRC -name \*.o -print -exec rm -f {} \; 
if [ "$?" = "0" ] ; then
echo ""
else
    error_flag=1
fi

echo "Removing all object files in PGS object directories..." 
find $PGSOBJ -name \*.o -print -exec rm -f {} \;
if [ "$?" = "0" ] ; then
echo ""
else
    error_flag=1
fi


#
#  Installation complete
#


echo ""
echo ""
echo ""

if [ "$bad_tools" = "" ] ; then
echo ""
else
    echo ""
    echo "Error may have occurred while attempting to compile toolkit source files"
    echo "Check the following tool groups for compilation errors:"
    echo "    ${bad_tools}"
    echo ""
fi

if [ "$bad_utils" = "" ] ; then
echo ""
else
    echo ""
    echo "Error may have occurred while attempting to compile toolkit utilities"
    echo "Check the following tool groups for compilation errors:"
    echo "    ${bad_utils}"
    echo ""
fi

case $error_flag in

   0 )
    echo "`basename $0` completed successfully at `date`"
    echo "The C library is in $PGSLIB"
    case $c_only in
	0 )   
	   ;;

	1 )
           exit 0
	   ;;
    esac
    ;;
     1 )
       echo "Error may have occurred while attempting to ${status_msg}"
       echo "`basename $0` completed at `date`"
       exit 0
       ;;

     2 )
       echo "`basename $0` completed at `date`"
       exit 0
       ;;
esac

#
# build preliminary utilities
#
cpp:

if [ "$c_only" = "1" ] ; then
 exit 0
fi

