#! /bin/sh
#-----------------------------------------------------------------------------
# filename:
# 	INSTALL-JPEG
#
# description:
# 	JPEG installation script for the (PGS) Toolkit and hdfeos
#
# usage:
# 	INSTALL-JPEG [-f90 [-NAG]] [-ABSOFT] [-fc_path <path/compiler_name>] 
#                    [-cc_path <path/compiler_name>] 
# 	             [-sgi32 | -sgi64] [-log <log-file> [-append]] 
# 	             [-clean | -cleano] [-strip] [-w_home <file>]
# 	             [-df <distribution-file>] [-batch]
# 	             [-install_dir <base installation directory>]
# 	INSTALL-JPEG [-h] 
#
# 	  -f90     : build JPEG FORTRAN modules using f90 
# 	             optional flag -NAG specifies NAG f90
#         -ABSOFT  : flag that specifies absoft f77 is used for fortran compilation
# 	  -fc_path : set the path of the FORTRAN compiler to <path/compiler_name>
# 	  -cc_path : set the path of the C compiler to <path/compiler_name>
# 	  -sgi32   : build in -n32   mode (SGI Power Challenge only)
# 	  -sgi64   : build in 64-bit mode (SGI Power Challenge only)
# 	  -log     : sdone session output to new <log-file>
# 	  -append  : append to existing <log-file>
# 	  -clean   : run 'make clean' after doing the installation
# 	  -cleano  : clean up all object and a.out files
# 	  -strip   : strip out all non-installation files following install
# 	  -w_home  : write JPEG home directory to <file>
# 	  -df      : install JPEG from distribution file <distribution-file>
# 	  -batch   : run script in batch (i.e. non-interactive) mode
#         -install_dir : full path to top level directory where JPEG will be
#                        installed (e.g. /usr/local/hdfeos/jpeg/sgi64)
# 	  -h       : display this help message and exit
#
# notes:
# 	1)  The use of the -clean option will cause the JPEG utilites to 
# 	    be deleted.  To preserve them, use the -cleano option instead.
# 
# 	2)  The -f90 and -NAG options are  * * NOT IMPLEMENTED  * * 
# 	    Currently, these options are accepted, but ignored.
# 	    
#
# author:
#        Mike Sucher / Applied Research Corp.
#        Megan E. Donovan Spencer / SM&AE
#        Abe Taaheri / SM&AE
#        Phuong T Nguyen / L3 Communication GSI.
#        Adura Adekunjo /L3 Communication GSI.
# history:
#       11-May-1995 MES Initial version
#       20-Oct-2003 AT  Used INSTALL-HDF5 as template for INSTALL-SZIP 
#       26-Mar-2004 AA  Used INSTALL-SZIP as template for INSTALL-JPEG
#-----------------------------------------------------------------------------

#
# 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()
{
    echo ""
    echo "$*"
    if [ "$log_file" != "" ] ; then
        echo "" >> $log_file
        echo "$*" >> $log_file
    fi
}


#
# 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 set up and export the following architecture-specific variables:
#
#         BRAND HOST LOGNAME OSTYPE PATH USER
#


SetArchitecture()
{
    # set path to a base subset of directories, allowing startup on unknown host
    # note: once the architecture has been determined the path is customized
    
    PATH=/usr/local/bin:/usr/bin:/bin:/etc:/usr/etc:/usr/ucb:/usr/bin/X11
    
    
    # get operating system type, login name
    # special cases: SCO and Cray  - uname works differently,
    
    machine=`uname -m | awk '{print $1}'`	# needed on Cray & SCO
    temp_ostype=`uname`

    case "$machine" in
    
      i386 )        			# SCO box
    	OSTYPE=sco386
        ;;
    
      CRAY )    				# CRAY
    	OSTYPE=UNICOS
        ;;
    
      * )					# everybody else
    	OSTYPE=`uname`
        ;;
    
    esac
    # Intel Macintosh is also i386 or i686 (?) machine

    if [ "$machine" = "i386" ] ; then
	if [ "$temp_ostype" = "Darwin" ] ; then 
	    OSTYPE=DarwinIntel
	fi
	if [ "`uname | awk -F_ '{print $1}'`" = "CYGWIN" ] ; then 
	    OSTYPE=Cygwin
	fi
    fi
    if [ "$machine" = "i686" ] ; then
	if [ "$temp_ostype" = "Darwin" ] ; then 
	    OSTYPE=DarwinIntel
	fi
	if [ "`uname | awk -F_ '{print $1}'`" = "CYGWIN" ] ; then 
	    OSTYPE=Cygwin
	fi
    fi
   if [ "$machine" = "x86_64" ] || [ "$machine" = "ia64" ] ; then
	if [ "$temp_ostype" = "Darwin" ] ; then 
	    OSTYPE=DarwinIntel
	    if [ "$MAC_BRAND" = "" ] ; then 
		echo " Error: In 64-bit MAC platform the env. variable MAC_BRAND must be set to mac32 or mac64 before running this script."
		exit 1
	    fi
	else
	    if [ "$LINUX_BRAND" = "" ] ; then 
		echo " Error: In 64-bit linux platform the env. variable LINUX_BRAND must be et to linux32 or linux64 before running this script."
		exit 1
	    fi
	fi
    fi

    user=`id | cut -d\( -f2 | cut -d\) -f1`
    if [ "$LOGNAME" = "" ] ; then 		# make sure $LOGNAME is defined
        LOGNAME=$user	
    fi
    USER=$LOGNAME				# make sure $USER is defined
    
    
    # set machine-depdoneent environment variables:
    # 	HOST  the host name of this machine
    # 	BRAND used by other achitecture-specific code
    # 	path  the execution search path exported to PATH
    
    case "$OSTYPE" in
    
      AIX ) 
    	PATH=/usr/local/bin:/bin:/usr/bin:/etc:/usr/etc:/usr/ucb:/usr/bin/X11:/usr/ccs/bin:/usr/sbin
    	HOST=`hostname`
    	BRAND=ibm
        CC=cc
        CFLAGS="-O"
        F77=xlf
        ;;
    
      HP-UX ) 
    	PATH=/usr/local/bin:/bin:/usr/bin:/etc:/usr/etc:/usr/bin/X11:/tools/bin
    	HOST=`hostname`
    	BRAND=hp 
        CC=cc
        CFLAGS="-O"
        F77=fort77
        ;;
    
      IRIX )  
    	PATH=/usr/local/bin:/usr/bin:/bin:/etc:/usr/etc:/usr/bsd:/usr/bin/X11:/usr/sbin
    	HOST=`hostname`
    	BRAND=sgi 
        CC=cc
        CFLAGS="-O"
        F77=f77
        ;;
    
      IRIX64 )  
        PATH=/usr/local/bin:/usr/bin:/bin:/etc:/usr/etc:/usr/bsd:/usr/bin/X11:/usr/sbin
        HOST=`hostname`
        case "$sgi_mode" in
          32  )  BRAND=sgi   ; CC="cc -32 -mips2"  ; F77="f77 -32 -mips2"  ;;
          n32 )  BRAND=sgi32 ; CC="cc -n32 -mips3" ; F77="f77 -n32 -mips3"  ;;
          64  )  BRAND=sgi64 ; CC="cc -64 -mips4"  ; F77="f77 -64 -mips4"  ;;
          *   )  BRAND=sgi   ; CC="cc"  ; F77="f77"  ;;
        esac
        CFLAGS="-O"
        set_compilers=0		# override JPEG default compilers
        ;;
    
      Linux )
	PATH=/usr/local/bin:/bin:/usr/bin:/etc:/usr/etc:/usr/X11/bin
	HOST=`hostname -s`
	BRAND=linux
	if [ "$LINUX_BRAND" = "linux64" ] ; then
	    BRAND=linux64
	elif [ "$LINUX_BRAND" = "linux32" ] ; then
	    BRAND=linux32
	fi
        CC="gcc $LNX_COMP_FLAG"
        CFLAGS="-O"
        F77="g77 $LNX_COMP_FLAG"
	;;

      Darwin )
        PATH=/bin:/sbin:/usr/bin:/usr/sbin
        HOST=`hostname -s`
        BRAND=macintosh
        CC=gcc
        CFLAGS="-O"
        F77=g77
        ;;

      DarwinIntel )
        PATH=/bin:/sbin:/usr/bin:/usr/sbin
        HOST=`hostname -s`
        BRAND=macintel32
	if [ "$MAC_BRAND" = "macintel64" ] ; then
	    BRAND=macintel64
	elif [ "$MAC_BRAND" = "macintel32" ] ; then
	    BRAND=macintel32
	fi
        CC="gcc $MACINTEL_COMP_FLAG"
        CFLAGS="-O2"
        F77="gfortran $MACINTEL_COMP_FLAG"
        ;;

      Cygwin )
        PATH=/bin:/sbin:/usr/bin:/usr/sbin
        HOST=`hostname`
        BRAND=cygwin
        CC=gcc
        CFLAGS="-O"
        F77=g77
        ;;

      OSF1 )  
    	PATH=/usr/local/bin:/bin:/usr/bin:/etc:/usr/etc:/usr/ucb:/usr/bin/X11:/usr/ccs/bin:/usr/sbin
    	HOST=`hostname -s`
    	BRAND=dec 
        CC=cc
        CFLAGS="-O -std"
        F77=f77
        ;;
    
      sco386 ) 
    	PATH=/usr/local/bin:/bin:/usr/bin:/etc:/usr/etc:/usr/bin/X11
    	HOST=`hostname -s`
    	BRAND=sco 
        CC=cc
        CFLAGS="-O"
        F77=f77
        ;;
    
      SunOS ) 
    	# distinguish between SunOS 5.x and 4.x versions
    	if [ `uname -r | awk -F. '{print $1}'` = "5" ] ; then
	    BRAND="sun5"                        # release V5.x SunOS
            PATH=/usr/local/bin:/opt/SUNWspro/bin:/bin:/usr/ucb:/usr/bin:/etc:/usr/etc:/usr/openwin/bin:/usr/openwin/demo
:/usr/ccs/bin:/usr/sbin
            CC=cc
            CFLAGS="-O"
            F77=f77
	    if [ "`uname -r | awk -F. '{print $1, $2}'`" = "5 10" ] ; then
		BRAND=sun5.10			# release V5.10 SunOS
		PATH=/usr/local/bin:/opt/SUNWspro/bin:/usr/ucb:/bin:/usr/bin:/etc:/usr/etc:/usr/openwin/bin:/usr/openwin/demo:/usr/ccs/bin:/usr/sbin
		CC=cc
		CFLAGS="-O"
		F77=f77
	    elif [ "`uname -r | awk -F. '{print $1, $2}'`" = "5 9" ] ; then
		BRAND=sun5.9			# release V5.9 SunOS
		PATH=/usr/local/bin:/opt/SUNWspro/bin:/usr/ucb:/bin:/usr/bin:/etc:/usr/etc:/usr/openwin/bin:/usr/openwin/demo:/usr/ccs/bin:/usr/sbin
		CC=cc
		CFLAGS="-O"
		F77=f77
	    elif [ "`uname -r | awk -F. '{print $1, $2}'`" = "5 8" ] ; then
		BRAND=sun5.8			# release V5.8 SunOS
		PATH=/usr/local/bin:/opt/SUNWspro/bin:/usr/ucb:/bin:/usr/bin:/etc:/usr/etc:/usr/openwin/bin:/usr/openwin/demo:/usr/ccs/bin:/usr/sbin
		CC=cc
		CFLAGS="-O"
		F77=f77
	    fi
    	else
            BRAND=sun4			# release V4.x SunOS
    	    PATH=/usr/local/bin:/usr/local/lang:/usr/lang:/bin:/usr/ucb:/usr/bin:/etc:/usr/etc:/usr/openwin/bin:/usr/openwin/demo
            CC=acc
            CFLAGS="-O"
            F77=f77
            set_compilers=1		# override JPEG default compilers
    	fi
    	HOST=`hostname`
        ;;
    
      UNICOS ) 
    	PATH=/usr/local/bin:/bin:/usr/bin:/etc:/usr/ucb:/usr/bin/X11
    	HOST=`hostname`
    	BRAND=cray 
        CC=cc
        CFLAGS="-O"
        F77=cf77
        ;;
    
      * )
    	echo "Operating system: $OSTYPE not supported" 	>&2
    	echo "This release of the Toolkit and hdfeos supports: " 	>&2
    	echo "   Sun, SGI HP-9000 IBM-6000 DEC-Alpha  Linux Macintosh and Cray/Unicos " 	>&2
        exit 1
        ;;
    
    esac


    # if debug version was requested replace -O with -g in CFLAGS

    if [ "$dbug" = "1" ] ; then
	CFLAGS=`echo $CFLAGS | sed 's/-O/-g/'`
    fi

    CFLAGS="$CFLAGS $EXT_CC_FLAGS"

    # export the architecture-specific variables

    export BRAND HOST LOGNAME OSTYPE PATH USER
    export CC CFLAGS F77
    
}


#
# Function to allow user to override the defaults for the compilers
# The following variables are affected:
#
#         PATH CC F77
#

UserCompilers()
{
    #
    # 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
		;;

		"linux" )
		    CC="$cc_path/$cc $LNX_COMP_FLAG"
		;; 

		"linux32" )
		    CC="$cc_path/$cc $LNX_COMP_FLAG"
		;; 

		"linux64" )
		    CC="$cc_path/$cc $LNX_COMP_FLAG"
		;; 

		"macintel" )
		    CC="$cc_path/$cc $MACINTEL_COMP_FLAG"
		;;

		"macintel32" )
		    CC="$cc_path/$cc $MACINTEL_COMP_FLAG"
		;; 

		"macintel64" )
		    CC="$cc_path/$cc $MACINTEL_COMP_FLAG"
		;; 

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

	    export CC		# user overrides C compiler name
	    set_compilers=1		# override HDF default compilers
    
	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  "JPEG build may fail."
    
        fi
    
    fi
    
    
    #
    # check to see if valid FORTRAN compiler path was specified
    #
    
    fc=""
    
    if [ "$fc_path" != "" ] ; then
    
        if [ -f $fc_path ] ; then
    
            fc=`basename $fc_path`
            fc_path=`echo $fc_path | sed 's%/[^/]*$%%'`
	    case "$BRAND" in
		"sgi32" )
		    F77="$fc_path/$fc -n32"
		;;

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

		"linux" )
		    F77="$fc_path/$fc $LNX_COMP_FLAG"
		;; 

		"linux32" )
		    F77="$fc_path/$fc $LNX_COMP_FLAG"
		;; 

		"linux64" )
		    F77="$fc_path/$fc $LNX_COMP_FLAG"
		;;

		"macintel" )
		    F77="$fc_path/$fc $MACINTEL_COMP_FLAG"
		;;

		"macintel32" )
		    F77="$fc_path/$fc $MACINTEL_COMP_FLAG"
		;;

		"macintel64" )
		    F77="$fc_path/$fc $MACINTEL_COMP_FLAG"
		;;

		* )
		    F77="$fc_path/$fc" 
		;; 
	    esac

    #        if [ "$fc" != f90 ] ; then	# (f90 not supported for HDF4.1r1)
    # f90 is supported starting with the HDF4.1r2 release
                export F77	# user overrides FORTRAN compiler name
                set_compilers=1		# override HDF default compilers
    #        fi
    
        fi
    
        if [ -d $fc_path ] ; then	# make sure fc_path is now a directory
    
            PATH=${fc_path}:${PATH} 	# prepend fc_path to search path
            export PATH
    
        else
    
            fc_path=""
            echo "Warning: FORTRAN directory $fc_path not found."
            echo  "JPEG build may fail."
    
        fi
    
    fi
    
}



#
# Function to get the location of the distribution file
#
    
GetDistribution()
{    
    : ${distrib_file:=jpegsrc.v6b.tar.gz}

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

    case "$distrib_file" in

      jpegsrc.v6b.tar.gz  )
        tar_file=jpegsrc.v6b.tar
        jpeg_home=jpeg-6b
        ;;

      * )
        EchoAndLog "JPEG 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 "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 "JPEG installation cancelled."
        exit 1
    fi

}


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

GetInstallDir()
{
    default_JPEG_path="`pwd`/jpeg/$BRAND"

    if [ "$JPEG_path" = "" ] ; then
	JPEG_path="`pwd`/jpeg/$BRAND"
    fi

    # ask the user

    if [ $batch_mode = 0 ] ; then
	UserPrompt "Pathname where directory '$jpeg_home' will be created [$JPEG_path]: " 
	echo ""
    else
	user_response=""
    fi
    
    if [ "$user_response" = "" ] ; then
        if [  ! -d `dirname $JPEG_path`  ] ; then mkdir `dirname $JPEG_path` ; fi
        if [  ! -d $JPEG_path  ] ; then  mkdir $JPEG_path ; fi
    else
#        if [ -d "$user_response" ] ; then 
	JPEG_path=$user_response ; #fi
    fi

    if [ ! -d $JPEG_path ] ; then
	if [ $batch_mode = 0 ] ; then
	    UserPrompt "$JPEG_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 $JPEG_path
		if [ ! -d $JPEG_path ] ; then
		    WriteError "Unable to create directory $JPEG_path." 
		    echo "JPEG installation cancelled." 2>&1
		    exit 1
		
		fi
		;;
	    *)
		WriteError "Directory $JPEG_path does not exist." 
		echo "JPEG installation cancelled." 2>&1
		exit 1
		;;
        esac
    fi
    
    
    #
    # confirm before proceeding
    #
    
    echo "The JPEG directory will be restored from: " 
    echo "    ${distrib_path}/${distrib_file}" 
    echo "" 
    echo "The JPEG directory will be created in: " 
    echo "     ${JPEG_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

#
# For linux create soft links if default JPEG installation directory is used
#
    save_dir="`pwd`"
    if [ "`pwd`/jpeg/$BRAND" = "$default_JPEG_path" ] ;then
	if [ "$BRAND" = "linux64" ] ; then
	    cd ${default_JPEG_path}/..
	    current_path="`pwd`"
	    EchoAndLog "64-bit structure is the default in this machine. Creating $current_path/linux as a soft link to $default_JPEG_path"
	    ln -s ./linux64 linux
 	    cd $save_dir
	fi

	if [ "$BRAND" = "linux" ] ; then
	    cd ${default_JPEG_path}/..
	    current_path="`pwd`"
	    EchoAndLog "32-bit structure is the default in this machine. Creating $current_path/linux32 as a soft link to $default_JPEG_path"
	    ln -s ./linux linux32
 	    cd $save_dir
	fi
    fi

#
# For macintel create soft links if default JPEG installation directory is used
#
    save_dir="`pwd`"
    if [ "`pwd`/jpeg/$BRAND" = "$default_JPEG_path" ] ;then
	if [ "$BRAND" = "macintel32" ] ; then
	    cd ${default_JPEG_path}/..
	    current_path="`pwd`"
	    EchoAndLog "macintel32 directories will be soft linked to macintel. Creating $current_path/macintel as a soft link to $default_JPEG_path"
	    ln -s ./macintel32 macintel
 	    cd $save_dir
	fi

	if [ "$BRAND" = "macintel" ] ; then
	    cd ${default_JPEG_path}/..
	    current_path="`pwd`"
	    EchoAndLog "macintel32 directories will be soft linked to macintel. Creating $current_path/macintel32 as a soft link to $default_JPEG_path"
	    ln -s ./macintel macintel32
 	    cd $save_dir
	fi
    fi

}


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

UnpackDistribution()
{
    #
    # Go to the JPEG parent directory
    #
    
    cd $JPEG_path

    #
    # If the new JPEG home directory already exits, it must
    # be removed first.  Check with the user before proceeding.
    #
    
    if [ -d "$jpeg_home" ] ; then
    
        EchoAndLog "The directory $jpeg_home already exists."
	if [ $batch_mode = 0 ] ; then
	    UserPrompt "Replace it [no] ? "
	else
	    user_response=y
	fi

        case "$user_response" in
          y* | Y* )
            EchoAndLog "Removing old $jpeg_home directory ..."
            /bin/rm -rf $jpeg_home
            ;;
    
          * )
            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 ""
        ;;

      gz )
	gzip -cd $distrib_file | tar xvf - | awk '{printf "."}' 2>&1
#        GnuZcat
#        if [ $gnu_zcat != 1 ] ; then
#            WriteError "You need the GNU version of zcat to unpack this file."
#            exit 1
#        fi
#         zcat $distrib_file | tar xvf - | awk '{printf "."}' 2>&1
        echo ""
        ;;

      tar)
        echo $distrib_file
        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


}


#
# See if zcat is GNU version
#

GnuZcat()
{
    if [ "$1" = "" ] ; then
        zcat_cmd=zcat
    else
        zcat_cmd=$1
    fi
    
    buf=`exec 2>&1; $zcat_cmd < /dev/null | tail -1` 
    buf=`echo $buf | awk -F: '{print $1}'`
    
    case "$buf" in
      zcat) gnu_zcat=1 ;;
      *   ) gnu_zcat=0 ;;
    esac
}


#
# Strip all JPEG distribution files not needed after installation.
#

StripHdfInstall()
{
    cd $JPEGHOME

    here=`pwd`
    here=`basename $here`

    #
    # Make sure this is a valid JPEG directory befor clobbering files !
    # 

    if [ "`echo $here | grep '^JPEG'`" = "" ] ; then
    
        EchoAndLog "Not a valid JPEG directory: no files removed."
        
    else

        EchoAndLog "Removing all non-installation JPEG files ..."

        for file in * ; do

            if [ -d "$file" ] ; then
    
                case "$file" in 
        
                  bin | lib | include | man ) ;;
                  * ) rm -rf $file ;;
              
                esac
        
            else
    
                rm -f $file
        
            fi
    
        done
        
    fi
}


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



#
# Get arguments from command line
#

log_file=""
home_file=""
noquery=0
clean=0
cleano=0
strip_install=0
append=0
no_untar=0
sgi_mode=32
f90_flag=0
nag_flag=0
absoft_flag=0
fc_path=""
cc_path=""
distrib_file=""
set_compilers=0
dbug=0
JPEG_path=""
batch_mode=0

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

    case "$1" in

      -h )
        Help
        ;;

      -f90 )
        f90_flag=1
        ;;

      -NAG )
        if [  $f90_flag = 0  ] ; then
            WriteError "Must first specify -f90 and set compiler path."
            Help
        else
            nag_flag=1
        fi
        ;;

      -ABSOFT )
	    absoft_flag=1
	;;

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

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

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

      -sgi32 )
        sgi_mode=n32
        ;;

      -sgi64 )
        sgi_mode=64
        ;;

      -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
        ;;

      -w_home )
        if [ "$2" != "" ] ; then
            if [ "` echo $2 | cut -c1`" = "/" ] ; then
                home_file="$2"
            else
                home_file="$start_dir/$2"
            fi
            shift
        else
            WriteError "No file specified with -w_home switch."
            Help
        fi
        ;;

      -nq | -noquery )
        noquery=1
        ;;

      -clean )
        clean=1
        ;;

      -cleano )
        cleano=1
        ;;

      -strip )
        strip_install=1
        ;;

      -no_untar )
        no_untar=1
        ;;

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

	-batch )
	    batch_mode=1
	;;

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

    esac

    shift

done


#
# Inititialize log file
#

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

    if [ "$append" = "0" ] ; then
        if [ -f "$log_file" ] ; then 
            /bin/rm  -f $log_file > /dev/null 2>&1
        fi
    fi

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

    echo ""
    echo "`basename $0`: session output will go to file: "
    echo ""
    echo "     $log_file"
    echo ""

fi

export log_file

#
# 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


#
# Set architecture-specific variables
#

SetArchitecture


#
# Set user compiler overrides
#

UserCompilers


#
# Get the location of the distribution file
#

GetDistribution


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

GetInstallDir


#
# Write the log file header
#

if [ "$log_file" != "" ] ; then
    echo "------------------------------------------------------------" >> $log_file
    echo "" >> $log_file
    echo "JPEG installation starting at: `date`" >> $log_file
    echo "host: $HOST    type: $BRAND     user: $USER " >> $log_file
    echo "" >> $log_file
    echo "" >> $log_file
    echo "The JPEG directory will be restored from: " >> $log_file
    echo "    ${distrib_path}/${distrib_file}" >> $log_file
    echo "" >> $log_file
    echo "The JPEG directory will be created in: " >> $log_file
    echo "     ${JPEG_path}" >> $log_file
    echo "" >> $log_file
    echo "------------------------------------------------------------" >> $log_file
    echo "" >> $log_file
    echo "" >> $log_file
fi


#
# Unpack the distribution file into the installation directory
#

    cd $JPEG_path

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


#
# Prepare to install the requested version of JPEG
#

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

cd $jpeg_home				# go to the starting directory

jpeg_install_caller="$this_script"	# set caller flag for sub-modules
export jpeg_install_caller


#
# Set the version to be installed
#

install_script=""
install_options=""

case $jpeg_home in

  jpeg-6b )

    JPEGHOME=${JPEG_path}/jpeg-6b ; export JPEGHOME
    install_script="$script_dir/INSTALL-JPEGSRC.v6b"
    if [ $set_compilers = 1 ] ; then
        install_options="-set_compilers"
    fi
    if [ $dbug = 1 ] ; then
        install_options="$install_options -dbug"
    fi
    install_options="$install_options -start_dir ${JPEGHOME}"
    ;;

  * ) 			# unsupported
    EchoAndLog "$jpeg_home installation is not supported by this script."
    exit 1
    ;;

esac


#
# Run the install script
#

if [ ! -f $install_script ] ; then
    EchoAndLog "Can't find $install_script - installation failed."
    exit 1
fi

$install_script $install_options

if [ $? != 0 ] ; then
    EchoAndLog "$jpeg_home Installation failed."
    exit 1
fi


# 
# Pass back the JPEG install directory for the hdfeos installation
# 

if [ "$home_file" != "" ] ; then
    echo $JPEGHOME >> $home_file
fi



#
# Clean up
#

cd $JPEG_path

if [ "$clean" = 1 ] ; then

    for dir in $JPEGHOME ; do 

        cd $dir
        echo "Cleaning up in $dir ..."

        if [ "$log_file" != "" ] ; then
            echo "" >> $log_file
            echo "Cleaning up in $dir ..." >> $log_file
            echo "" >> $log_file
            make clean  >> $log_file 2>&1
        else
            make clean
        fi

    done

fi

if [ "$cleano" = 1 ] ; then

    cd $JPEGHOME
    echo "Removing any remaining object files ..." 

    if [ "$log_file" != "" ] ; then
        echo "" >> $log_file
        echo "Removing any remaining object files ..." >> $log_file
        echo "" >> $log_file
        find . -name \*.o -print -exec rm -f {} \; >> $log_file 2>&1
        find . -name a.out -print -exec rm -f {} \; >> $log_file 2>&1
    else
        find . -name \*.o -print -exec rm -f {} \; 
        find . -name a.out -print -exec rm -f {} \; 
    fi
    
fi


if [ "$strip_install" = 1 ] ; then

    StripHdfInstall
   
fi

#
# Mac and MacIntel have problems with libjpeg table; run ranlib
#

if [ "$BRAND" = "macintosh" ] || [ "$BRAND" = "macintel" ]  || [ "$BRAND" = "macintel32" ] || [ "$BRAND" = "macintel32" ]; then
   ranlib $JPEGHOME/lib/libjpeg.a
fi


echo "------------------------------------------------------------"
echo ""
echo "    JPEG installation ending at: `date` " 
echo ""
echo "------------------------------------------------------------"



#
# Write the log file trailer
#

if [ "$log_file" != "" ] ; then
    echo "------------------------------------------------------------" >> $log_file
    echo "" >> $log_file
    echo "    JPEG installation ending at: `date`" >> $log_file
    echo "" >> $log_file
    echo "------------------------------------------------------------" >> $log_file
    echo "" >> $log_file
fi

