#!/bin/sh
#
#	$Id: //depot/idl/IDL_70/idldir/bin/unix/startapp#1 $
#
# This script determines the operating system and hardware combination
# and overlays itself with the correct binary for the desired program.
# The program is determined from the name by which the script is invoked.

APPLICATION=`basename $0`
APP_ARGS=
IDL_VERSION=70
ENVI_VERSION=45
ZOOM_VERSION=45
STUDENT_FLAG=

LICENSE_NAME=license.dat

INSTALL_DIR=/Applications/itt

IDLDIRECTORY_VERS=idl${IDL_VERSION}
IDLDIRECTORY=idl

# Some applications can be invoked with or without a version suffix.
# Recognise the versioned ones and remove the version.
case $APPLICATION in
"idl$IDL_VERSION") APPLICATION=idl;;
"idlde$IDL_VERSION") APPLICATION=idlde;;
"idlrpc$IDL_VERSION") APPLICATION=idlrpc;;
"idlhelp$IDL_VERSION") APPLICATION=idlhelp;;
"idlman$IDL_VERSION") APPLICATION=idlman;;
"idldemo$IDL_VERSION") APPLICATION=idldemo;;
"envihelp$ENVI_VERSION") APPLICATION=envihelp;;
"envizoom_help$ZOOM_VERSION") APPLICATION=envizoom_help;;
"enviman$ENVI_VERSION") APPLICATION=enviman;;
"envi_tut$ENVI_VERSION") APPLICATION=envi_tut;;
esac


# Find the main IDL directory
if [ "$IDL_DIR" = "" ]; then
    for DIR in $INSTALL_DIR/$IDLDIRECTORY_VERS /usr/local/itt/$IDLDIRECTORY_VERS . ./idl$IDL_VERSION /usr/local/lib/$IDLDIRECTORY /usr/local/$IDLDIRECTORY /usr/local/bin/$IDLDIRECTORY /usr/local/rsi/$IDLDIRECTORY_VERS
	do
	if [ -d $DIR ]; then
	    if [ -f $DIR/resource/fonts/hersh1.chr ]; then
        	IDL_DIR=$DIR
		break
	    fi
        fi
    done
fi

if [ "$IDL_DIR" = "" ]; then
    echo "Unable to access $APPLICATION. You will have to
define the IDL_DIR environment variable to point at the main
IDL directory before it will work."
    exit 1
fi


# If LM_LICENSE_FILE is not defined and one of the following exists
#    $IDL_DIR/../license/license.dat     (license dir from base idl product)
# then define LM_LICENSE_FILE. If file is not found, leave it unset.
# If LM_LICENSE_FILE is already set, then respect that and leave it alone.  
if [ \( "$LM_LICENSE_FILE" = "" \) ] ; then
    if [ \( -f $IDL_DIR/../license/$LICENSE_NAME \) ] ; then
#      default location for standard IDL distribution
       LM_LICENSE_FILE=$IDL_DIR/../license/$LICENSE_NAME
       export LM_LICENSE_FILE
    fi
fi


if [ "$STUDENT_FLAG" != "" ]; then
  if [ \( "$APPLICATION" = "idl" \) -o \( "$APPLICATION" = "idlde" \) ]; then
     APP_ARGS=$STUDENT_FLAG
  fi
fi


#Strip off any trailing "/" from path
if [ "$IDLJAVAB_LIB_LOCATION" != "" ]; then 
   IDLJAVAB_LIB_LOCATION=`echo $IDLJAVAB_LIB_LOCATION |sed 's/[\/]*$//'`
fi

# Look for a -32 argument. If seen, take note, but don't worry
# about removing it (idl accepts it and ignores it, allowing this
# script to be simpler). If a platform supports both 32 and 64-bit
# versions of our application, the default is to take the 64-bit version.
# However, -32 says to run the 32-bit version even on a 64-bit platform
PREFER_32=0
for arg
do
  if [ "$arg" = "-32" ]; then
    PREFER_32=1
  fi
done


# Determine the operating system, hardware architecture, and os release
# Make sure these agree with IDL's compiled in paths or online help
# won't be found.
OS=
ARCH=
UNAME=`uname`
case $UNAME in

    "Darwin")
        OS="darwin"
        BIN_ARCH=/usr/bin/arch
        # IDLDE_IDLBITS remains 32
        IDLDE_IDLBITS=32
        if [ `$BIN_ARCH` = i386 ]; then
          ARCH=".i386"
          IDLDE_ARCH=$ARCH
	else
          ARCH=".ppc"
          IDLDE_ARCH=$ARCH
        fi
        if [ -d $IDL_DIR/bin/bin.$OS$ARCH ]; then
          IDLDE_ARCHDIR_IDL32=$IDL_DIR/bin/bin.$OS$ARCH
        fi
	;;

    "SunOS")				# Solaris
	OS="solaris2"
        BIN_ARCH=/usr/bin/arch
        if [ -x /usr/bin/arch ]; then
            BIN_ARCH=/usr/bin/arch
        elif [ -x /usr/ucb/arch ]; then
            BIN_ARCH=/usr/ucb/arch
        fi
        if [ `$BIN_ARCH` = i86pc ]; then
          ARCH=".x86"
          IDLDE_ARCH=$ARCH
	else
	  # If the system is running the 64-bit SunOS kernel, a 64-bit
	  # IDL is installed, and the user did not specify the -32
	  # command argument, then run the 64-bit version. Otherwise
	  # the 32-bit version will work on all supported platforms.
          #
          # Whether running 32 or 64, set the directory paths so that
          # the OPS launched from the IDLDE can get to either directory
          # (if it exists). The IDLDE_ARCHDIR_IDLnn (either 32 or 64) will
          # be unset if the corresponding directory has not been installed.
          #
          # For ARCH, start with default of 32 bit, override if 64 chosen
          ARCH=".sparc"
          # IDLDE arch always stays 32 bit
          IDLDE_ARCH=$ARCH
          # For IDLDE_IDLBITS start with default of 32
          IDLDE_IDLBITS=32
          if [ -d $IDL_DIR/bin/bin.$OS$ARCH ]; then
            IDLDE_ARCHDIR_IDL32=$IDL_DIR/bin/bin.$OS$ARCH
          fi
	  if [ -x /bin/isainfo ]; then
            if [ `/bin/isainfo -b` = 64 ]; then
              if [ "$APPLICATION" = "idlde" ]; then
                # idlde app is not in bin.os.arch - test directory only
                if [ -d $IDL_DIR/bin/bin.$OS.sparc64 ]; then
                  # can do 64, set IDLDE_ARCHDIR_IDL64 for use by IDLDE
                  IDLDE_ARCHDIR_IDL64=$IDL_DIR/bin/bin.$OS.sparc64
	          if [ $PREFER_32 = 0  ]; then
                    # ops should use 64; don't set ARCH to 64 though
                    # since idlde needs ld_lib to point to 32 for idlde
                    IDLDE_IDLBITS=64
                  fi
                fi
              else
                if [ -f $IDL_DIR/bin/bin.$OS.sparc64/$APPLICATION ]; then
                  IDLDE_ARCHDIR_IDL64=$IDL_DIR/bin/bin.$OS.sparc64
	          if [ $PREFER_32 = 0  ]; then
                    # actually using 64
	            ARCH=.sparc64
                  fi
                fi
              fi
            fi
          fi
        fi
	;;

    "AIX")
	OS="ibm"
	# Under AIX, all threads must have system contention scope or IDL
	# will not run reliably. If AIXTHREAD_SCOPE is set to S, then we
	# are fine without changing anything. Otherwise, we set
	# AIXTHREAD_SCOPE to force that, and IDL_SAVE_AIXTHREAD_SCOPE
	# is used to remember the previous setting. Once IDL is running,
	# it reads IDL_SAVE_AIXTHREAD_SCOPE and uses its value to reset
	# AIXTHREAD_SCOPE back to its original value. In this way, IDL can
	# run properly, but does not alter the global environment for any
	# programs that it forks.
	if [ "$AIXTHREAD_SCOPE" = "S" ]; then
	  unset IDL_SAVE_AIXTHREAD_SCOPE
	else
	  IDL_SAVE_AIXTHREAD_SCOPE=$AIXTHREAD_SCOPE
	  export IDL_SAVE_AIXTHREAD_SCOPE
	  AIXTHREAD_SCOPE=S
	  export AIXTHREAD_SCOPE
	fi
        # For a 64-bit program to run, both the hardware and software
        # must support 64-bit operation. You will notice that on other
        # platforms we test for this, and quietly roll over to 32-bit
        # operation if 64-bit support is not present. I have not yet
        # found a reliable shell-scriptable test for this ability on
        # AIX, although I have found several that are either not
        # 100% reliable or which require superuser privs to work.
        # Therefore, for now this script assumes that users won't
        # install 64-bit IDL on a system that can't support it, and that
        # this already minor issue will disappear completely as
        # 32-bit only hardware becomes more and more rare.
	if [ \( $PREFER_32 = 0 \) \
            -a \( -f $IDL_DIR/bin/bin.$OS.rs6000_64/$APPLICATION \) ]; then
	    ARCH=.rs6000_64
        fi
	;;


    "IRIX"|"IRIX6"|"IRIX64")
        # 32-bit kernels reply with IRIX. The other variants come from
        # kernels that are capable of running 64-bit binaries.
	OS="sgi"
	if [ \( $PREFER_32 = 0 \) -a \( "$UNAME" != "IRIX" \) \
            -a \( -f $IDL_DIR/bin/bin.$OS.mips64/$APPLICATION \) ]; then
	    ARCH=.mips64
          fi
	;;

    "Linux")
	OS="linux"
        ARCH=".x86"
        # IDLDE arch always stays 32 bit
        IDLDE_ARCH=$ARCH
        # For IDLDE_IDLBITS start with default of 32
        IDLDE_IDLBITS=32
        if [ -d $IDL_DIR/bin/bin.$OS$ARCH ]; then
          IDLDE_ARCHDIR_IDL32=$IDL_DIR/bin/bin.$OS$ARCH
        fi
        if [ -x /bin/arch ]; then
	  # If the system is running the 64-bit kernel, a 64-bit
	  # IDL is installed, and the user did not specify the -32
	  # command argument, then run the 64-bit version. Otherwise
	  # the 32-bit version will work on all supported X86 platforms.
          if [ `/bin/arch` = x86_64 ]; then
            if [ "$APPLICATION" = "idlde" ]; then
              # idlde app is not in bin.os.arch - test directory only
	      if [ -d $IDL_DIR/bin/bin.$OS.x86_64 ]; then
                # can do 64, set IDLDE_ARCHDIR_IDL64 for use by IDLDE
                IDLDE_ARCHDIR_IDL64=$IDL_DIR/bin/bin.$OS.x86_64
	        if [ $PREFER_32 = 0  ]; then
                  # ops should use 64; don't set ARCH to 64 though
                  # since idlde needs ld_lib to point to 32 for idlde
                  IDLDE_IDLBITS=64
                fi
              fi
            else
	      if [ -f $IDL_DIR/bin/bin.$OS.x86_64/$APPLICATION ]; then
                IDLDE_ARCHDIR_IDL64=$IDL_DIR/bin/bin.$OS.x86_64
                if [ $PREFER_32 = 0 ]; then
                  ARCH=.x86_64
                fi
              fi
            fi
          fi
        fi
	;;
		
    *)
	echo "$APPLICATION: Unable to recognize system architecture."
	exit 1
	;;

esac

# Now we have everything we need to construct the path to the bin directory
BIN_DIR=$IDL_DIR/bin/bin.$OS$ARCH
BIN_DIR_IDLDE=$IDL_DIR/idlde/bin.$OS$IDLDE_ARCH


# Add the bin directory to the library search path
case $OS in
    "darwin")
	if [ "$DYLD_LIBRARY_PATH" = "" ]; then
	    DYLD_LIBRARY_PATH="$BIN_DIR"
	else
	    DYLD_LIBRARY_PATH="$BIN_DIR:$DYLD_LIBRARY_PATH"
	fi
	if [ "$APPLICATION" = "idlde" ]; then
		# add bindir for idlde shareable libraries
		DYLD_LIBRARY_PATH="$BIN_DIR_IDLDE:$DYLD_LIBRARY_PATH"
	fi
	export DYLD_LIBRARY_PATH
	;;



    "ibm")
        NEW_TEXT="/lib:/usr/lib:$BIN_DIR:$BIN_DIR/dm/lib"
	if [ "$LIBPATH" = "" ]; then
	    LIBPATH="$NEW_TEXT"
	else
	    LIBPATH="$NEW_TEXT:$LIBPATH"
	fi

	#  Append the Oracle lib directory if ORACLE_HOME set for Dataminer
        if [ "$ORACLE_HOME" != "" ]; then 
             LIBPATH="$ORACLE_HOME/lib:$LIBPATH"
        fi
        #  Append the Sybase lib directory if Sybase set for Dataminer
        if [ "$SYBASE" != "" ]; then
             LIBPATH="$SYBASE/lib:$LIBPATH"
        fi
	export LIBPATH
	;;


    "linux")
        NEW_TEXT="$BIN_DIR:$BIN_DIR/dm/lib"
	if [ "$LD_LIBRARY_PATH" = "" ]; then
	    LD_LIBRARY_PATH="$NEW_TEXT"
	else
	    LD_LIBRARY_PATH="$NEW_TEXT:$LD_LIBRARY_PATH"
	fi

        #  Append the Sybase lib directory if Sybase set for Dataminer
        if [ "$SYBASE" != "" ]; then
             LD_LIBRARY_PATH="$SYBASE/lib:$LD_LIBRARY_PATH"
        fi
	export LD_LIBRARY_PATH

	#  Append the Oracle lib directory if ORACLE_HOME set for Dataminer
        if [ "$ORACLE_HOME" != "" ]; then 
             LD_LIBRARY_PATH="$ORACLE_HOME/lib:$LD_LIBRARY_PATH"
	fi

        if [ "$IDLJAVAB_LIB_LOCATION" != "" ]; then 
             LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$IDLJAVAB_LIB_LOCATION:$IDLJAVAB_LIB_LOCATION/..:$IDLJAVAB_LIB_LOCATION/../native_threads"
	fi

	if [ "$APPLICATION" = "idlde" ]; then
		# add bindir for idlde shareable libraries
		LD_LIBRARY_PATH="$BIN_DIR_IDLDE:$LD_LIBRARY_PATH"
	fi

	export LD_LIBRARY_PATH
	;;


    "sgi")
        if [ "$ARCH" = ".mips64" ]; then
	    if [ "$LD_LIBRARY64_PATH" = "" ]; then
		LD_LIBRARY64_PATH="$BIN_DIR"
	    else
		LD_LIBRARY64_PATH="$BIN_DIR:$LD_LIBRARY64_PATH"
	    fi
            if [ "$IDLJAVAB_LIB_LOCATION" != "" ]; then
                LD_LIBRARYN64_PATH="$LD_LIBRARYN64_PATH:$IDLJAVAB_LIB_LOCATION:$IDLJAVAB_LIB_LOCATION/..:$IDLJAVAB_LIB_LOCATION/../native_threads"
            fi

	    export LD_LIBRARY64_PATH
	else			# 32-bit executable

	    NEW_TEXT="$BIN_DIR:$BIN_DIR/dm/lib"
	    if [ "$LD_LIBRARYN32_PATH" = "" ]; then
		LD_LIBRARYN32_PATH="$NEW_TEXT"
	    else
		LD_LIBRARYN32_PATH="$NEW_TEXT:$LD_LIBRARYN32_PATH"
	    fi

            if [ "$IDLJAVAB_LIB_LOCATION" != "" ]; then
                LD_LIBRARYN32_PATH="$LD_LIBRARYN32_PATH:$IDLJAVAB_LIB_LOCATION:$IDLJAVAB_LIB_LOCATION/..:$IDLJAVAB_LIB_LOCATION/../native_threads"
            fi

	    #  Append the Sybase lib directory if Sybase set for Dataminer
	    if [ "$SYBASE" != "" ]; then
                 LD_LIBRARYN32_PATH="$SYBASE/lib:$LD_LIBRARYN32_PATH"
	    fi


	    #  Append the Oracle lib directory if ORACLE_HOME set for Dataminer
	    #  NOTE: The user must set Oracle N32 home.
	    if [ "$ORACLE_N32_HOME" != "" ]; then
		LD_LIBRARYN32_PATH="$ORACLE_N32_HOME/lib32:$LD_LIBRARYN32_PATH"
	    fi
	    export LD_LIBRARYN32_PATH
        fi
	;;


    "solaris2")
        NEW_TEXT="$BIN_DIR:$BIN_DIR/dm/lib:/usr/openwin/lib:/usr/dt/lib"
	if [ "$LD_LIBRARY_PATH" = "" ]; then
	    LD_LIBRARY_PATH="$NEW_TEXT"
	else
	    LD_LIBRARY_PATH="$NEW_TEXT:$LD_LIBRARY_PATH"
	fi

        #  Append the Sybase lib directory if Sybase set for Dataminer
        if [ "$SYBASE" != "" ]; then
             LD_LIBRARY_PATH="$SYBASE/lib:$LD_LIBRARY_PATH"
        fi
	export LD_LIBRARY_PATH

	#  Append the Oracle lib directory if ORACLE_HOME set for Dataminer
        if [ "$ORACLE_HOME" != "" ]; then 
             LD_LIBRARY_PATH="$ORACLE_HOME/lib:$LD_LIBRARY_PATH"
	fi

        if [ "$IDLJAVAB_LIB_LOCATION" != "" ]; then 
             LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$IDLJAVAB_LIB_LOCATION:$IDLJAVAB_LIB_LOCATION/..:$IDLJAVAB_LIB_LOCATION/../native_threads"
	fi

	if [ "$APPLICATION" = "idlde" ]; then
		# add bindir for idlde shareable libraries
		LD_LIBRARY_PATH="$BIN_DIR_IDLDE:$LD_LIBRARY_PATH"
	fi

	export LD_LIBRARY_PATH
	;;


    *)
	if [ "$LD_LIBRARY_PATH" = "" ]; then
	    LD_LIBRARY_PATH="$BIN_DIR"
	else
	    LD_LIBRARY_PATH="$BIN_DIR:$LD_LIBRARY_PATH"
	fi
	export LD_LIBRARY_PATH
	;;
esac


# Add the IDL bin directory to the path so that idlde will always find idl
PATH=$IDL_DIR/bin:$PATH
export PATH IDL_DIR 


case $APPLICATION in
    "idlhelp"|"idlman")
        if [ "$UNAME" = "Darwin" ]; then
           APPLICATION=java
           IDLDE=idlhelpserver.darwin.universal.app/Contents/MacOS/idlhelpserver
        else
           APPLICATION=$BIN_DIR_IDLDE/jre/bin/java
           IDLDE=idlhelpserver.$OS$IDLDE_ARCH
        fi
        CLASSPATH1=`find $IDL_DIR/idlde/plugins -name "com.rsi.idldt_*.jar" | sort | tail -1`
        CLASSPATH2=`find $IDL_DIR/idlde/plugins -name "org.eclipse.help.base_*.jar" | sort | tail -1`
        $APPLICATION -cp $CLASSPATH1:$CLASSPATH2 com.rsi.idldt.IDLHelp -eclipsehome $IDL_DIR/idlde -executable $IDLDE -data $HOME/.idl/itt/idlhelp $* &
        exit 0;
	;;

# For the ENVI help commands, construct their help file argument, and
# then turn the APPLICATION into IDL_ASSISTANT. Note the upper case: We reserve
# the real "idl_assistant" for the case where we want to run the actual
# assistant program without any special interpretation.
     "envihelp"|"enviman"|"envi_tut")
	APPLICATION=IDL_ASSISTANT
	APP_ARGS="-profile $IDL_DIR/products/envi$ENVI_VERSION/help/envi.adp -file $IDL_DIR/products/envi$ENVI_VERSION/help/home.html"
	;;

     "envizoom_help")
	APPLICATION=IDL_ASSISTANT
	APP_ARGS="-profile $IDL_DIR/products/envizoom$ZOOM_VERSION/help/envizoom.adp -file $IDL_DIR/products/envizoom$ZOOM_VERSION/help/home.html"
        ;;


     "idl_assistant")
	# A direct passthru to the underlying program. Supply
	# our adp file, but the caller supplies the rest.
   # On the Mac, we must supply the path to the "executable" file.
   if [ "$UNAME" = "Darwin" ]; then
      APPLICATION=idl_assistant.app/Contents/MacOS/idl_assistant
   fi
	;;	
esac

if [ "$APPLICATION" = "IDL_ASSISTANT" ]; then
    # Run it as a background process without waiting for it.
    case $UNAME in
	"Darwin")
	    $BIN_DIR/idl_assistant.app/Contents/MacOS/idl_assistant $APP_ARGS &
	    ;;
	"Linux")
	    if [ \( $PREFER_32 = 0 \) -a \( `/bin/arch` = x86_64 \) ]; then
		ARCH=.x86_64
		BIN_DIR=$IDL_DIR/bin/bin.$OS$ARCH
	    fi
	    $BIN_DIR/idl_assistant $APP_ARGS &
	    ;;
	*)
	    $BIN_DIR/idl_assistant $APP_ARGS &
	    ;;
   esac
   exit 0
fi


# export archdir directories for both IDL and IDLDE
export IDLDE_ARCHDIR_IDL32
export IDLDE_ARCHDIR_IDL64

if [ "$APPLICATION" = "idlde" ]; then
  if [ "$CLASSPATH" = "" ]; then
    CLASSPATH=".:$IDL_DIR/resource/bridges/export/java/javaidlb.jar"
  else
    CLASSPATH="$CLASSPATH:.:$IDL_DIR/resource/bridges/export/java/javaidlb.jar"
  fi
  export CLASSPATH
  export IDLDE_IDLBITS

 if [ $UNAME = 'Darwin' ]; then
   exec $IDL_DIR/idlde/idlde.darwin.universal.app/Contents/MacOS/idlde "$@" $APP_ARGS 
 else
   exec $IDL_DIR/idlde/$APPLICATION.$OS$IDLDE_ARCH -vm $IDL_DIR/idlde/bin.$OS$IDLDE_ARCH/jre/bin/java "$@" $APP_ARGS
 fi

    # We shouldn't get here unless there was an error.
    echo "$APPLICATION is not available for this system ($OS/$ARCH)"
    exit 1
fi

exec $BIN_DIR/$APPLICATION "$@" $APP_ARGS

# We shouldn't get here unless there was an error.
echo "$APPLICATION is not available for this system ($OS$ARCH)"
exit 1
