#! /bin/sh
#-----------------------------------------------------------------------------
# filename:
# 	INSTALL-HDF4.2r6
#
# description:
# 	Installation script module for installing HDF 4.2 release 6
# 	for the SDP (PGS) Toolkit and hdfeos.  
#       It implements patches to the NCSA 
# 	installation procedure to allow for trouble-free installation on
# 	the platforms supported by the SDP (PGS) Toolkit and hdfeos.
#
# usage:
# 	Called from INSTALL-HDF
#
# 	INSTALL-HDF4.2r6 [-dbug]
# 	INSTALL-HDF4.2r6 -h[elp]
#
# 	  -h      : display this help message and exit
# 	  -dbug   : install debug version of HDF
#
# notes:
# 	This is NOT a standalone script.  It is only intended to be run
# 	by INSTALL-HDF, the HDF installation script.
# 
# author:
#        Mike Sucher / Applied Research Corp.
#        
# history:
#       23-Apr-1996 MES Initial version (file: INSTALL-HDF4.0r1)
# 	26-Apr-1996 MES Updated PreConfigure compiler logic.
# 	02-Jul-1996 MES Updated to handle SunOS 5.5
# 	19-Aug-1996 MES Updated for HDF4.0r2 (file: INSTALL-HDF4.0r2)
# 	23-Sep-1996 MES Added patch to hdfi.h on dec to avoid potential
# 			conflict with <rpc/types.h>.
#       19-Aug-1996 MES Updated for HDF4.1r3 (file: INSTALL-HDF4.1r3)
#       05-Oct-2000 AT  for machine HP-11 the xdr library libnsl.a needs to be
#                       loaded as for the SOLARIS. Added code to edit mh-hpux
#                       file in config directory to include the load flag -lnsl
#      15-Aug-2001  AA  Updated for HDF4.1r5 (file: INSTALL-HDF4.1r5)
#      16-Mar-2004  AA  Updated for HDF4.2r0 (file: INSTALL-HDF4.2r0)
#       8-Sep-2005  AT  Updated for HDF4.2r1 (file: INSTALL-HDF4.2r1)
#       5-Jul-2007  AT  Updated for HDF4.2r2 (file: INSTALL-HDF4.2r2)
#       9-Jan-2008  AT  Updated for HDF4.2r3 (file: INSTALL-HDF4.2r3)
#       9-MAY-2009  AT  Updated for HDF4.2r4 (file: INSTALL-HDF4.2r4)
#      20-Jul-2010  AT  Updated for hdf-4.2.5 (file: INSTALL-HDF4.2r5)
#      20-Jul-2010  AT  Updated for hdf-4.2.6 (file: INSTALL-HDF4.2r6)
#
#-----------------------------------------------------------------------------

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

start_dir=`pwd`
start_path=$PATH
this_script="`basename $0`"
script_error="$this_script: Error:"

#
# Get absolute pathname where this script is located
# This allows the script to locate any needed auxilliary files
#

script_dir="`dirname $0`"
abs="`echo $script_dir | cut -c1`"
if [ "$abs" != "/" ] ; then
    script_dir=`cd $script_dir;pwd`
fi


#****************************************************************
#                                                               *
#                   * * * Functions  * * *                      *
#                                                               *
#****************************************************************

#
# Function to output error message
#

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


#
# Function to output message to logfile and console
#

EchoAndLog()
{
    if [ "$1" = "-n" ] ; then		# -n: suppress leading blank line
        shift
    else
        echo ""
        if [ "$log_file" != "" ] ; then
            echo "" >> $log_file
        fi
    fi
    
    echo "$*"
    if [ "$log_file" != "" ] ; then
         echo "$*" >> $log_file
    fi
}


#
# Function to 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 silently emulate the 'which' command 
#

Which()
{
    if [ "$1" = "" ] ; then 
        WriteError "Which() : Too few arguments." 
        exit 1 
    fi

    saveifs="$IFS"
    IFS=:
    which_result=0
    which_cmd=""
    
    for dir in $PATH;  do 
        if [ -x $dir/$1 ] ; then 
            which_result=1 
            which_cmd=$dir/$1
            break 
        fi 
    done
    
    IFS="$saveifs"
}

PreConfigJPEGFixes_dec()
{
    cd $start_dir

    EchoAndLog "Pre-configure JPEG fixes for $BRAND ... "
    echo " "
    file=$start_dir/configure
    jpeglib=$JPEGLIB/libjpeg.a

    if [ ! -f $file.ncsa.default ] ; then
	mv  $file $file.ncsa.default
    fi

    sed 's^-ljpeg ^$JPEGLIB/libjpeg.a ^g' $file.ncsa.default > $file
    /bin/chmod 0755 $file
    /bin/rm -f $file.ncsa.default

    cd $start_dir
}

#
# Function to implement debug version of hdf libraries
#

PreConfigFixes_dbug()
{
    cd $start_dir

    EchoAndLog "Pre-configure for debug ... "

    #
    # Despite overrides in Makefile.in, the configure script steadfastly
    # tries to change the compilers.  To get around this, we edit the
    # file config/<arch-file> to comment out the offending lines that get
    # get included in the Makefile generated by configure.
    #

    cd $start_dir/config

    case $BRAND in

        cray  ) 
		file=mh-unicos;
		c_opt='-O'; c_dbug='-g';
		f_opt='-O'; f_dbug='-g';
		;;
    
        dec   ) 
		file=mh-alpha;
		c_opt='-Olimit 2048'; c_dbug='-g';
		f_opt='='; f_dbug='= -g';
		
		;;
    
        hp    ) 
		file=mh-hpux;
		c_opt='-O'; c_dbug='-g';
		f_opt='-O'; f_dbug='-g';
		;;
    
        ibm   ) 
		file=mh-aix;
		c_opt='-O'; c_dbug='-g';
		f_opt='-O'; f_dbug='-g';
		;;
    
        linux ) 
		file=mh-linux;
		c_opt='-ansi'; c_dbug='-ansi -g';
		f_opt='='; f_dbug='= -g';
		;;
        
        linux32 ) 
		file=mh-linux;
		c_opt='-ansi'; c_dbug='-ansi -g';
		f_opt='='; f_dbug='= -g';
		;;
        
        linux64 ) 
		file=mh-linux;
		c_opt='-ansi'; c_dbug='-ansi -g';
		f_opt='='; f_dbug='= -g';
		;;
    
        macintosh )
                file=mh-mac;
                c_opt='-ansi'; c_dbug='-ansi -g';
                f_opt='='; f_dbug='= -g';
                ;;
        
        macintel )
                file=mh-mac;
                c_opt='-ansi'; c_dbug='-ansi -g';
                f_opt='='; f_dbug='= -g';
                ;;
       
        macintel32 )
                file=mh-mac;
                c_opt='-ansi'; c_dbug='-ansi -g';
                f_opt='='; f_dbug='= -g';
                ;;
       
        macintel64 )
                file=mh-mac;
                c_opt='-ansi'; c_dbug='-ansi -g';
                f_opt='='; f_dbug='= -g';
                ;;

        cygwin )
                file=mh-cygwin;
                c_opt='-ansi'; c_dbug='-ansi -g';
                f_opt='='; f_dbug='= -g';
                ;;
    
        sgi   ) 
		file=mh-irix32;
		if [ "$OSTYPE" = "IRIX" ] ; then
		    file=mh-irix5;
		fi
		c_opt='-O'; c_dbug='-g';
		f_opt='-O'; f_dbug='-g';
		;;
    
        sgi32 )
		file=mh-irix32;
		c_opt='-O' c_dbug='-g';
		f_opt='-O' f_dbug='-g';
		;;

        sgi64 )
		file=mh-irix6;
		c_opt='-O'; c_dbug='-g';
		f_opt='-O'; f_dbug='-g';
		;;
    
        sun4  )
		file=mh-sun; c_opt='-O';
		c_opt='-O'; c_dbug='-g';
		f_opt='-O'; f_dbug='-g';
		;;
    
        sun5  )
		file=mh-solaris;
		c_opt='-xO2'; c_dbug='-g';
		f_opt='-O'; f_dbug='-g';
		;;

       sun5.8  )
		file=mh-solaris;
		c_opt='-xO2'; c_dbug='-g';
		f_opt='-O'; f_dbug='-g';
		;;
    
       sun5.9  )
		file=mh-solaris;
		c_opt='-xO2'; c_dbug='-g';
		f_opt='-O'; f_dbug='-g';
		;;
    
       sun5.10  )
		file=mh-solaris;
		c_opt='-xO2'; c_dbug='-g';
		f_opt='-O'; f_dbug='-g';
		;;
    
        *     ) WriteError "Architecture $BRAND is not supported."
                exit 1 ;;
    
    esac

    if [ ! -f $file.ncsa.default ] ; then
        mv  $file $file.ncsa.default
    fi
    
    cat $file.ncsa.default |
    sed "/^CFLAGS/s/$c_opt/ $c_dbug/" |
    sed "/^FFLAGS/s/$f_opt/ $f_dbug/" |
    cat > $file.ncsa
    
    mv $file.ncsa $file


    cd $start_dir
}



#
# Function to implement pre-configure installation fixes for linux
#

PreConfigFixes_linux()
{
    cd $start_dir

    EchoAndLog "Pre-configure patching for $BRAND ... "

    #
    # Despite overrides in Makefile.in, the configure script steadfastly
    # tries to change the compilers.  To get around this, we edit the
    # file config/<arch-file> to comment out the offending lines that
    # get included in the Makefile generated by configure.
    #

    cd $start_dir/config

    mv mh-linux mh-linux.ncsa
    sed 's/f77/g77/g' mh-linux.ncsa > mh-linux

    cd $start_dir
}

#
# Function to implement pre-configure installation fixes for macintosh
#

PreConfigFixes_macintosh()
{
    cd $start_dir
        
    EchoAndLog "Pre-configure patching for $BRAND ... "
 
    #
    # Despite overrides in Makefile.in, the configure script steadfastly
    # tries to change the compilers.  To get around this, we edit the
    # file config/<arch-file> to comment out the offending lines that
    # get included in the Makefile generated by configure.
    #
 
    cd $start_dir/config
 
    mv mh-mac mh-mac.ncsa
    sed 's/f77/g77/g' mh-mac.ncsa > mh-mac
 
    cd $start_dir
}

PreConfigFixes_cygwin()
{
    cd $start_dir/config
    cd $start_dir
}

#
# Function to implement pre-configure installation fixes for sgi
#

PreConfigFixes_sgi()
{
    cd $start_dir

    EchoAndLog "Pre-configure patching for $BRAND ... "

    #
    # Despite overrides in Makefile.in, the configure script steadfastly
    # tries to change the compilers.  To get around this, we edit the
    # file config/<arch-file> to comment out the offending lines that
    # get included in the Makefile generated by configure.
    #

    cd $start_dir/config

    if [ ! -f mh-irix6.ncsa ] ; then
        mv mh-irix6 mh-irix6.ncsa
        mv mh-irix32 mh-irix32.ncsa
        sed 's/n32/32/g' mh-irix32.ncsa > mh-irix32
        mv mh-irix32 mh-irix32.ncsa
        sed 's/mips3/mips2/g' mh-irix32.ncsa > mh-irix32
        mv mh-irix32 mh-irix6
    fi

    cd $start_dir
}




#
# Function to implement pre-configure installation fixes for sgi32
#

PreConfigFixes_sgi32()
{
    cd $start_dir

    EchoAndLog "Pre-configure patching for $BRAND ... "


    #
    # Return to starting directory
    #

    cd $start_dir
}


#
# Function to implement pre-configure installation fixes for sgi64
#

PreConfigFixes_sgi64()
{
    cd $start_dir

    EchoAndLog "Pre-configure patching for $BRAND... "

    #
    # If the SGI is using old R4?00 chips then mips4 is
    # not supported, so force the use of mips3.
    #

    cd $start_dir/config

    if [ ! -f mh-irix6.ncsa ] ; then
	sed 's/mips4/mips3/g' mh-irix6 > mh-irix6.ncsa
        mv mh-irix6.ncsa mh-irix6
    fi

    cd $start_dir
}




#
# Function to implement pre-configure installation fixes for SunOs 4.x
#

PreConfigFixes_sun4()
{
    cd $start_dir

    EchoAndLog "Pre-configure patching for SunOs `uname -r`... "

    #
    # Despite overrides in Makefile.in, the configure script steadfastly
    # tries to make gcc the default C compiler.  To get around this we
    # edit the file config/mh-sun to comment out the offending lines that
    # get included in the Makefile generated by configure.
    #

    cd $start_dir/config

    if [ ! -f mh-sun.ncsa ] ; then
        mv mh-sun mh-sun.ncsa
    fi

    cat mh-sun.ncsa |
    sed '/^CC *=/s/^/#/' |
    sed '/^CFLAGS *=/s/^/#/' |
    cat > mh-sun

    cd $start_dir
}


#
# Function to implement pre-configure installation fixes for SunOs 5.x
#

PreConfigFixes_sun5()
{
    cd $start_dir

    EchoAndLog "Pre-configure patching for SunOs `uname -r`... "



    #
    # Return to starting directory
    #

    cd $start_dir

}


#
# Function to implement pre-configure installation fixes for dec
#

PreConfigFixes_dec()
{
    cd $start_dir/config

    EchoAndLog "Pre-configure patching for $BRAND... "


    # Modify mh-alpha to include math library for DEC-ALPHA
    # version 4.0

    chmod 0755 *
    mv mh-alpha mh-alpha.ncsa
    sed 's/#LIBSX/LIBSX/g' mh-alpha.ncsa > mh-alpha
    mv mh-alpha mh-alpha.ncsa
    sed 's/FC = NONE/FC = f77/g' mh-alpha.ncsa > mh-alpha
    #
    # Return to starting directory
    #

    cd $start_dir
}



#
# Function to implement post-configure installation fixes for dec
#

PostConfigFixes_dec()
{
    cd $start_dir

    EchoAndLog "Post-configure patching for $BRAND... "

    cd $start_dir
}


#
# Function to implement post-configure installation fixes for IBM AIX 4.2
#

PostConfigFixes_ibm()
{
    cd $start_dir/mfhdf/nctest
 
    EchoAndLog "Post-configure patching for IBM `uname -r`... "
 
    # Rename program error.c and edit the Makfile to reflect change
    # This version of AIX will not compile the program error.c
    # So I am changing the name so that HDF will install
 
    cp error.c dave.c
    chmod 0755 Makefile
    mv Makefile Makefile.ncsa
    sed 's/error.c/dave.c/g' Makefile.ncsa > Makefile
    mv Makefile Makefile.ncsa
    sed 's/error.o/dave.o/g' Makefile.ncsa > Makefile
 
    #
    # Return to starting directory
    #
 
    cd $start_dir
 
}

#
# Function to implement post-configure installation fixes for SunOs 5.x
#

PostConfigFixes_sun5()
{
    cd $start_dir

    EchoAndLog "Post-configure patching for SunOs `uname -r`... "

    cd $start_dir
}


#
# Function to implement post-configure installation fixes for 
# platforms that do not have tbl or neqn
#

PostConfigFixes_NoTblNeqn()
{
    #
    # Search the system for 'tbl' and 'neqn'
    # If either one is not found, set up a sed command to patch 
    # Makefiles to use 'cat' instead
    #

    sed_cmd="cat"
    for util in tbl neqn ; do

        Which $util
        if [ "$which_result" = "0" ] ; then 
            EchoAndLog "This system does not have the '$util' utility:"
            EchoAndLog -n "- setting up to use 'cat' instead"
            sed_cmd="$sed_cmd | sed 's/ $util/cat/g'"
        fi
    
    done

    #
    # Edit the Makefiles generated by configure, setting the values of
    # 'neqn' and 'tbl' to 'cat', as needed, so that man page installation
    # can proceed without fatal errors.   Note: this may cause some
    # man pages to be improperly formatted.   This only appears to
    # be a problem in the mfhdf section, so only those makefiles
    # are patched.
    # 

    if [ "$sed_cmd" != cat ] ; then

        cd $start_dir/mfhdf

        files=`find . -name Makefile -print`

        for file in $files ; do

            mv $file $file.tmp
            eval "cat $file.tmp | $sed_cmd | cat > $file"

        done
        
        cd $start_dir

    fi


}



#
# Function to set compilers, overriding the NCSA compiler defaults
#
    
SetCompilers()
{
    #
    # Set C compiler based on value of $CC
    # Set C compilation flags based on value of $CFLAGS
    # Set FORTRAN compiler based on value of $F77
    # 
    # NOTE: 
    #     This is done recursively for ALL Makefiles because on some
    #     platforms, the compiler variables are not inherited from the
    #     the calling Makefile.


    EchoAndLog "Overriding NCSA default compiler settings:"

    files=`find . -name Makefile -print`

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

        if [ "$CFLAGS" = "" ] ; then

            EchoAndLog -n "- setting C compiler to '$CC'"
            EchoAndLog -n "- setting FORTRAN compiler to '$F77'"
             
            for file in $files ; do
            
                if [ ! -f $file.ncsa.default ] ; then
                    mv  $file $file.ncsa.default
                fi

                cat $file.ncsa.default |
#                sed '/^CC/s/=.*$'"/= $CC/" | 
#                sed '/^FC/s/=.*$'"/= $F77/" |
                cat > $file.ncsa

                cp $file.ncsa $file

            done

        else

            EchoAndLog -n "- setting C compiler to '$CC'"
            if [ "$CFLAGS" = "-O" ] ; then
                EchoAndLog -n "- setting C compilation flags to default"
            else
                EchoAndLog -n "- setting C compilation flags to '$CFLAGS'"
            fi

            EchoAndLog -n "- setting FORTRAN compiler to '$F77'"
             
           for file in $files ; do

                if [ ! -f $file.ncsa.default ] ; then
                    mv  $file $file.ncsa.default
                fi

                cat $file.ncsa.default |
#                sed '/^CC/s/=.*$'"/= $CC/" | 
#                sed '/^CFLAGS/s/=.*$'"/= $CFLAGS/" |
#                sed '/^FC/s/=.*$'"/= $F77/" |
                cat > $file.ncsa

                cp $file.ncsa $file

            done

        fi

    else

        EchoAndLog "Warning: Environment variable(s) CC and/or F77 not set."
        EchoAndLog "If CC is not set, the installation uses NCSA defaults."

    fi

}



#
# Function to do architecture-specific pre-configure fixes
#
    
PreConfigure()
{

    cd $start_dir

    if [ "$dbug" = "1" ] ; then
	PreConfigFixes_dbug
    fi

    #
    # Call architecture-specific functions
    #

    case $BRAND in
    
        cray  ) ;;
    
        dec   ) PreConfigFixes_dec;;
    
        hp    ) 
	    # for HP-11 add -lnsl
	    if [ `uname -r | awk -F. '{print $2}'` = "11" ] ; then
		mv $install_dir/config/mh-hpux $install_dir/config/mh-hpux_tmp1
		cat $install_dir/config/mh-hpux_tmp1 |
		sed '/^LD_XDR/s/=.*$'"/= -lnsl/" |
		cat > $install_dir/config/mh-hpux_tmp2
		cp $install_dir/config/mh-hpux_tmp2 $install_dir/config/mh-hpux
		/bin/rm -f $install_dir/config/mh-hpux_tmp1
		/bin/rm -f $install_dir/config/mh-hpux_tmp2
	    fi
	    ;;
    
        ibm   ) ;;

        linux ) 
		if [ ! -f /bin/f77 ] ; then
		    PreConfigFixes_linux
		fi
		;;
    
        linux32 ) 
		if [ ! -f /bin/f77 ] ; then
		    PreConfigFixes_linux
		fi
		;;
    
        linux64 ) 
		if [ ! -f /bin/f77 ] ; then
		    PreConfigFixes_linux
		fi
		;;
    
        macintosh )
                if [ ! -f /bin/f77 ] ; then
                    PreConfigFixes_macintosh
                fi
                ;;
            
        macintel )
                if [ ! -f /bin/f77 ] ; then
                    PreConfigFixes_macintosh
                fi
                ;;
               
        macintel32 )
                if [ ! -f /bin/f77 ] ; then
                    PreConfigFixes_macintosh
                fi
                ;;
               
        macintel64 )
                if [ ! -f /bin/f77 ] ; then
                    PreConfigFixes_macintosh
                fi
                ;;
    
        cygwin )
                if [ ! -f /bin/f77 ] ; then
                    PreConfigFixes_cygwin
                fi
   		;;

        sgi   ) 
		if [ "$OSTYPE" = "IRIX64" ] ; then
		    PreConfigFixes_sgi
		fi
		;;
    
        sgi32 ) config_flags=irix6_32;;

        sgi64 )
		cpu_type=`hinv | fgrep CPU | head -1 | cut -d' ' -f3 | cut -b2`
		if [ "$cpu_type" = "4" ] ; then
		    PreConfigFixes_sgi64
		fi
		;;
    
        sun4  ) ;;
    
        sun5  ) ;;

        sun5.8  ) ;;
    
        sun5.9  ) ;;
    
        sun5.10  ) ;;
    
        *     ) WriteError "Architecture $BRAND is not supported."
                exit 1 ;;
    
    esac

    cd $start_dir

}


#
# Function to do architecture-specific post-configure fixes
#
    
PostConfigure()
{
    cd $start_dir
    
    
    #
    # Check for missing tbl and neqn, patching as needed
    #

    PostConfigFixes_NoTblNeqn


    #
    # Do architecture-specific fixes
    #

    case $BRAND in
    
        cray  ) ;;
    
        dec   ) ;;
    
        hp    ) ;;
    
        ibm   ) PostConfigFixes_ibm;;

        linux ) ;;
 
        linux32 ) ;;
  
        linux64 ) ;;
   
        macintosh ) ;;
   
        macintel ) ;;

        macintel32 ) ;;

        macintel64 ) ;;
    
        cygwin ) ;;
  
        sgi   ) ;;
    
        sgi32 ) ;;

        sgi64 ) ;;
    
        sun4  ) ;;
    
        sun5  ) ;;

        sun5.8  ) ;;
    
        sun5.9  ) ;;
    
        sun5.10  ) ;;
    
        *     ) WriteError "Architecture $BRAND is not supported."
                exit 1 ;;
    
    esac

    
    #
    # If requested, set compilers to something other than the defaults
    #

    if [ $set_compilers = 1 ] ; then
        SetCompilers
    fi

    cd $start_dir
}



#****************************************************************
#                                                               *
#                  * * * Main Program  * * *                    *
#                                                               *
#****************************************************************

#
# Get arguments from command line
#

set_compilers=0
config_flags=""
dbug=0

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

    case "$1" in

      -h | help ) Help ;;

      -set_compilers ) set_compilers=1 ;;

      -dbug ) dbug=1 ;;

      -start_dir ) 
          if [ "$2" != "" ] ; then
              start_dir=$2
              shift
          else
              WriteError "No installation directory specified with -start_dir switch."
              Help
          fi
        ;;


      * ) echo "$script_error invalid argument '$1'" ; Help ;;

    esac

    shift

done


#
# Make sure we're not running standalone
#

if [ "$hdf_install_caller" = "" ] ; then

    WriteError "This script can only be run from INSTALL-HDF."
    Help

else

    # Here we set up the HDF installation directory,
    # for use by the configure script.  
    # 
    # With this release of HDF, we abandon the convention of setting the
    # installation directory to $HDFHOME/hdf, which was formerly done for
    # backwards compatibility with the installed HDF directory structure 
    # that the toolkit was using with the 3.3r4 version of HDF.
    # 
    # New releases of the SDP Toolkit  and hdfeos have been updated to handle this
    # directory change

    install_dir="$HDFHOME"

    if [ ! -d "$install_dir" ] ; then 	# create the install dir, if needed.
        mkdir $install_dir
    fi

fi



#
# Announce
#

EchoAndLog "Running $this_script"

#
# Do architecture-specific pre-configure fixes
#

#PreConfigure

#
# In dec -ljpeg does not work, must use full path for the library
#
if [ "$BRAND" = "dec" ] ; then
	PreConfigJPEGFixes_dec
fi

if [ "$dbug" = "1" ] ; then
        CFLAGS=" -g"
    fi
        export CFLAGS

#
# Run the NCSA-supplied configure script
#

if [ "$dbug" = "1" ] ; then
        config_flags="$config_flags --disable-production  --disable-shared"
else
        config_flags="$config_flags --disable-shared"
fi

cd $start_dir

tmp_home=$HOME			# save $HOME
HOME=`pwd` ; export HOME	# change it to avoid interaction w/ .cshrc

if [ "$User_has_external_netCDF" = "1" ] ; then # Configure HDF4 with --disable-netcdf
    if [ "$CONFIG_HDF4_WITH_SZIP" = "1" ] ; then    # Configure HDF4 with SZIP
	EchoAndLog "Running ./configure -v --with-jpeg=$JPEGINC,$JPEGLIB --with-zlib=$ZLIBINC,$ZLIBLIB --with-szlib=$SZIPINC,$SZIPLIB --disable-netcdf --prefix=$install_dir $config_flags"

	if [ "$log_file" != "" ] ; then
	    echo "" >> $log_file

	    ./configure -v --with-zlib=$ZLIBINC,$ZLIBLIB --with-jpeg=$JPEGINC,$JPEGLIB  --with-szlib=$SZIPINC,$SZIPLIB --disable-netcdf --prefix=$install_dir $config_flags >> $log_file 2>&1
	else
	
	    echo ""
	    ./configure -v --with-zlib=$ZLIBINC,$ZLIBLIB --with-jpeg=$JPEGINC,$JPEGLIB   --with-szlib=$SZIPINC,$SZIPLIB --disable-netcdf --prefix=$install_dir $config_flags
	fi
    else      # Do not configure HDF4 with SZIP
	EchoAndLog "Running ./configure -v  --with-jpeg=$JPEGINC,$JPEGLIB --with-zlib=$ZLIBINC,$ZLIBLIB  --disable-netcdf --prefix=$install_dir $config_flags"
	
	if [ "$log_file" != "" ] ; then
	    echo "" >> $log_file
	    
	    ./configure -v --with-zlib=$ZLIBINC,$ZLIBLIB --with-jpeg=$JPEGINC,$JPEGLIB --disable-netcdf --prefix=$install_dir $config_flags >> $log_file 2>&1
	else
	
	    echo ""
	    ./configure -v --with-zlib=$ZLIBINC,$ZLIBLIB --with-jpeg=$JPEGINC,$JPEGLIB --disable-netcdf --prefix=$install_dir $config_flags
	fi
    fi
else  # Configure HDF4 without --disable-netcdf
    if [ "$CONFIG_HDF4_WITH_SZIP" = "1" ] ; then    # Configure HDF4 with SZIP
	EchoAndLog "Running ./configure -v  --with-jpeg=$JPEGINC,$JPEGLIB --with-zlib=$ZLIBINC,$ZLIBLIB --with-szlib=$SZIPINC,$SZIPLIB --prefix=$install_dir $config_flags"

	if [ "$log_file" != "" ] ; then
	    echo "" >> $log_file

	    ./configure -v --with-zlib=$ZLIBINC,$ZLIBLIB --with-jpeg=$JPEGINC,$JPEGLIB  --with-szlib=$SZIPINC,$SZIPLIB --prefix=$install_dir $config_flags >> $log_file 2>&1
	else
	
	    echo ""
	    ./configure -v --with-zlib=$ZLIBINC,$ZLIBLIB --with-jpeg=$JPEGINC,$JPEGLIB   --with-szlib=$SZIPINC,$SZIPLIB --prefix=$install_dir $config_flags
	fi
    else      # Do not configure HDF4 with SZIP
	EchoAndLog "Running ./configure -v  --with-jpeg=$JPEGINC,$JPEGLIB --with-zlib=$ZLIBINC,$ZLIBLIB  --prefix=$install_dir $config_flags"
	
	if [ "$log_file" != "" ] ; then
	    echo "" >> $log_file
	    
	    ./configure -v --with-zlib=$ZLIBINC,$ZLIBLIB --with-jpeg=$JPEGINC,$JPEGLIB  --prefix=$install_dir $config_flags >> $log_file 2>&1
	else
	
	    echo ""
	    ./configure -v --with-zlib=$ZLIBINC,$ZLIBLIB --with-jpeg=$JPEGINC,$JPEGLIB --prefix=$install_dir $config_flags
	fi
    fi
fi

HOME=$tmp_home ; export HOME	# restore $HOME


#
# Do architecture-specific post-configure fixes
#


PostConfigure


#
# Run the make utility to compile the library and utiltites
#

EchoAndLog "Running make, target=all"

if [ "$log_file" != "" ] ; then
    echo "" >> $log_file
    make >> $log_file 2>&1
    tmp_status=$?
else
    echo ""
    make 
    tmp_status=$?
fi

if [ "$tmp_status" != 0 ] ; then
    EchoAndLog "Run of 'make ' failed."
    exit 1
fi


#
# Run the make utility install the libraries, utilities, includes and man pages 
#

EchoAndLog "Running make, target=install "

if [ "$log_file" != "" ] ; then
    echo "" >> $log_file
    make install >> $log_file 2>&1
    tmp_status=$?
else
    echo ""
    make install
    tmp_status=$?
fi


if [ "$tmp_status" != 0 ] ; then
    EchoAndLog "Run of 'make install' failed."
    exit 1
fi

