#!/bin/sh
# Copyright (C) 1996 Hughes and Applied Research Corporation
#
# Permission to use, modify, and distribute this software and its documentation 
# for any purpose without fee is hereby granted, provided that the above 
# copyright notice appear in all copies and that both that copyright notice and 
# this permission notice appear in supporting documentation.
#
#-----------------------------------------------------------------------------
# filename:	
# 	INSTALL-HDFEOS
#
# description:
# 	This script handles the installation of HDFEOS5.
#
# usage:
# 	1) Set directory to HDFEOS top level directory.
# 	2) Type:
# 
# 	       bin/INSTALL-HDFEOS [-dbug] [-sgi32 | -sgi64] [-i<incdir>] [-l<libdir>] [-szi<SZincdir>] [-szl<SZlibdir>]
#              bin/INSTALL-HDFEOS [-cc_path <path/compiler_name>]
# 	       bin/INSTALL-HDFEOS [-h]
# 
# 	   Where (optional) flags are:
#
# 	      -dbug    : build debug version of HDF-EOS
# 	      -sgi32   : build in -n32 mode (SGI Power Challenge only)
# 	      -sgi64   : build in -64  mode (SGI Power Challenge only)
#             -ts      : build thread-safe version
#             -ts_dbug : build thread-safe version and enable debug statements
#             -cc_path : set the path of the C compiler to <path/compiler_name>
# 	      -i       : set HDF include directory to <libdir>
# 	      -l       : set HDF library directory to <libdir>
# 	      -szi     : set SZIP include directory to <libdir>
# 	      -szl     : set SZIP library directory to <libdir>
# 	      -h       : display this help message and exit
#
# 	3) Enter requested information when prompted.
# 
# notes:
# 	1) This script is compatible with the following platforms:
# 	   Sun, SGI, HP-9000, IBM RS-6000 and DEC Alpha.
# 	   It automatically figures out which platform you are on,
# 	   and sets environment variables accordingly.
#
# author:
# 	Mike Sucher / A.R.C.
#
# history:
#	18-Jun-1996 MES  Initial version 
# 	27-Sep-1996 MES  Add -i and -l options
#	17-Apr-1997 DaW  Added support for old 32-bit compiling on SGI
#			 Power Challenge
#	09-Nov-2000 DaW  Modified to work with HDF5.1r2.1
#       10-Oct-2001 AM   Modified to add option to build thread-safe version 
#       15-Sep-2003 PTN  Added -cc_path option
#       15-Apr-2004 PTN  Modified for Linux64
#       09-Nov-2005 TR   Added i686 (cygwin on Pentium)
#-----------------------------------------------------------------------------

this_script="`basename $0`"

#
# Function to output error message
#

WriteError()
{
    echo ""
    echo "${this_script}: Error: $*"  >&2
}


#
# Function prompt for user response
#

UserPrompt()
{
    if [ "$BRAND" = "linux" ] || [ "$BRAND" = "linux32" ] || [ "$BRAND" = "linux64" ] ; then
	/bin/echo "$* \c" > /dev/tty
    else
	echo "$* \\c" > /dev/tty
    fi
    read user_response
}


#
# Function to output help message and exit
#

Help()
{
    sed -n '/filename:/,/author:/p' $0 | grep -v "author:" | cut -c3-300 | more
    #this line must be here because it contains the string: "author:"
    exit
}


#
# Function to output a startup banner
#

Banner()
{
    echo ""
    echo "-----------------------------------------------------------------------"
    echo "$this_script: Installation script for HDFEOS5"
    echo ""
    echo "    Installing HDFEOS5 on host $HOST running $OSTYPE"
    echo "    HDFEOS5 home directory is: $HDFEOS5_HOME"
    echo ""
    echo "    Starting HDFEOS5 installation at `date`"
    echo ""
    echo "-----------------------------------------------------------------------"
    echo ""
}

#
# Function to handle error exit
#

ErrorExit()
{
    echo ""
    echo "${this_script}: Fatal error running command: $*"  >&2
    echo ""

    echo "-----------------------------------------------------------------------"
    echo ""
    echo "    HDFEOS5 installation ending with errors at `date`"
    echo ""
    echo "-----------------------------------------------------------------------"

    exit 1
}


#
# Functions to check HDF5 environment variables for validity
#

ValidHDFLIB()
{
    if [ "$HDF5LIB" = "" ] ; then
        WriteError "No HDF5 library directory specified"
        exit 1
    else
        if [ ! -f "$HDF5LIB/libhdf5.a" ] ; then
            WriteError "HDF5 library not found in: $HDF5LIB"
            exit 1
        fi
    fi
}

ValidHDFINC()
{
    if [ "$HDF5INC" = "" ] ; then
        WriteError "No HDF5 include directory specified"
        exit 1
    else
        if [ ! -f "$HDF5INC/hdf5.h" ] ; then
            WriteError "HDF5 header file not found in: $HDF5INC"
            exit 1
        fi
    fi
}

#
# Functions to check SZIP environment variables for validity
#

ValidSZIPLIB()
{
    if [ "$SZIPLIB" = "" ] ; then
        WriteError "No SZIP library directory specified"
        exit 1
    else
        if [ ! -f "$SZIPLIB/libsz.a" ] ; then
            WriteError "SZIP library not found in: $SZIPLIB"
            exit 1
        fi
    fi
}

ValidSZIPINC()
{
    if [ "$SZIPINC" = "" ] ; then
        WriteError "No SZIP include directory specified"
        exit 1
    else
        if [ ! -f "$SZIPINC/szlib.h" ] ; then
            WriteError "SZIP header file not found in: $SZIPINC"
            exit 1
        fi
    fi
}
#
# Function to set up the HDF5 environment variables HDF5INC and HDF5LIB 
# The values are set via the following priority scheme:
#
#     (1) command line override, via the -l and -i flags
#     (2) loaded from the saved HDF5 environment file
#     (3) user prompt or inherited from the environment.
# 
# The user will be prompted to override inherited values, except in
# case (1).
#

GetHdfEnv()
{
    #
    # If HDF5INC or HDF5LIB has not been specified on the command line. 
    # first look for them in the saved HDF5 environment file
    #

    hdf_env_file=$HDFEOS5_BIN/.hdf_env

    if [ -f $hdf_env_file ] ; then

        if [ $lib_flag = 0 ] ; then
            echo "Getting HDF5LIB from $hdf_env_file"
            HDF5LIB=`head -1 $hdf_env_file`
        fi

        if [ $inc_flag = 0 ] ; then
            echo "Getting HDF5INC from $hdf_env_file"
            HDF5INC=`tail -1 $hdf_env_file`
        fi

    fi

    #
    # If HDF5INC or HDF5LIB is undefined at this point, then prompt for them
    # Otherwise ask if they should be accepted, unless specified via the
    # command line.  Check them in any case !
    #

    if [ $lib_flag = 0 ] ; then
        if [ "$HDF5LIB" = "" ] ; then
            UserPrompt "Please enter the HDF5 library files directory: "
        else
            echo "Current value of the HDF5 library directory is: $HDF5LIB"
            UserPrompt "New value (or Return to accept):"
        fi
        if [ "$user_response" != "" ] ; then
            HDF5LIB="$user_response"
        fi
    fi

    ValidHDFLIB 	# make sure it's valid before proceeding
    

    if [ $inc_flag = 0 ] ; then
        if [ "$HDF5INC" = "" ] ; then
            UserPrompt "Please enter the HDF5 include files directory: "
        else
            echo "Current value of the HDF5 include directory is: $HDF5INC"
            UserPrompt "New value (or Return to accept):"
        fi
        if [ "$user_response" != "" ] ; then
            HDF5INC="$user_response"
        fi
    fi

    ValidHDFINC  	# make sure it's valid before proceeding


    echo ""
    echo "HDF5 include files in: $HDF5INC"
    echo "HDF5 library files in: $HDF5LIB"


    cat /dev/null > $hdf_env_file
    echo $HDF5LIB >> $hdf_env_file
    echo $HDF5INC >> $hdf_env_file
    
    export HDF5LIB HDF5INC

}


#
# Function to set up the SZIP environment variables SZIPINC and SZIPLIB 
# The values are set via the following priority scheme:
#
#     (1) command line override, via the -szl and -szi flags
#     (2) loaded from the saved SZIP environment file
#     (3) user prompt or inherited from the environment.
# 
# The user will be prompted to override inherited values, except in
# case (1).
#

GetSzipEnv()
{
    #
    # If SZIPINC or SZIPLIB has not been specified on the command line. 
    # first look for them in the saved SZIP environment file
    #

    szip_env_file=$HDFEOS5_BIN/.szip_env

    if [ -f $szip_env_file ] ; then

        if [ $szlib_flag = 0 ] ; then
            echo "Getting SZIPLIB from $szip_env_file"
            SZIPLIB=`head -1 $szip_env_file`
        fi

        if [ $szinc_flag = 0 ] ; then
            echo "Getting SZIPINC from $szip_env_file"
            SZIPINC=`tail -1 $szip_env_file`
        fi

    fi

    #
    # If SZIPINC or SZIPLIB is undefined at this point, then prompt for them
    # Otherwise ask if they should be accepted, unless specified via the
    # command line.  Check them in any case !
    #

    if [ $szlib_flag = 0 ] ; then
        if [ "$SZIPLIB" = "" ] ; then
            UserPrompt "Please enter the SZIP library files directory: "
        else
            echo "Current value of the SZIP library directory is: $SZIPLIB"
            UserPrompt "New value (or Return to accept):"
        fi
        if [ "$user_response" != "" ] ; then
            SZIPLIB="$user_response"
        fi
    fi

    ValidSZIPLIB 	# make sure it's valid before proceeding
    

    if [ $szinc_flag = 0 ] ; then
        if [ "$SZIPINC" = "" ] ; then
            UserPrompt "Please enter the SZIP include files directory: "
        else
            echo "Current value of the SZIP include directory is: $SZIPINC"
            UserPrompt "New value (or Return to accept):"
        fi
        if [ "$user_response" != "" ] ; then
            SZIPINC="$user_response"
        fi
    fi

    ValidSZIPINC  	# make sure it's valid before proceeding


    echo ""
    echo "SZIP include files in: $SZIPINC"
    echo "SZIP library files in: $SZIPLIB"


    cat /dev/null > $szip_env_file
    echo $SZIPLIB >> $szip_env_file
    echo $SZIPINC >> $szip_env_file
    
    export SZIPLIB SZIPINC

}

#
# Function to set up architecture-dependent environment scripts
#

SetupEnvScripts()
{

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

cd $HDFEOS5_BIN

if [ -f ../tmp/hdfeos_env.ksh.tmp ] ; then

    echo "    Setting up Korn shell (ksh) script 'hdfeos_env.ksh' "
    echo "    Korn shell users may call this file from their .profile "
    echo "    file via the the line:"
    echo "    "
    echo "        . $HDFEOS5_BIN/hdfeos_env.ksh  "
    echo "    "
    echo "    It sets up machine-specific environment variables needed "
    echo "    by the HDFEOS5 makefiles for compilation and linking."
    echo "    "

    echo "# " > hdfeos_env.ksh

    echo '# Clear all conditional flags'  	>> hdfeos_env.ksh
    echo 'sgi_mode=""'  	>> hdfeos_env.ksh
    echo 'hdfeos_f90_comp=""' 	>> hdfeos_env.ksh
    echo 'hdfeos_nag_flag=""' 	>> hdfeos_env.ksh
    if [ "`uname -m`" = "x86_64" ] ; then
	if [ "$LNX_COMP_FLAG" = "-m32" ] ; then
		echo 'LNX_COMP_FLAG="-m32"' 	>> hdfeos_env.ksh
	else
		echo 'LNX_COMP_FLAG=""' 	>> hdfeos_env.ksh
	fi
    elif [ "`uname -m`" = "ia64" ] ; then
	if [ "$LNX_COMP_FLAG" = "-m32" ] ; then
		echo 'LNX_COMP_FLAG="-m32"' 	>> hdfeos_env.ksh
	else
		echo 'LNX_COMP_FLAG=""' 	>> hdfeos_env.ksh
	fi
    else
	echo 'LNX_COMP_FLAG=""' 	>> hdfeos_env.ksh
    fi

    echo " " >> hdfeos_env.ksh
    echo "# set the HDFEOS5 home directory and HDF5 variables" >> hdfeos_env.ksh
    echo "# HDFEOS5 installation done on `date` " >> hdfeos_env.ksh
    echo "# " >> hdfeos_env.ksh
    echo " " >> hdfeos_env.ksh

    echo "HDFEOS5_HOME=$HDFEOS5_HOME	# the HDFEOS5 home directory" >> hdfeos_env.ksh
    echo "HDF5LIB=$HDF5LIB 		# the HDF5 lib directory" >> hdfeos_env.ksh
    echo "HDF5INC=$HDF5INC 		# the HDF5 include directory" >> hdfeos_env.ksh
    echo "SZIPLIB=$SZIPLIB 		# the SZIP lib directory" >> hdfeos_env.ksh
    echo "SZIPINC=$SZIPINC 		# the SZIP include directory" >> hdfeos_env.ksh
    case "$sgi_mode" in
      64 ) echo "sgi_mode=64 		# SGI for -64 mode" >> hdfeos_env.ksh
        ;;
      n32 ) echo "sgi_mode=n32 		# SGI for -n32 mode" >> hdfeos_env.ksh
        ;;
      * ) echo "sgi_mode=64 		# SGI for standard mode" >> hdfeos_env.ksh
        ;;
    esac


    if [ "$f90_flag" = "1" ] ; then	 	#  do FORTRAN-90 setup

        echo "hdfeos_f90_comp='$F77' 		# f90 compiler" >> hdfeos_env.ksh

        if [ "$nag_flag" = "1" ] ; then 	#  using NAG f90
            echo "hdfeos_nag_flag=1		# using NAG f90" >> hdfeos_env.ksh
        fi

    fi

    echo "opt_flag='$opt_flag'		# set compiler optimization level" >> hdfeos_env.ksh

    echo " " >> hdfeos_env.ksh

    sed "s^LNX_CMP_FLAG^$LNX_COMP_FLAG^" ../tmp/hdfeos_env.ksh.tmp >> hdfeos_env.ksh.1
    cat hdfeos_env.ksh.1 >> hdfeos_env.ksh
    /bin/rm -f hdfeos_env.ksh.1
    
    sleep 3

fi

echo "                -----------------------------------"
echo ""

if [ -f ../tmp/hdfeos_env.csh.tmp ] ; then

    echo "    Setting up C-shell (csh) script 'hdfeos_env.csh' "
    echo "    This file may be called from your .cshrc file via the the line:"
    echo "    "
    echo "        source $HDFEOS5_BIN/hdfeos_env.csh  "
    echo "    "
    echo "    It sets up machine-specific environment variables needed "
    echo "    by the HDFEOS5 makefiles for compilation and linking."
    echo "    "

    echo "# " > hdfeos_env.csh
    if [ "`uname -m`" = "x86_64" ] ; then
	if [ "$LNX_COMP_FLAG" = "-m32" ] ; then
		echo 'setenv LNX_COMP_FLAG "-m32"' 	>> hdfeos_env.csh
	else
		echo 'setenv LNX_COMP_FLAG ""' 	        >> hdfeos_env.csh
	fi
    elif [ "`uname -m`" = "ia64" ] ; then
	if [ "$LNX_COMP_FLAG" = "-m32" ] ; then
		echo 'setenv LNX_COMP_FLAG "-m32"' 	>> hdfeos_env.csh
	else
		echo 'setenv LNX_COMP_FLAG ""' 	        >> hdfeos_env.csh
	fi
    else
	echo 'setenv LNX_COMP_FLAG ""' 	>> hdfeos_env.csh
    fi

    echo '# Clear all conditional flags'  	>> hdfeos_env.csh
    echo "unset sgi_mode" 	>> hdfeos_env.csh
    echo "unset hdfeos_f90_comp" 	>> hdfeos_env.csh
    echo "unset hdfeos_nag_flag" 	>> hdfeos_env.csh

    echo " " >> hdfeos_env.csh
    echo "# set the HDFEOS5 home directory and HDF5 variables" >> hdfeos_env.csh
    echo "# HDFEOS5 installation done on `date` " >> hdfeos_env.csh
    echo "# " >> hdfeos_env.csh
    echo " " >> hdfeos_env.csh

    echo "setenv HDFEOS5_HOME $HDFEOS5_HOME	# the HDFEOS5 home directory" >> hdfeos_env.csh
    echo "setenv HDF5LIB $HDF5LIB 		# the HDF5 lib directory" >> hdfeos_env.csh
    echo "setenv HDF5INC $HDF5INC 		# the HDF5 include directory" >> hdfeos_env.csh
    echo "setenv SZIPLIB $SZIPLIB 		# the SZIP lib directory" >> hdfeos_env.csh
    echo "setenv SZIPINC $SZIPINC 		# the SZIP include directory" >> hdfeos_env.csh

    case "$sgi_mode" in
      64 ) echo "set sgi_mode=64 		# SGI for -64 mode" >> hdfeos_env.csh
        ;;
      n32 ) echo "set sgi_mode=n32 		# SGI for -n32 mode" >> hdfeos_env.csh
        ;;
      * ) echo "set sgi_mode=64 		# SGI for standard mode" >> hdfeos_env.csh
        ;;
    esac


    if [ "$f90_flag" = "1" ] ; then 		# do FORTRAN-90 setup

        echo "hdfeos_f90_comp='$F77' 		# f90 compiler" >> hdfeos_env.csh

        if [ "$nag_flag" = "1" ] ; then 	# check if using NAG f90
            echo "hdfeos_nag_flag=1		# using NAG f90" >> hdfeos_env.csh
        fi

    fi

    echo "set opt_flag='$opt_flag'		# set compiler optimization level" >> hdfeos_env.csh

    echo " " >> hdfeos_env.csh

    sed "s^LNX_CMP_FLAG^$LNX_COMP_FLAG^" ../tmp/hdfeos_env.csh.tmp >> hdfeos_env.csh.1
    cat hdfeos_env.csh.1 >> hdfeos_env.csh
    /bin/rm -f hdfeos_env.csh.1

    sleep 3

fi


}


#
# Get parameters from command line
#

sgi_mode=64         # by default, SGI mode is standard 64-bit
pgs_f90_comp=""     # by default, no FORTRAN-90 compiler
pgs_nag_flag=0      # by default, not using NAG FORTRAN-90
inc_flag=0          # HDF5INC override flag
lib_flag=0          # HDF5LIB override flag
szinc_flag=0		# SZIPINC override flag
szlib_flag=0		# SZIPLIB override flag
opt_flag=-O         # compiler optimization level
ts_flag=""          # build threadsafe version flag
fc_path=""
cc_path=""

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

    case $1 in

      -h | -he  | -hel  | -help  ) Help ;;	# help

      -sgi32) sgi_mode=n32 ;;
      -sgi64) sgi_mode=64 ;;

      -dbug) opt_flag=-g ;;

      -ts) ts_flag=-D_HDFEOS5_THREADSAFE ;;
      -ts_dbug) ts_flag="-D_HDFEOS5_THREADSAFE -D_HDFEOS5_THREADSAFE_DEBUG" ;;

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

      -i) HDF5INC=$2 ; inc_flag=1; shift ;;
      -l) HDF5LIB=$2 ; lib_flag=1; shift ;;

      -szi) SZIPINC=$2 ; szinc_flag=1; shift ;;
      -szl) SZIPLIB=$2 ; szlib_flag=1; shift ;;

      -i*) HDF5INC=`echo $1 | cut -c3-` ; inc_flag=1 ;;
      -l*) HDF5LIB=`echo $1 | cut -c3-` ; lib_flag=1 ;;

      -szi*) SZIPINC=`echo $1 | cut -c5-` ; szinc_flag=1 ;;
      -szl*) SZIPLIB=`echo $1 | cut -c5-` ; szlib_flag=1 ;;

      -*)
        echo "Invalid option: $1"
        Help
        ;;

      *) 				# default
        value1="$1"
        ;;

    esac

    shift

done



# 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/local/bin:/bin:/usr/bin:/etc:/usr/etc:/usr/ucb:/usr/bin/X11
export PATH 

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

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

case "$MACHINE" in
    i386) OSTYPE=sco386 ;;		# SCO box
    i686) OSTYPE=sco686 ;;      # Pentium
    CRAY) OSTYPE=UNICOS ;;		# CRAY
    *) OSTYPE="`uname`"	;;		# everybody else
esac
    
CYGPL="`uname | awk -F_ '{print $1}'`"  
         
# Intel Macintosh is also i386 or i686 (?) machine

    if [ "$MACHINE" = "i386" ] ; then
	if [ "$temp_ostype" = "Darwin" ] ; then 
	    OSTYPE=DarwinIntel
	fi
	if [ "$CYGPL" = "CYGWIN" ] ; then 
	    OSTYPE=Cygwin
	fi
    fi
    if [ "$MACHINE" = "i686" ] ; then
	if [ "$temp_ostype" = "Darwin" ] ; then 
	    OSTYPE=DarwinIntel
	fi
	if [ "$CYGPL" = "CYGWIN" ] ; then 
	    OSTYPE=Cygwin
	fi
    fi
    if [ "$MACHINE" = "x86_64" ] ; then
	if [ "$LINUX_BRAND" = "" ] ; then 
	    echo " Error: In 64-bit linux platform the env. variable LINUX_BRAND must be set to linux32 or linux64 before running this script."
	    exit 1
	else
	  if [ "$LINUX_BRAND" = "linux32" ] ; then
	      if [ "$LNX_COMP_FLAG" = ""  ] ; then
		  LNX_COMP_FLAG="-m32"
	      fi
	  fi
	fi
     fi


user=`id | cut -d\( -f2 | cut -d\) -f1`

if [ "$LOGNAME" != "$user"  ] ; then
        LOGNAME=$user
        export LOGNAME
fi
if [ "$USER" != "$LOGNAME"  ] ; then
        LOGNAME=$LOGNAME
        export USER
fi


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

case "$OSTYPE" in

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

  HP-UX) 
    PATH=/usr/local/bin:/bin:/usr/bin:/etc:/usr/bin/X11
    HOST="`hostname`"
    BRAND="hp"
    if [ `uname -r | awk -F. '{print $2}'` = "11" ] ; then 
        OS_VERSION="11"			# release 11 
        echo HP-UX 11 platform
    else
        OS_VERSION="10"                 # release 10
        echo HP-UX 10 platform
    fi
    ;;

  IRIX) 
    PATH=/usr/local/bin:/bin:/usr/bin:/etc:/usr/etc:/usr/bsd:/usr/sbin
    HOST="`hostname`"
    if [ `uname -r | awk -F. '{print $2}'` = "5" ] ; then
       OS_VERSION="5"                  # release V6.5 IRIX
       echo IRIX 6.5 platform
       case $sgi_mode in
          64 ) BRAND=sgi64 ;;
          n32) BRAND=sgi32 ;;
          *  ) BRAND=sgi64 ;;  # just in case
       esac
    fi
    if [ `uname -r | awk -F. '{print $2}'` = "2" ] ; then
       OS_VERSION="2"                  # release V6.2 IRIX
       echo IRIX 6.2 platform
       case $sgi_mode in
          64 ) BRAND=sgi64 ;;
          n32) BRAND=sgi32 ;;
          *  ) BRAND=sgi64 ;;  # just in case
       esac
    else
       BRAND="sgi"
    fi
    ;;

  IRIX64) 
    PATH=/usr/local/bin:/bin:/usr/bin:/etc:/usr/etc:/usr/bsd:/usr/sbin
    HOST="`hostname`"
    if [ `uname -r | awk -F. '{print $2}'` = "5" ] ; then
       OS_VERSION="5"                  # release V6.5 IRIX
       echo IRIX 6.5 platform
       case $sgi_mode in
          64 ) BRAND=sgi64 ;;
          n32) BRAND=sgi32 ;;
          *  ) BRAND=sgi64 ;;  # just in case
       esac
    else
       OS_VERSION="2"                  # release V6.2 IRIX
       echo IRIX 6.2 platform
       case $sgi_mode in
          64 ) BRAND=sgi64 ;;
          n32) BRAND=sgi32 ;;
          *  ) BRAND=sgi64 ;;  # just in case
       esac
    fi
    ;;

  Linux )
    PATH=/usr/local/bin:/bin:/usr/bin:/etc:/usr/etc:/usr/X11/bin
    HOST=`hostname`
    BRAND=linux
    if [ "$LINUX_BRAND" = "linux64" ] ; then
	BRAND=linux64
    elif [ "$LINUX_BRAND" = "linux32" ] ; then
	BRAND=linux32
    fi
    if [ "$LNX_COMP_FLAG" = "-m32"  ] ; then
        export LNX_COMP_FLAG
    else
	LNX_COMP_FLAG=""
	export LNX_COMP_FLAG
    fi
    ;;

  Darwin)
    PATH=/bin:/sbin:/usr/bin:/usr/sbin
    pgs_host=`hostname`
    BRAND=macintosh
    ;;

  DarwinIntel)
    PATH=/bin:/sbin:/usr/bin:/usr/sbin
    pgs_host=`hostname`
    BRAND=macintel
    ;;

  Cygwin)
    PATH=/bin:/sbin:/usr/bin:/usr/sbin
    pgs_host=`hostname`
    BRAND=cygwin
    ;;

  OSF1) 
    PATH=/usr/local/bin:/bin:/usr/bin:/etc:/usr/etc:/usr/ucb:/usr/bin/X11:/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`"
	BRAND=linux
	if [ "$LINUX_BRAND" = "linux64" ] ; then
	    BRAND=linux64
	elif [ "$LINUX_BRAND" = "linux32" ] ; then
	    BRAND=linux32
	fi
    else
	PATH=/usr/local/bin:/bin:/usr/bin:/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
	if [ "$LINUX_BRAND" = "linux64" ] ; then
	    BRAND=linux64
	elif [ "$LINUX_BRAND" = "linux32" ] ; then
	    BRAND=linux32
	fi
    else
	PATH=/usr/local/bin:/bin:/usr/bin:/etc:/usr/bin/X11
	HOST="`hostname`"
	BRAND="winnt98"
    fi
    ;;

  SunOS) 
    # distinguish between SunOS 5.x versions
    if [ `uname -r | awk -F. '{print $1}'` = "5" ] ; then 
       	if [ `uname -r | awk -F. '{print $2}'` = "10" ] ; then
	    OS_VERSION="10"                # release V5.10 SunOS
	    BRAND="sun5.10"			# release V5.x SunOS
	    echo Solaris 10 platform
	elif [ `uname -r | awk -F. '{print $2}'` = "9" ] ; then
	    OS_VERSION="9"                # release V5.9 SunOS
	    BRAND="sun5.9"			# release V5.x SunOS
	    echo Solaris 9 platform
	elif [ `uname -r | awk -F. '{print $2}'` = "8" ] ; then
          OS_VERSION="8"                # release V5.8 SunOS
          BRAND="sun5.8"		# release V5.x SunOS
          echo Solaris 2.8 platform
       else
          OS_VERSION="5"                # release V5.8 SunOS
          BRAND="sun5"			# release V5.x SunOS
          echo Solaris 2.5 platform
       fi
       PATH=/usr/local/bin:/opt/SUNWspro/bin:/bin:/usr/bin:/etc:/usr/etc:/usr/ucb:/usr/openwin/bin:/usr/openwin/demo:/usr/sbin:/usr/ccs/bin
    fi
    HOST="`hostname`"
    ;;

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

  *)	
    echo "Operating system: $OSTYPE not supported"
    echo "This release of HDFEOS5 supports: "
    echo "   Sun, SGI, HP-9000, Linux and DEC-Alpha "
    ;;

esac

export PATH HOST BRAND


#
# Flag IRIX 6.2 because this may impact on makefiles
#

irix53=0
case $BRAND in
  sgi ) 
    if [ "$OSTYPE" = "IRIX" ] ; then
        irix53=1 
    fi
    ;;
esac
export irix53


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

NSL_FLAG="" 			# this is nil on all but Sun platforms
NSL_LIB="" 			# this is nil on all but Sun platforms
AR="ar r"			# command to build library
RANLIB=touch			# default for platforms that don't need ranlib
RANLIBSUN4=touch		# default for platforms that don't need ranlib

case "$BRAND" in

    cray)
	CC=cc 			  # C compiler
	CFLAGS="$opt_flag -DH5_USE_16_API" # default C flags (optimize, ansi)
	C_CFH="-DCRAYFortran"     # C/cfortran.h called from FORTRAN
	CFHFLAGS="$CFLAGS $C_CFH" # CFLAGS + C_CFH
	C_F77_CFH="$C_CFH"	  # calling FORTRAN
	C_F77_LIB=""		  # FORTRAN lib called by C main
	F77=cf77		  # FORTRAN compiler
	F77FLAGS="$opt_flag -DH5_USE_16_API"	  # common FORTRAN flags
	F77_CFH=""		  # FORTRAN callable from C w/ cfortran.h
	F77_C_CFH="$F77_CFH"	  # calling C w/ cfortran.h
	CFH_F77="$F77_C_CFH"	  # old version of F77_C_CFH
	F77_C_LIB=""		  # C lib called by FORTRAN main 
	HDFSYS=UNICOS		  # system type as defined by HDF5
	MACHINE=CRAY		  # system type as defined by HDFEOS5
	;;

    dec)
	CC=cc 			         # C compiler
	CFLAGS="$opt_flag $ts_flag -std -DH5_USE_16_API" # default C flags
	C_CFH="-DDECFortran"	         # C w/ cfortran.h callable from FORTRAN
	CFHFLAGS="$CFLAGS $C_CFH"        # CFLAGS + C_CFH
	C_F77_CFH="$C_CFH -Dmain=MAIN__" # calling FORTRAN
	C_F77_LIB=""		         # FORTRAN lib called by C main
	F77=f77 		         # FORTRAN compiler
	F77FLAGS="$opt_flag -DH5_USE_16_API"	         # common FORTRAN flags
	F77_CFH=""		         # FORTRAN callable from C w/ cfortran.h
	F77_C_CFH="$F77_CFH "            # calling C w/ cfortran.h
	CFH_F77="$F77_C_CFH"	         # old version of F77_C_CFH
	F77_C_LIB=""		         # C lib called by FORTRAN main
	HDFSYS=DEC_ALPHA	         # system type as defined by HDF5
	MACHINE=DEC		         # system type as defined by HDFEOS5
	;;

    hp)
	CC=cc			         # C compiler
	CFLAGS="$opt_flag $ts_flag -Ae -DH5_USE_16_API"  # default C flags
	C_CFH="" 		         # C w/cfortran.h callable from FORTRAN
	CFHFLAGS="$CFLAGS $C_CFH"        # CFLAGS + C_CFH
	C_F77_CFH="$C_CFH"	         # calling FORTRAN
	C_F77_LIB=""		         # FORTRAN lib called by C main 
	F77=fort77		         # FORTRAN compiler
	F77FLAGS="$opt_flag -DH5_USE_16_API"	         # common FORTRAN flags
	F77_CFH=""		         # FORTRAN callable from C w/cfortran.h
	F77_C_CFH="$F77_CFH"	         # calling C w/ cfortran.h
	CFH_F77="$F77_C_CFH"	         # old version of F77_C_CFH
	F77_C_LIB=""		         # C lib called by FORTRAN main
	HDFSYS=HP9000		         # system type as defined by HDF5
	MACHINE=HP		         # system type as defined by HDFEOS5
	;;

    ibm)
	CC=cc 			         # C compiler
	CFLAGS="$opt_flag -qlanglvl=ansi -DH5_USE_16_API" # default C flags (optimize, ansi)
	C_CFH="" 		         # C w/cfortran.h callable from FORTRAN
	CFHFLAGS="$CFLAGS $C_CFH"        # CFLAGS + C_CFH
	C_F77_CFH="$C_CFH"	         # calling FORTRAN
	C_F77_LIB=""		         # FORTRAN lib called by C main  FORTAN
	F77=xlf 		         # FORTRAN compiler
	F77FLAGS="$opt_flag -DH5_USE_16_API"	         # common FORTRAN flags
	F77_CFH="" 		         # FORTRAN callable from C w/ cfortran.h
	F77_C_CFH="$F77_CFH"	         # calling C w/ cfortran.h
	CFH_F77="$F77_C_CFH"	         # old version of F77_C_CFH
	F77_C_LIB=""		         # C lib called by FORTRAN main
	HDFSYS=IBM6000		         # system type as defined by HDF5
	MACHINE=IBM		         # system type as defined by HDFEOS5
	;;

    linux | linux32 | linux64)

	#CC="gcc -m32"  		        # C compiler
	CC="gcc $LNX_COMP_FLAG"  		# C compiler
	CFLAGS="$opt_flag $ts_flag -ansi -DH5_USE_16_API"       # default C flags
	C_CFH="-Df2cFortran"	         # C w/ cfortran.h callable from FORTRAN
	CFHFLAGS="$CFLAGS $C_CFH"        # CFLAGS + C_CFH
	C_F77_CFH="$C_CFH"	         # calling FORTRAN
	C_F77_LIB=""		         # FORTRAN lib called by C main
	#F77="g77 -m32"			 # FORTRAN compiler
	F77="g77 $LNX_COMP_FLAG"			 # FORTRAN compiler
	F77FLAGS="$opt_flag -DH5_USE_16_API"	         # common FORTRAN flags
	F77_CFH=""		         # FORTRAN callable from C w/ cfortran.h
	F77_C_CFH="$F77_CFH"	         # calling C w/ cfortran.h
	CFH_F77="$F77_C_CFH"	         # old version of F77_C_CFH
	F77_C_LIB=""		         # C lib called by FORTRAN main
        if [ "`uname -m`" = "x86_64" ] ; then
	    if [ "$LNX_COMP_FLAG" = "-m32" ] ; then
		HDFSYS=LINUX		# system type as defined by HDF5
	    else
		HDFSYS=LINUX64       # Only for 64 bit linux
	    fi
	elif [ "`uname -m`" = "ia64" ] ; then
	    if [ "$LNX_COMP_FLAG" = "-m32" ] ; then
		HDFSYS=LINUX		# system type as defined by HDF5
	    else
		HDFSYS=IA64          #Only for IA64
	    fi
        else
	   HDFSYS=LINUX		# system type as defined by HDF
	fi
	MACHINE=LINUX		         # system type as defined by HDFEOS5
	;;

    macintosh)
        CC=gcc                    # C compiler
        CFLAGS="$opt_flag -ansi -D_ANSI_SOURCE -DH5_USE_16_API"  # default C flags (optimize, ansi)
        C_CFH="-Df2cFortran"      # C w/ cfortran.h callable from FORTRAN
        CFHFLAGS="$CFLAGS $C_CFH" # CFLAGS + C_CFH
        C_F77_CFH="$C_CFH"        # calling FORTRAN
        C_F77_LIB=""              # FORTRAN lib called by C main
        F77=g77                   # FORTRAN compiler
        F77FLAGS="$opt_flag -DH5_USE_16_API"      # common FORTRAN flags
        F77_CFH=""                # FORTRAN callable from C w/ cfortran.h
        F77_C_CFH="$F77_CFH"      # calling C w/ cfortran.h
        CFH_F77="$F77_C_CFH"      # old version of F77_C_CFH
        F77_C_LIB=""              # C lib called by FORTRAN main
        HDFSYS=MACINTOSH          # system type as defined by HDF5
        MACHINE=MACINTOSH         # system type as defined by HDFEOS5
        ;;

    macintel)
        CC=gcc                    # C compiler
        CFLAGS="$opt_flag -ansi -D_ANSI_SOURCE -DH5_USE_16_API"  # default C flags (optimize, ansi)
        C_CFH="-Df2cFortran"      # C w/ cfortran.h callable from FORTRAN
        CFHFLAGS="$CFLAGS $C_CFH" # CFLAGS + C_CFH
        C_F77_CFH="$C_CFH"        # calling FORTRAN
        C_F77_LIB=""              # FORTRAN lib called by C main
        F77=gfortran              # FORTRAN compiler
        F77FLAGS="$opt_flag -DH5_USE_16_API"      # common FORTRAN flags
        F77_CFH=""                # FORTRAN callable from C w/ cfortran.h
        F77_C_CFH="$F77_CFH"      # calling C w/ cfortran.h
        CFH_F77="$F77_C_CFH"      # old version of F77_C_CFH
        F77_C_LIB=""              # C lib called by FORTRAN main
        HDFSYS=MACINTEL           # system type as defined by HDF5
        MACHINE=MACINTEL          # system type as defined by HDFEOS5
        ;;

    cygwin)
        CC=gcc                  # C compiler
        CFLAGS="$opt_flag -ansi -DH5_USE_16_API" # default C flags (optimize, ansi)
        C_CFH="-Df2cFortran"    # C w/ cfortran.h callable from FORTRAN
        CFHFLAGS="$CFLAGS $C_CFH" # CFLAGS + C_CFH
        C_F77_CFH="$C_CFH"      # calling FORTRAN
        C_F77_LIB=""            # FORTRAN lib called by C main
        F77=g77                 # FORTRAN compiler
        F77FLAGS="$opt_flag -DH5_USE_16_API"    # common FORTRAN flags
        F77_CFH=""              # FORTRAN callable from C w/ cfortran.h
        F77_C_CFH="$F77_CFH"    # calling C w/ cfortran.h
        CFH_F77="$F77_C_CFH"    # old version of F77_C_CFH
        F77_C_LIB=""            # C lib called by FORTRAN main
        RANLIB=ranlib		# activate the ranlib command
	HDFSYS=CYGWIN           # system type as defined by HDF
        MACHINE=CYGWIN          # system type as defined by HDFEOS
        ;;

    sco)
	CC=cc  			  # C compiler
	CFLAGS="$opt_flag -posix -DH5_USE_16_API" # default C flags (optimize, ansi)
	C_CFH="-Df2cFortran"	  # C w/ cfortran.h callable from FORTRAN
	CFHFLAGS="$CFLAGS $C_CFH" # CFLAGS + C_CFH
	C_F77_CFH="$C_CFH"	  # calling FORTRAN
	C_F77_LIB=""		  # FORTRAN lib called by C main
	F77=""		 	  # FORTRAN compiler
	F77FLAGS="$opt_flag -DH5_USE_16_API"	  # common FORTRAN flags
	F77_CFH=""		  # FORTRAN callable from C w/ cfortran.h
	F77_C_CFH="$F77_CFH"	  # calling C w/ cfortran.h
	CFH_F77="$F77_C_CFH"	  # old version of F77_C_CFH
	F77_C_LIB=""		  # C lib called by FORTRAN main
	HDFSYS=SCO		  # system type as defined by HDF5
	MACHINE=SCO		  # system type as defined by HDFEOS5
	;;

    sgi32)
	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 $ts_flag -DH5_USE_16_API"# default C flags
	C_CFH=""	 	        # C w/ cfortran.h callable from FORTRAN
	CFHFLAGS="$CFLAGS $C_CFH"       # CFLAGS + C_CFH
	C_F77_CFH="$C_CFH"	        # calling FORTRAN
	C_F77_LIB="-lI77 -lU77 -lF77"   # FORTRAN lib called by C main
	F77FLAGS="$opt_flag -DH5_USE_16_API"	        # common FORTRAN flags
	F77_CFH=""		        # FORTRAN callable from C w/ cfortran.h
	F77_C_CFH="$F77_CFH"	        # calling C w/ cfortran.h
	CFH_F77="$F77_C_CFH"	        # old version of F77_C_CFH
	F77_C_LIB=""		        # C lib called by FORTRAN main
	HDFSYS=IRIS4		        # system type as defined by HDF5
	MACHINE=SGI		        # system type as defined by HDFEOS5
	;;

    sgi64)
	cpu_type=`hinv | fgrep CPU | head -1 | cut -d' ' -f3 | cut -b2`
	if [ "$cpu_type" = "4" ] ; then
	    CC="cc -64 -mips3"		# C compiler (R4?00 chip)
	    F77="f77 -64 -mips3"	# FORTRAN compiler (R4?00 chip)
        else
            CC="cc -64"      		# C compiler
            F77="f77 -64"    		# FORTRAN compiler
        fi
	CFLAGS="$opt_flag -xansi -D_POSIX_SOURCE $ts_flag -DH5_USE_16_API"   # default C flags
	C_CFH=""	 	        # C w/cfortran.h callable from FORTRAN
	CFHFLAGS="$CFLAGS $C_CFH"       # CFLAGS + C_CFH
	C_F77_CFH="$C_CFH"	        # calling FORTRAN
	C_F77_LIB="-lI77 -lU77 -lF77"   # FORTRAN lib called by C main
	F77FLAGS="$opt_flag -DH5_USE_16_API"	        # common FORTRAN flags
	F77_CFH=""		        # FORTRAN callable from C w/ cfortran.h
	F77_C_CFH="$F77_CFH"	        # calling C w/ cfortran.h
	CFH_F77="$F77_C_CFH"	        # old version of F77_C_CFH
	F77_C_LIB=""		        # C lib called by FORTRAN main
	HDFSYS=IRIS4	 	        # system type as defined by HDF5
	MACHINE=SGI		        # system type as defined by HDFEOS5
	;;

    sun5)
	CC=cc			        # C compiler
	CFLAGS="$opt_flag -Xa $ts_flag -DH5_USE_16_API" # default C flags
	C_CFH="-DsunFortran"	        # C w/ cfortran.h callable from FORTRAN
	CFHFLAGS="$CFLAGS $C_CFH"       # CFLAGS + C_CFH
	C_F77_CFH="$C_CFH"	        # calling FORTRAN
	C_F77_LIB=""		        # FORTRAN lib called by C main
	F77=f77 		        # FORTRAN compiler
	F77FLAGS="$opt_flag -DH5_USE_16_API"	        # common FORTRAN flags
	F77_CFH=""		        # FORTRAN callable from C w/ cfortran.h
	F77_C_CFH="$F77_CFH" 	        # calling C w/ cfortran.h
	CFH_F77="$F77_C_CFH"	        # old version of F77_C_CFH
	F77_C_LIB="-lm" 	        # C lib called by FORTRAN main
	HDFSYS=SUN		        # system type as defined by HDF5
	MACHINE=SUN5		        # system type as defined by HDFEOS5
	NSL_FLAG="-lnsl"	        # this is nil on all but Sun platforms
	NSL_LIB="/usr/lib/libnsl.a"	# this is nil on all but Sun platforms
	;;

    sun5.8)
	CC=cc			        # C compiler
	CFLAGS="$opt_flag $ts_flag -Xa -DH5_USE_16_API" # default C flags
	C_CFH="-DsunFortran"	        # C w/ cfortran.h callable from FORTRAN
	CFHFLAGS="$CFLAGS $C_CFH"       # CFLAGS + C_CFH
	C_F77_CFH="$C_CFH"	        # calling FORTRAN
	C_F77_LIB=""		        # FORTRAN lib called by C main
	F77=f77 		        # FORTRAN compiler
	F77FLAGS="$opt_flag -DH5_USE_16_API"	        # common FORTRAN flags
	F77_CFH=""		        # FORTRAN callable from C w/ cfortran.h
	F77_C_CFH="$F77_CFH" 	        # calling C w/ cfortran.h
	CFH_F77="$F77_C_CFH"	        # old version of F77_C_CFH
	F77_C_LIB="-lm" 	        # C lib called by FORTRAN main
	HDFSYS=SUN		        # system type as defined by HDF5
	MACHINE=SUN8		        # system type as defined by HDFEOS5
	NSL_FLAG="-lnsl"	        # this is nil on all but Sun platforms
	NSL_LIB="/usr/lib/libnsl.a"	# this is nil on all but Sun platforms
	;;

    sun5.9)
	CC=cc			        # C compiler
	CFLAGS="$opt_flag $ts_flag -Xa -DH5_USE_16_API" # default C flags
	C_CFH="-DsunFortran"	        # C w/ cfortran.h callable from FORTRAN
	CFHFLAGS="$CFLAGS $C_CFH"       # CFLAGS + C_CFH
	C_F77_CFH="$C_CFH"	        # calling FORTRAN
	C_F77_LIB=""		        # FORTRAN lib called by C main
	F77=f77 		        # FORTRAN compiler
	F77FLAGS="$opt_flag -DH5_USE_16_API"	        # common FORTRAN flags
	F77_CFH=""		        # FORTRAN callable from C w/ cfortran.h
	F77_C_CFH="$F77_CFH" 	        # calling C w/ cfortran.h
	CFH_F77="$F77_C_CFH"	        # old version of F77_C_CFH
	F77_C_LIB="-lm" 	        # C lib called by FORTRAN main
	HDFSYS=SUN		        # system type as defined by HDF5
	MACHINE=SUN9		        # system type as defined by HDFEOS5
	NSL_FLAG="-lnsl"	        # this is nil on all but Sun platforms
	NSL_LIB="/usr/lib/libnsl.a"	# this is nil on all but Sun platforms
	;;

    sun5.10)
	CC=cc			        # C compiler
	CFLAGS="$opt_flag $ts_flag -Xa -DH5_USE_16_API" # default C flags
	C_CFH="-DsunFortran"	        # C w/ cfortran.h callable from FORTRAN
	CFHFLAGS="$CFLAGS $C_CFH"       # CFLAGS + C_CFH
	C_F77_CFH="$C_CFH"	        # calling FORTRAN
	C_F77_LIB=""		        # FORTRAN lib called by C main
	F77=f77 		        # FORTRAN compiler
	F77FLAGS="$opt_flag -DH5_USE_16_API"	        # common FORTRAN flags
	F77_CFH=""		        # FORTRAN callable from C w/ cfortran.h
	F77_C_CFH="$F77_CFH" 	        # calling C w/ cfortran.h
	CFH_F77="$F77_C_CFH"	        # old version of F77_C_CFH
	F77_C_LIB="-lm" 	        # C lib called by FORTRAN main
	HDFSYS=SUN		        # system type as defined by HDF5
	MACHINE=SUN10		        # system type as defined by HDFEOS5
	NSL_FLAG="-lnsl"	        # this is nil on all but Sun platforms
	NSL_LIB="/usr/lib/libnsl.a"	# this is nil on all but Sun platforms
	;;

  winnt98 )
    CPP=CC                  # C++ compiler
    CC=gcc                  # C compiler
    CFLAGS="$opt_flag -O3 -Wall -W -Wundef -Wno-switch -DH5_USE_16_API"     # 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 -DH5_USE_16_API"     # 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 -DH5_USE_16_API"    # common FORTRAN flags
    HDFSYS=CYGWIN           # system type as defined by HDF
    ;;

    *)
	CC=cc			  # C compiler
	CFLAGS="$opt_flag -DH5_USE_16_API" 	  # default C flags (optimize)
	C_CFH=""	          # C w/ cfortran.h callable from FORTRAN
	CFHFLAGS="$CFLAGS $C_CFH" # CFLAGS + C_CFH
	C_F77_CFH="$C_CFH"	  # calling FORTRAN
	C_F77_LIB=""		  # FORTRAN lib called by C main
	F77=f77 		  # FORTRAN compiler
	F77FLAGS="$opt_flag -DH5_USE_16_API"	  # common FORTRAN flags
	F77_CFH=""	 	  # FORTRAN callable from C w/ cfortran.h
	F77_C_CFH="$F77_CFH" 	  # calling C w/ cfortran.h
	CFH_F77="$F77_C_CFH"	  # old version of F77_C_CFH
	F77_C_LIB="-lm" 	  # C lib called by FORTRAN main
	HDFSYS=unknown		  # system type as defined by HDF5
	MACHINE=unknown		  # system type as defined by HDFEOS5
	;;
esac
export NSL_FLAG NSL_LIB AR RANLIB RANLIBSUN4
export CC CFLAGS C_CFH CFHFLAGS C_F77_CFH C_F77_LIB F77
export F77FLAGS F77_CFH F77_C_CFH CFH_F77 F77_C_LIB HDFSYS

#
# Verify that this is the HDFEOS5 home directory before proceeding
# If OK, then set the value of HDFEOS5_HOME, else quit.

dirs="bin include lib obj src"
ok=1

for dir in $dirs ; do

    if [ ! -d $dir ] ; then
        ok=0
        break
    fi

done

if [ $ok = 1 ] ; then

    HDFEOS5_HOME=`pwd`

else

    WriteError "You must first go to the HDFEOS5 home directory."
    Help

fi

dirs="bin lib obj"
for dir in $dirs ; do

    if [ ! -d $dir/$BRAND ] ; then
	mkdir $dir/$BRAND
    fi

done

#
# For linux create soft links for backward compatibility with 
#                 older versions of the hdfeos5
#
    if [ "$BRAND" = "linux64" ] ; then
	    
	echo "64-bit structure is the default in this machine. Creating linux as soft link to linux64 in bin, lib, and obj directories."

	dirs="bin lib obj"
	for dir in $dirs ; do
# back up existing linux diretory
	    if [ -d $dir/linux ] ; then
		/bin/mv  $dir/linux $dir/linux_backup
	    fi
	    ln -s ./$BRAND $dir/linux
	done
    fi

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

	echo "32-bit structure is the default in this machine. Creating linux32 as soft link to linux in bin, lib, and obj directories."

	dirs="bin lib obj"
	for dir in $dirs ; do
# back up existing linux32 diretory
	    if [ -d $dir/linux32 ] ; then
		/bin/mv  $dir/linux32 $dir/linux32_backup
	    fi
	    ln -s ./$BRAND $dir/linux32
	done
    fi

#
# set HDFEOS5-related environment variables
# these may be referred to in makefiles and on compiler command lines
#

if [ "$HDFEOS5_HOME" != "" ] ; then

# set up base set of HDFEOS5 directory variables.

    HDFEOS5_BIN=${HDFEOS5_HOME}/bin/$BRAND	# executable files
    HDFEOS5_INC=$HDFEOS5_HOME/include		# include header files
    HDFEOS5_LIB=${HDFEOS5_HOME}/lib/$BRAND  	# library files
    HDFEOS5_OBJ=${HDFEOS5_HOME}/obj/$BRAND	# object files
    HDFEOS5_SRC=$HDFEOS5_HOME/src		# HDFEOS5 source files

    export HDFEOS5_HOME HDFEOS5_BIN HDFEOS5_DAT HDFEOS5_INC HDFEOS5_LIB 
    export HDFEOS5_MSG  HDFEOS5_OBJ HDFEOS5_RUN HDFEOS5_SRC HDFEOS5_TST

# update path variables

    PATH=$PATH:$HDFEOS5_BIN; export PATH	# add HDFEOS5_BIN to path

else

    echo "You must first set the environment variable HDFEOS5_HOME"
    exit 1

fi


# 
# set up environment to handle FORTRAN-90 compiler
#

if [ "$pgs_f90_comp" != "" ] ; then		# using FORTRAN-90

    F77="$pgs_f90_comp"

    if [ "$pgs_nag_flag" = "1" ] ; then		# using NAG f90
        C_CFH="$C_CFH -DNAGf90F"
        CFHFLAGS="$CFLAGS $C_CFH"
    fi

    export CFHFLAGS C_CFH F77

fi

#
# Ouput a banner
#

Banner

#
# Get the HDF5 and SZIP library and include directories from the user
#

GetHdfEnv
GetSzipEnv

#
# Set up the architecture-dependent environment scripts
#

SetupEnvScripts


#
# Run the architecture-dependent installation commands
#

MAKECMD=make; export MAKECMD 		# force use of regular make
					# ClearMake doesn't work with
					# current makefile

: ${MAKECMD:=make}			# the make command defaults to 'make'

					# augment path if using clearmake
if [ "`echo $MAKECMD | grep clearmake`" != "" ] ; then
    echo "$this_script : note : make command is set to clearmake"
    PATH="$PATH:/usr/atria/bin" 
    export PATH
fi

cd $HDFEOS5_SRC

echo ""
echo ""
echo "-----------------------------------------------------------------------"
echo ""
echo "Configuring the GCTP library"
echo ""
echo "-----------------------------------------------------------------------"
echo ""

make_file="-f Makefile"		        # default makefile

case $BRAND in

  dec)
    echo cp $HDFEOS5_LIB/../tmp/geolibDEC.a $HDFEOS5_LIB/libGctp.a
    cp $HDFEOS5_LIB/../tmp/geolibDEC.a $HDFEOS5_LIB/libGctp.a
    ;;

  hp)
    case $OS_VERSION in
    10)
       echo cp $HDFEOS5_LIB/../tmp/geolibHP.a $HDFEOS5_LIB/libGctp.a
       cp $HDFEOS5_LIB/../tmp/geolibHP.a $HDFEOS5_LIB/libGctp.a 
     ;;
    11)
       echo cp $HDFEOS5_LIB/../tmp/geolibHP11.a $HDFEOS5_LIB/libGctp.a
       cp $HDFEOS5_LIB/../tmp/geolibHP11.a $HDFEOS5_LIB/libGctp.a 
     ;;
    esac
    ;;

  ibm)
    echo cp $HDFEOS5_LIB/../tmp/geolibIBM.a $HDFEOS5_LIB/libGctp.a
    cp $HDFEOS5_LIB/../tmp/geolibIBM.a $HDFEOS5_LIB/libGctp.a
    ;;

  sgi32)
    case $OS_VERSION in
    2)
       echo cp $HDFEOS5_LIB/../tmp/geolibIRIX62-n32.a $HDFEOS5_LIB/libGctp.a
       cp $HDFEOS5_LIB/../tmp/geolibIRIX62-n32.a $HDFEOS5_LIB/libGctp.a
     ;;
    5)
       echo cp $HDFEOS5_LIB/../tmp/geolibIRIX65-n32.a $HDFEOS5_LIB/libGctp.a
       cp $HDFEOS5_LIB/../tmp/geolibIRIX65-n32.a $HDFEOS5_LIB/libGctp.a
    ;;
    esac
    ;;

  sgi64)
    case $OS_VERSION in
    2)
       if [ "$cpu_type" = "4" ] ; then
	  echo cp $HDFEOS5_LIB/../tmp/geolibIRIX62-64mips3.a $HDFEOS5_LIB/libGctp.a
	  cp $HDFEOS5_LIB/../tmp/geolibIRIX62-64mips3.a $HDFEOS5_LIB/libGctp.a 
       else
	  echo cp $HDFEOS5_LIB/../tmp/geolibIRIX62-64.a $HDFEOS5_LIB/libGctp.a
	  cp $HDFEOS5_LIB/../tmp/geolibIRIX62-64.a $HDFEOS5_LIB/libGctp.a
       fi
     ;;
    5)
       echo cp $HDFEOS5_LIB/../tmp/geolibIRIX65-64.a $HDFEOS5_LIB/libGctp.a
       cp $HDFEOS5_LIB/../tmp/geolibIRIX65-64.a $HDFEOS5_LIB/libGctp.a
    ;;
    esac
    ;;

  sun5)
       echo cp $HDFEOS5_LIB/../tmp/geolibSOL24.a $HDFEOS5_LIB/libGctp.a
       cp $HDFEOS5_LIB/../tmp/geolibSOL24.a $HDFEOS5_LIB/libGctp.a
    ;;

  sun5.8)
       echo cp $HDFEOS5_LIB/../tmp/geolibSOL28.a $HDFEOS5_LIB/libGctp.a
       cp $HDFEOS5_LIB/../tmp/geolibSOL28.a $HDFEOS5_LIB/libGctp.a
    ;;

  sun5.9)
       echo cp $HDFEOS5_LIB/../tmp/geolibSOL29.a $HDFEOS5_LIB/libGctp.a
       cp $HDFEOS5_LIB/../tmp/geolibSOL29.a $HDFEOS5_LIB/libGctp.a
    ;;

  sun5.10)
       echo cp $HDFEOS5_LIB/../tmp/geolibSOL210.a $HDFEOS5_LIB/libGctp.a
       cp $HDFEOS5_LIB/../tmp/geolibSOL210.a $HDFEOS5_LIB/libGctp.a
    ;;

  linux | linux32 | linux64)
    if [ "`uname -m`" = "x86_64" ] ; then
	if [ "$LNX_COMP_FLAG" = "-m32" ] ; then
	    echo cp $HDFEOS5_LIB/../tmp/geolibLINUX.a $HDFEOS5_LIB/libGctp.a
	    cp $HDFEOS5_LIB/../tmp/geolibLINUX.a $HDFEOS5_LIB/libGctp.a
	else	    
	    echo cp $HDFEOS5_LIB/../tmp/geolibLINUX64.a $HDFEOS5_LIB/libGctp.a
	    cp $HDFEOS5_LIB/../tmp/geolibLINUX64.a $HDFEOS5_LIB/libGctp.a
	fi
    elif [ "`uname -m`" = "ia64" ] ; then
	if [ "$LNX_COMP_FLAG" = "-m32" ] ; then
	    echo cp $HDFEOS5_LIB/../tmp/geolibLINUX.a $HDFEOS5_LIB/libGctp.a
	    cp $HDFEOS5_LIB/../tmp/geolibLINUX.a $HDFEOS5_LIB/libGctp.a
	else
	    echo cp $HDFEOS5_LIB/../tmp/geolibLINUXIA64.a $HDFEOS5_LIB/libGctp.a
	    cp $HDFEOS5_LIB/../tmp/geolibLINUXIA64.a $HDFEOS5_LIB/libGctp.a
	fi
    else
	echo cp $HDFEOS5_LIB/../tmp/geolibLINUX.a $HDFEOS5_LIB/libGctp.a
	cp $HDFEOS5_LIB/../tmp/geolibLINUX.a $HDFEOS5_LIB/libGctp.a
    fi
    ;;

  macintosh)
    echo cp $HDFEOS5_LIB/../tmp/geolibMAC.a $HDFEOS5_LIB/libGctp.a
    cp $HDFEOS5_LIB/../tmp/geolibMAC.a $HDFEOS5_LIB/libGctp.a
    ;;

  macintel)
    echo cp $HDFEOS5_LIB/../tmp/geolibMACIntel.a $HDFEOS5_LIB/libGctp.a
    cp $HDFEOS5_LIB/../tmp/geolibMACIntel.a $HDFEOS5_LIB/libGctp.a
    ;;

  cygwin)
    echo cp $HDFEOS5_LIB/../tmp/geolibCYGWIN.a $HDFEOS5_LIB/libGctp.a
    cp $HDFEOS5_LIB/../tmp/geolibCYGWIN.a $HDFEOS5_LIB/libGctp.a
    ;;

  winnt98)
    echo cp $HDFEOS5_LIB/../tmp/geolibCYGWIN.a $HDFEOS5_LIB/libGctp.a
    cp $HDFEOS5_LIB/../tmp/geolibCYGWIN.a $HDFEOS5_LIB/libGctp.a
    ;;

  *)
    echo '!!!!!! WARNING !!!!!! WARNING !!!!!! WARNING !!!!! WARNING !!!!!!'
    echo '"'$BRAND'"' is not officially supported, no GCTP library available
    echo for this mode '"'$BRAND'"'.  HDF-EOS5 grid routines will not work
    echo without this library.
    ;;

esac

if [ -f $HDFEOS5_LIB/libGctp.a ] ; then
    chmod u+w $HDFEOS5_LIB/libGctp.a
fi

#
# check to see if valid C compiler path was specified
#
    
cc=""
    
if [ "$cc_path" != ""  ] ; then
    
    if [ -f $cc_path ] ; then
    
            cc=`basename $cc_path`
            cc_path=`echo $cc_path | sed 's%/[^/]*/*$%%'`
	    case "$BRAND" in
		"sgi32" )
		    CC="$cc_path/$cc -n32"
		;;

		"sgi64" )
		    cpu_type=`hinv | fgrep CPU | head -1 | cut -d' ' -f3 | cut -b2`
		    if [ "$cpu_type" == "4" ] ; then
			CC="$cc_path/$cc -64 -mips3"
		    else
			CC="$cc_path/$cc -64 -mips4"
		    fi
		;;

		* )
		    CC="$cc_path/$cc" 
		;; 
	    esac

	    export CC		# user overrides C compiler name
    fi
    
    if [ -d $cc_path ] ; then	         # make sure cc_path is now a directory
         PATH=${cc_path}:${PATH} 	 # prepend cc_path to search path
         export PATH
    else
         cc_path=""
         echo "Warning: C compiler directory $cc_path not found."
         echo  "HDFEOS5 build may fail."
    fi
fi

cat /dev/null > ../include/HE5_config.h


echo ""
echo ""
echo "-----------------------------------------------------------------------"
echo ""
echo "Building HDFEOS5"
echo ""
echo "-----------------------------------------------------------------------"
echo ""

$MAKECMD $make_file
if [ $? != 0 ] ; then
    ErrorExit "$MAKECMD $make_file"
fi

if [ "$BRAND" = "macintosh" ] ; then
    ranlib -s $HDFEOS5_LIB/libGctp.a
    ranlib -s $HDFEOS5_LIB/libhe5_hdfeos.a
fi

if [ "$BRAND" = "macintel" ] ; then
    ranlib -s $HDFEOS5_LIB/libGctp.a
    ranlib -s $HDFEOS5_LIB/libhe5_hdfeos.a
fi

if [ $BRAND = "cygwin" ] ; then
    ranlib $HDFEOS5_LIB/libGctp.a
    ranlib $HDFEOS5_LIB/libhe5_hdfeos.a
fi

echo ""
echo ""
echo "-----------------------------------------------------------------------"
echo ""
echo "Cleaning up"
echo ""
echo "-----------------------------------------------------------------------"
echo ""

$MAKECMD $make_file clean
if [ $? != 0 ] ; then
    ErrorExit "$MAKECMD $make_file clean"
fi


echo ""
echo ""
echo "-----------------------------------------------------------------------"
echo ""
echo "Building HDFEOS5 utilities"
echo ""
echo "-----------------------------------------------------------------------"
echo ""

    HDFEOS5_UTIL=${HDFEOS5_HOME}/util	# utility files
    cd $HDFEOS5_UTIL
    make_utilfile="-f makefile"		# default utility makefile
    $MAKECMD $make_utilfile
    if [ $? != 0 ] ; then
	ErrorExit "$MAKECMD $make_utilfile"
    fi

#    cp HE5_GDconvert_ij2ll $HDFEOS5_BIN/HE5_GDconvert_ij2ll
#    /bin/rm $HDFEOS5_UTIL/HE5_GDconvert_ij2ll
    chmod u+wx $HDFEOS5_BIN/HE5_GDconvert_ij2ll

# done

echo ""
echo ""
echo "-----------------------------------------------------------------------"
echo ""
echo "    HDFEOS5 installation ending at `date`"
echo ""
echo "-----------------------------------------------------------------------"














