#!/bin/sh
########################################################################
#                 copyright 2001, Amoco Production Company             #
#                             All Rights Reserved                      #
#                     an affiliate of BP America Inc.                  #
########################################################################
#
# This script is a part of FreeUSP - the UNIX Seismic Processing system.
#
#
# get the actual program name if it has not been previously set
#
if [ ${APP:-"not set"} = "not set" ]
  then
   APP="`echo $0 | awk -F/ '{print $NF}'`"
fi
#
# use variables set by osarch to determine path to binary
#

case "${OSARCH_OS}" in
  Sun*)    COMP_ARCHS="SunOS-CM SunOS";
	   OS_REVS="5.9 5.8 5.7 5.6 5.5 5.5.1 4.1.4 4.1.3 4";
	   ARCHDIRS="sun4u sun4d sun4c sun4m sun4 sun4m sun4c sun4d sun4u";;
  IRIX*)   COMP_ARCHS="IRIX64 IRIX";
	   OS_REVS="6.5 6.4 6.2 5 ";
#
# this is kind of wierd. 8000 chips are IP21. We don't have an 8000 chip to
# develop on. 10000 code should run, so we want to look UP the chip dirs too.
#
 	   ARCHDIRS="IP35 IP30 IP28 IP27 IP25 IP26 IP21 IP22 IP21 IP26 IP25 IP27 IP28 IP30 IP35";;
  HP*)     COMP_ARCHS="HP-UX";
	   OS_REVS="11 10 9";
 	   ARCHDIRS="9000-889 9000-755 9000-735 9000-755 9000-889";;
  Convex*) ARCHDIRS="convex_C220";;
  Linux*)  COMP_ARCHS="Linux";
	   OS_REVS="2.6 2.4 2.2 2.1";
	   ARCHDIRS="i386 i586 i686 i586 i386";;
  FreeBSD*)  COMP_ARCHS="FreeBSD Linux";
	   OS_REVS="4.6 4.5 2.4 2.2 2.1";
	   ARCHDIRS="i386 i586 i686 i586 i386";;
  OSF*)    COMP_ARCHS="OSF1";
	   OS_REVS="5.0";
	   ARCHDIRS="alpha";;
  Darwin*) COMP_ARCHS="Darwin";
	   OS_REVS="6.8 6.6";
	   ARCHDIRS="PowerMacintosh";;
  *     )  ARCHDIRS="";;
esac
#
# first place to look is in the common directory to see if there's an
# executable that's not architecture dependent. This is also useful for
# "hiding" utilities that may have the same name as a widely used 
# utility. (ie. man) This can be used with a generic script like "usp"
# to come up with a command like "usp man prgm" or "usp which prgm"
#
# obviously we don't want to do this if we're already in that script
#
if test -x ${APPDIR}/common/${DIST_PRODUCT}/${DIST_VERSION}/${APP} && \
  test "$0" != "${APPDIR}/common/${DIST_PRODUCT}/${DIST_VERSION}/${APP}"
 then
   EXEC_APP=${APPDIR}/common/${DIST_PRODUCT}/${DIST_VERSION}/${APP}
else
#
# look through combinations of OS level and hardware starting with the
# most compatible
#

BUILDINGPATH="false"
EXEC_APP="ERROR"
test ! -x ${APPDIR}/common/${DIST_PRODUCT}/${DIST_VERSION}/${APP} && \
  SEARCHED_PATH="${APPDIR}/common/${DIST_PRODUCT}/${DIST_VERSION}"
#
# Look through compatible operating systems
#
for osarch in ${COMP_ARCHS}
do
# echo "OSARCH_OS = ${OSARCH_OS} ; osarch = ${osarch}"
  if [ "${OSARCH_OS}" = "${osarch}" ]
    then
# echo building path
	BUILDINGPATH="true"
  fi
  if [ "${BUILDINGPATH}" = "true" ]
    then
	BUILDINGPATH="false"
  for osrev in ${OS_REVS}
  do
# echo "OSARCH_MAJOR_OS_REV = ${OSARCH_MAJOR_OS_REV}.${OSARCH_MINOR_OS_REV} ; osrev = ${osrev}"
#
# - changed this upon migration to /apps/global. Full OS version is used.
#   if [ "${OSARCH_OS_REV}" = "${osrev}" ]
#
    if [ "${OSARCH_MAJOR_OS_REV}.${OSARCH_MINOR_OS_REV}" = "${osrev}" ]
      then
	BUILDINGPATH="true"
# echo building path
    fi
    if [ "${BUILDINGPATH}" = "true" ]
      then
	BUILDINGPATH="false"
      for target in ${ARCHDIRS}
      do
# echo "OSARCH_HARDWARE = ${OSARCH_HARDWARE} ; target = ${target}"
        if [ "${OSARCH_HARDWARE}" = "${target}" ]
          then
  	    BUILDINGPATH="true"
# echo building path
        fi
        if [ "${BUILDINGPATH}" = "true" ]
          then
# echo looking in ${APPDIR}/${osarch}/${osrev}/${DIST_PRODUCT}/${DIST_VERSION}/${target}/${APP}
	    SEARCHED_PATH="${SEARCHED_PATH} ${APPDIR}/${osarch}/${osrev}/${DIST_PRODUCT}/${DIST_VERSION}/${target}/${APP}"
	    EXEC_APP=${APPDIR}/${osarch}/${osrev}/${DIST_PRODUCT}/${DIST_VERSION}/${target}/${APP}
	    if  [ -x ${EXEC_APP} ]
	      then
                 break
            else
	      EXEC_APP="ERROR"
	    fi
        fi
      done
#
# this means we've got an executable - no need to look at lower OS levels
#
      if [ "${EXEC_APP}" != "ERROR" ]
        then
          break
      fi
    fi
  done
#
# this means we've got an executable - no need to look at compatible OS's 
#
    if [ "${EXEC_APP}" != "ERROR" ]
      then
        break
    fi
  fi
done
fi
#

if [ "${EXEC_APP}" = "ERROR" ]
   then
#
# - had to add this block to prevent problems with awk receiving too
#   long of an input string - joe m. wade 1/28/2000
#
	ERRFILE=/tmp/${USER}.`date +%y%j`.$$
	touch ${ERRFILE} > /dev/null 2>&1
	if (test -f ${ERRFILE}) then
	  for filename in ${SEARCHED_PATH}
          do
	    (grep ${filename} ${ERRFILE} > /dev/null ) || \
		 echo ${filename} >> ${ERRFILE}
 	  done
#
	  echo No binary found for ${APP} in
#	  echo "${SEARCHED_PATH}" |		- old method - 1/28/2000
	  awk '{for (i=1; i<=NF; i++) { \
	    dup="false" ;\
	    for (j=1; j<i; j++) if ( $j == $i ) dup="true" ;\
	      if ( dup != "true" ) printf("     %s\n",$i); \
	      } \
     	    }' < ${ERRFILE}
	  rm ${ERRFILE}
	else
	  devchk=`tty`
	  if test "${devchk}" != "not a tty"
	  then
	      ( echo No binary found for ${APP} in ${APPDIR} structure ) > /dev/tty
	  fi
	fi
	if test "${APP}" = "feedback"
	then
	  APP=""
	else
	  exit 1
	fi
   else

# - added this to get library path if we've not found an exact match on the 
#   binary

## - removed it because it causes problems in cases like ikp on a 5.7 system
##   problem is that the 5.6 binary is picked up, then awk run during 
##   execution of a child script dies because it needs the 5.7 libraries
##						- joe m. wade - 4/12/01
##
## - trying this scheme again because we need to separate the Linux 
##   ia64 libraries from the i686. I've switched to appending the new 
##   path rather than a prepend. 
##						- joe m. wade - 5/1/01
##
       if [ "${OSARCH_OS}/${OSARCH_MAJOR_OS_REV}.${OSARCH_MINOR_OS_REV}/${OSARCH_HARDWARE}" != "${osarch}/${osrev}/${target}" ]
	then
# if LD_LIBRARY_PATH is already set, append to it

##	  LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+"${APPDIR}/${osarch}/${osrev}/${DIST_PRODUCT}/lib:${LD_LIBRARY_PATH}"}
	  LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+"${LD_LIBRARY_PATH}:${APPDIR}/${osarch}/${osrev}/${DIST_PRODUCT}/${DIST_VERSION}/${target}"}

# if LD_LIBRARY_PATH is not set, set it

	  LD_LIBRARY_PATH=${LD_LIBRARY_PATH:="${APPDIR}/${osarch}/${osrev}/${DIST_PRODUCT}/lib"}

	else
	  LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+"${LD_LIBRARY_PATH}:${APPDIR}/${OSARCH_OS}/${OSARCH_MAJOR_OS_REV}.${OSARCH_MINOR_OS_REV}/${DIST_PRODUCT}/${DIST_VERSION}/${OSARCH_HARDWARE}"}
	fi
	  export LD_LIBRARY_PATH

	APP=${EXEC_APP}
fi
