#!/bin/sh
#-----------------------------------------------------------------------------
# filename:
# 	INSTALL-HDFEOS-Wrap
#
# description:
# 	HDFEOS installation script wrapper for the SDP(PGS) Toolkit and hdfeos
# 	The  purpose of this script is to integrate the installation
# 	of HDFEOS when it is being performed concurrently with the
# 	SDP Toolkit or hdfeos5 installation.In normal use this script is called
# 	by INSTALL, the master Toolkit installation script, though it
# 	may also be run standalone.
#
# usage:
# 	INSTALL-HDFEOS-Wrap [-sgi32 | -sgi64] [-log <log-file> 
#                           [-cc_path <path/compiler_name>]
#	                    [-append]] [-df <distribution-file>] 
#                           [-w_home <file>] [-batch]
# 	                    [-install_dir <base installation directory>]
# 	INSTALL-HDFEOS-Wrap [-h] 
#
# 	  -sgi32  : build in -n32   mode (SGI Power Challenge only)
# 	  -sgi64  : build in 64-bit mode (SGI Power Challenge only)
# 	  -log    : send session output to <log-file>
# 	  -dbug   : build debug version of hdfeos library
#         -cc_path: set the path of the C compiler to <path/compiler_name>
# 	  -append : append to existing <log-file>
# 	  -w_home : write HDFEOS home directory to <file>
# 	  -df     : install HDFEOS from distribution file <distribution-file>
# 	  -batch  : run script in batch (i.e. non-interactive) mode
# 	  -h      : display this help message and exit
#
# notes:
# 	1) An initial installation requires the HDFEOS distribution file to 
# 	   have been previously downloaded.  Also, HDF must already be 
# 	   installed, preferably as part of the Toolkit installation.
# 	    
# 	2) This script requires the environment variables HDFINC, HDFLIB,
#          SZIPINC and SZIPLIB to be properly set.  In normal use these
# 	   will be set by the INSTALL script.
#
# author:
#        Mike Sucher / Applied Research Corp.
#        Phuong T. Nguyen/ Emergent Information Technology, Inc.
#        Abe Taaheri/ Raytheon IIS
#
# history:
#       27-Sep-1996 MES Initial version (portions taken from INSTALL-HDF)
# 	17-Oct-1996 MES Various fixes and enhancements.
# 	23-Oct-1996 MES Add UserPrompt -x option to do shell syntax expansion.
#       24-Jun-1998 MEDS Updating script to include new tar file for version
#                        HDF-EOS 2.3 in the distribution list
#       24-Jun-1998 MEDS Updating script to include new tar file for version
#                        HDF-EOS 2.4 in the distribution list
#       19-Jun-1999 MEDS Updating script to include new tar file for version
#                        HDF-EOS 2.5 in the distribution list
#       12-Oct-1999 MEDS Updating script to include new tar file for version
#                        HDF-EOS 2.6 in the distribution list
#       26-Sep-2001 PTN  Updating script to include new tar file for version
#                        HDF-EOS 2.8 in the distribution list
#       17-Mar-2003  AA  Updating script to include new tar file for version
#                        HDF-EOS 2.9 in the distribution list 
#       15-Sep-2003 PTN  Add -cc_path option.
#       20-Oct-2003  AT  Updating script to include new tar file for version
#                        HDF-EOS 2.10 in the distribution list 
#       18-Jan-2005  MP  Updating script to include new tar file for version
#                        HDF-EOS 2.13 in the distribution list
#       08-Sep-2005  AT  Updating script to include new tar file for version
#                        HDF-EOS 2.14 in the distribution list
#       05-Jul-2007  AT  Updating script to include new tar file for version
#                        HDF-EOS 2.15 in the distribution list
#       20-May-2009  AT  Updating script to include new tar file for version
#                        HDF-EOS 2.16 in the distribution list
#       20-Jul-2010  AT  Updating script to include new tar file for version
#                        HDF-EOS 2.17 in the distribution list
#       16-Nov-2011  AT  Updating script to include new tar file for version
#                        HDF-EOS 2.18 in the distribution list
#       05-Nov-2013  AT  Updating script to include new tar file for version
#                        HDF-EOS 2.19 in the distribution list
#       27-Jan-2014  AT  Updating script to include new tar file for version
#                        HDF-EOS 2.19 in the distribution list
#
#
#-----------------------------------------------------------------------------

this_script="`basename $0`"


#****************************************************************
#                                                               *
#                   * * * 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 [ "$1" = -x ] ; then		# -x: eXpand shell variables
        shift
        expand_flag=1
    else
        expand_flag=0
    fi

    if [ "$BRAND" = "linux" ] || [ "$BRAND" = "linux32" ] || [ "$BRAND" = "linux64" ]; then
	/bin/echo "$* \c" > /dev/tty
    else
	echo "$* \\c" > /dev/tty
    fi

    read user_response

    if [ $expand_flag = 1 ] ; then	# perform shell variable expansion
        user_response=`echo $user_response | sed 's%~%$HOME%'`  # map ~ to $HOME
        user_response=`eval echo $user_response`		# expand all
    fi
}


#
# 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 get the location of the distribution file
#
    
GetDistribution()
{    
    : ${distrib_file:=HDF-EOS2.19v1.00.tar.Z}

    ds1=$distrib_file			# backup copy
    
    # derive tar file name

    case "$distrib_file" in

      HDF-EOS2.0v1.00.tar.Z )
        tar_file=HDF-EOS2.0v1.00.tar
        HDFEOS_home=hdfeos
        ;;


      HDF-EOS2.1v1.00.tar.Z )
        tar_file=HDF-EOS2.1v1.00.tar
        HDFEOS_home=hdfeos
        ;;


      HDF-EOS2.2v1.00.tar.Z )
        tar_file=HDF-EOS2.2v1.00.tar
        HDFEOS_home=hdfeos
        ;;

      HDF-EOS2.3v1.00.tar.Z )
        tar_file=HDF-EOS2.3v1.00.tar
        HDFEOS_home=hdfeos
        ;;

      HDF-EOS2.4v1.00.tar.Z )
        tar_file=HDF-EOS2.4v1.00.tar
        HDFEOS_home=hdfeos
        ;;

      HDF-EOS2.5v1.00.tar.Z )
        tar_file=HDF-EOS2.5v1.00.tar
        HDFEOS_home=hdfeos
        ;;

      HDF-EOS2.7.2v1.00.tar.Z )
        tar_file=HDF-EOS2.7.2v1.00.tar
        HDFEOS_home=hdfeos
        ;;

      HDF-EOS2.8v1.00.tar.Z )
        tar_file=HDF-EOS2.8v1.00.tar
        HDFEOS_home=hdfeos
        ;;
      
      HDF-EOS2.10v1.00.tar.Z )
        tar_file=HDF-EOS2.10v1.00.tar
        HDFEOS_home=hdfeos
        ;;

     HDF-EOS2.11v1.00.tar.Z )
      tar_file=HDF-EOS2.11v1.00.tar
      HDFEOS_home=hdfeos
        ;;

     HDF-EOS2.12v1.00.tar.Z )
      tar_file=HDF-EOS2.12v1.00.tar
      HDFEOS_home=hdfeos
        ;;

     HDF-EOS2.13v1.00.tar.Z )
      tar_file=HDF-EOS2.13v1.00.tar
      HDFEOS_home=hdfeos
        ;;

     HDF-EOS2.14v1.00.tar.Z )
      tar_file=HDF-EOS2.14v1.00.tar
      HDFEOS_home=hdfeos
        ;;

     HDF-EOS2.16v1.00.tar.Z )
      tar_file=HDF-EOS2.16v1.00.tar
      HDFEOS_home=hdfeos
        ;;

     HDF-EOS2.17v1.00.tar.Z )
      tar_file=HDF-EOS2.17v1.00.tar
      HDFEOS_home=hdfeos
        ;;

     HDF-EOS2.18v1.00.tar.Z )
      tar_file=HDF-EOS2.18v1.00.tar
      HDFEOS_home=hdfeos
        ;;

     HDF-EOS2.19v1.00.tar.Z )
      tar_file=HDF-EOS2.19v1.00.tar
      HDFEOS_home=hdfeos
        ;;

     * )
        EchoAndLog "HDFEOS version in $distrib_file not supported by this script."
        exit 1
        ;;

    esac

    
    # set search path to include current dir, parent of current and $HOME    
    search_path=`pwd`
    search_path="$search_path `cd ..; pwd` $HOME"

    # set search file to include compressed tar file and uncompressed tar file
    search_file="$distrib_file $tar_file"

    # search for the distribution file

    found_file=0
    for distrib_path in $search_path ; do

        for distrib_file in $search_file ; do

            if [ -f ${distrib_path}/${distrib_file} ] ; then
                found_file=1
                break
            fi

        done

        if [ $found_file = 1 ] ; then
            break
        fi

    done

    # if file was not found, ask the user

    if [ $found_file = 0 ] ; then

	if [ $batch_mode = 0 ] ; then
	    echo ""	
	    UserPrompt -x "Pathname where $ds1 is located: "
	    echo ""
  	else
	    user_response=""
	fi
    
        if [ "$user_response" != "" ] ; then

            # make sure directory exists

            if [ -d $user_response ] ; then
                distrib_path=$user_response
            else 
                WriteError "Invalid directory: $user_response"
                exit 1
            fi
    
            # search for the distribution file in user directory

            for distrib_file in $search_file ; do

                if [ -f ${distrib_path}/${distrib_file} ] ; then
                    found_file=1
                    break
                fi

            done

        fi


    fi


    if [ $found_file = 0 ] ; then
        WriteError "Can't find ${ds1} in (${search_path})"
        echo "HDFEOS installation cancelled."
        exit 1
    fi

}

# 
# Function to get the location of the HDFEOS home directory for installation
# Also, prompt user that all information is valid before proceeding
#

GetInstallDir()
{    
    if [ "$HDFEOS_path" = "" ] ; then
	HDFEOS_path="`pwd`"
    fi
    
    # ask the user
    
    if [ $batch_mode = 0 ] ; then
	UserPrompt -x "Pathname where hdfeos will be installed [$HDFEOS_path]: " 
	echo ""
    else
	user_response=""
    fi
    
    if [ "$user_response" = "" ] ; then
        if [  ! -d `dirname $HDFEOS_path`  ] ; then mkdir `dirname $HDFEOS_path` ; fi
        if [  ! -d $HDFEOS_path  ] ; then  mkdir $HDFEOS_path ; fi
    else
        if [ -d "$user_response" ] ; then HDFEOS_path=$user_response ; fi
    fi
    
    if [ ! -d $HDFEOS_path ] ; then
	if [ $batch_mode = 0 ] ; then
	    UserPrompt "$HDFEOS_path does not exist.  Create [y]? "
	else
	    user_response=y
	fi
	if [ "$user_response" = "" ] ; then user_response="y" ; fi
	case "$user_response" in
	    y* | Y* )
		mkdir -p $HDFEOS_path
		if [ ! -d $HDFEOS_path ] ; then
		    WriteError "Unable to create directory $HDFEOS_path." 
		    echo "HDFEOS installation cancelled." 2>&1
		    exit 1
		
		fi
		;;
	    *)
		WriteError "Directory $HDFEOS_path does not exist." 
		echo "HDFEOS installation cancelled." 2>&1
		exit 1
		;;
        esac
    fi
    
    
    #
    # confirm before proceeding
    #
    
#    echo "The HDFEOS directory will be restored from: " 
#    echo "    ${distrib_path}/${distrib_file}" 
#    echo "" 
    echo "The HDFEOS will be installed in: " 
    echo "     ${HDFEOS_path}" 
    echo "" 

    if [ $batch_mode = 0 ] ; then
	UserPrompt "Continue [yes] ? " 
	case "$user_response" in
	    n* | N* ) echo "Cancelled." ; exit 1 ;;
	    * ) echo "Confirmed." ; echo "" ;;
	esac
    fi
}

#
# Function to unpack the distribution file into the installation directory
#

UnpackDistribution()
{
    #
    # Go to the HDFEOS parent directory
    #
    
    cd $HDFEOS_path

    #
    # If the new HDFEOS home directory already exits, it must
    # be removed first.  Check with the user before proceeding.
    #
    
    if [ -d "$HDFEOS_home" ] ; then
    
        EchoAndLog "The directory $HDFEOS_home already exists."
	if [ $batch_mode = 0 ] ; then
	    UserPrompt "[O]verwrite, [R]e-use or [Q]uit (default) ? "
	else
	    user_response=r
	fi

        case "$user_response" in
          o* | O* )
            EchoAndLog "Removing old $HDFEOS_home directory ..."
            /bin/rm -rf $HDFEOS_home
            ;;
    
          r* | R* )
            EchoAndLog "Installation will use existing $HDFEOS_home directory."
            return 0
            ;;
    
          * )
            EchoAndLog "Cancelled."
            exit 1
            ;;
        esac
    
    fi
    
    
    #
    # Create a link to the distribution file, if necessary.
    #

    link_flag=0
    if [ ! -f $distrib_file ] ; then
        if [ -f ${distrib_path}/${distrib_file} ] ; then
            ln -s ${distrib_path}/${distrib_file} ${distrib_file}
            link_flag=1
        fi
    fi
    
    # 
    # Uncompress the distribution file, (if needed), and extract the files
    # 
    
    EchoAndLog "Extracting files from $distrib_file ..."

    suffix="`echo $distrib_file |  sed 's/^.*\.//'`"
    case $suffix in
    
      Z  )
        zcat $distrib_file | tar xvf - | awk '{printf "."}' 2>&1
        echo ""
        ;;

      tar)
        tar xvf $distrib_file | awk '{printf "."}' 2>&1
        echo ""
        ;;
    
    esac

    #
    # Clean up symbolic link, if present
    #

    if [ $link_flag = 1 ] ; then
        /bin/rm -f $distrib_file
    fi


}


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



#
# Get arguments from command line
#

home_file=""
no_untar=0
sgi_mode=32		# by default, SGI mode is standard 32-bit
log_file=""
distrib_file=""
fc_path=""
cc_path=""
append=0
dbug=0
batch_mode=0

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

    case $1 in

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

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

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

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

      -df) distrib_file="$2" ; shift ;;

      -log )
        if [ "$2" != "" ] ; then
            if [ "` echo $2 | cut -c1`" = "/" ] ; then
                log_file="$2"
            else
                log_file="$start_dir/$2"
            fi
            shift
        else
            WriteError "A log file must be specified with the -log option"
            Help
        fi
        ;;

      -append )
        append=1
        ;;

      -dbug )
        dbug=1
        ;;


      -w_home )
        if [ "$2" != "" ] ; then
            if [ "` echo $2 | cut -c1`" = "/" ] ; then
                home_file="$2"
            else
                home_file="$start_dir/$2"
            fi
            shift
        else
            WriteError "A file name must be specified with the -w_home option."
            Help
        fi
        ;;

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

	-batch )
	    batch_mode=1
	;;

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

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

    esac

    shift

done


#
# Initialize the home file
#

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

    if [ -f "$home_file" ] ; then 
        /bin/rm -f $home_file > /dev/null 2>&1
    fi

    touch $home_file > /dev/null 2>&1
    if [ $? != 0 ] ; then
        WriteError "Cannot write to file $home_file"
        exit 1
    fi

fi


#
# Get the location of the distribution file
#
HDFEOS_home=.
#GetDistribution


#
# Got valid distribution file
# Get the location of the HDFEOS home directory for installation
#

GetInstallDir




#
# Unpack the distribution file into the installation directory
#

cd $HDFEOS_path

#if [ "$no_untar" = 0 ] ; then
#    UnpackDistribution
#fi


#
# Prepare to install the requested version of HDFEOS
#

if [ ! -d "$HDFEOS_path" ] ; then
    EchoAndLog "Error creating directory: $HDFEOS_path"
    exit 1
fi

cd $HDFEOS_path/$HDFEOS_home		# go to the starting directory

EchoAndLog "Setting directory to: $HDFEOS_path/$HDFEOS_home"

# HDF-EOS expects these directories to be in place, if they are not
# then create them here.  If we have to do this then the INSTALL may
# fail, but at least we're still one step ahead of the game

if [ ! -d bin/$BRAND ] ; then
        mkdir bin/$BRAND
fi
 
if [ ! -d obj/$BRAND ] ; then
        mkdir obj/$BRAND
fi
 
if [ ! -d lib/$BRAND ] ; then
        mkdir lib/$BRAND
fi

install_flags="-i$HDFINC -l$HDFLIB -szi$SZIPINC -szl$SZIPLIB"

if [ $dbug = 1 ] ; then
    install_flags="$install_flags -dbug"
fi

case "$sgi_mode" in
  n32)
    install_flags="$install_flags -sgi32" ;;
  64)
    install_flags="$install_flags -sgi64" ;;
esac

if [ "$cc_path" != "" ] ; then 		# path for C compiler
    install_flags="$install_flags -cc_path $cc_path"
fi

if [ "$fc_path" != "" ] ; then 		# path for FORTRAN compiler
    install_flags="$install_flags -fc_path $fc_path"
fi

EchoAndLog "Running the HDFEOS installation script ..."

return_status=0
if [ "$log_file" != "" ] ; then

    if [ $append = 1 ] ; then
        bin/INSTALL-HDFEOS $install_flags >> $log_file 2>&1 # append
        return_status=$?
    else
        bin/INSTALL-HDFEOS $install_flags > $log_file  2>&1 # clobber
        return_status=$?
    fi

else

    bin/INSTALL-HDFEOS $install_flags
    return_status=$?

fi


# 
# Pass back the HDF install directory for the toolkit installation
# 

if [ $return_status = 0 ] ; then

    if [ "$home_file" != "" ] ; then
      echo $HDFEOS_path/$HDFEOS_home >> $home_file
    fi

fi


if [ $return_status != 0 ] ; then
    echo "The HDFEOS Installation Script failed or had errors."
    exit 1
fi


