#!/bin/sh
#
#                       Copyright (c) 1996 by
#                       Advanced Visual Systems Inc.
#                       All Rights Reserved
#
#       This software comprises unpublished confidential information of
#       Advanced Visual Systems Inc. and may not be used, copied or made
#       available to anyone, except in accordance with the license
#       under which it is furnished.
#
#       This file is under Perforce control at AVS in:
#       $Id: //depot/express/fcs70/bin/xp#1 $
#
# A script for starting AVS/Express dev edition ("xp") or vis edition ("vxp")
#

# set FEATURE name
if [ "`basename $0`" = "xp" ]; then
  XP_FEATURE=EXPRESS
elif [ "`basename $0`" = "vxp" ]; then
  XP_FEATURE=EXPRESS_VIZ
  args=-viz
else
  echo $0: command not found
  exit 1
fi
export XP_FEATURE

# attempt to determine install dir
xp_path=${XP_PATH:-${XP_ROOT:-`dirname $0`/../..}}

# set MACHINE
if [ "$MACHINE" = "" -a -x $xp_path/bin/xp_mach ]; then
  MACHINE=`$xp_path/bin/xp_mach`
  export MACHINE
fi

# Look for and exec AVS/Express
#
# Check $xp_path first; if XP_PATH or XP_ROOT are set then the
# user really wants that version...
#
if [ -x $xp_path/bin/$MACHINE/express -a ! -d $xp_path/bin/$MACHINE/express ] ; then
  # Found express executable, now set
  # LD_LIBRARY_PATH or SHLIB_PATH or DYLD_LIBRARY_PATH
  #
  if [ "$MACHINE" != "" ]; then
    if [ "$MACHINE" = "hp11" -o "$MACHINE" = "hp11_64" ]; then
      # HP-UX
      SHLIB_PATH=$SHLIB_PATH:$xp_path/lib/$MACHINE:$xp_path/lib/$MACHINE/missing
      export SHLIB_PATH
      #
    elif [ "$MACHINE" = "darwin" ]; then
      # Darwin
      if [ "$DYLD_LIBRARY_PATH" = "" ]; then
        DYLD_LIBRARY_PATH=$xp_path/lib/$MACHINE
      else
        DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:$xp_path/lib/$MACHINE
      fi
      export DYLD_LIBRARY_PATH
      #
    elif [ "$MACHINE" = "sgN32" -a LD_LIBRARYN32_PATH != "" ]; then
      # SGI/IRIX
      # Need to avoid picking up /usr/lib32/libgd.so
      LD_LIBRARYN32_PATH=$LD_LIBRARYN32_PATH:$xp_path/lib/$MACHINE
      export LD_LIBRARYN32_PATH
      #
    elif [ "$MACHINE" = "sg6" -a LD_LIBRARY64_PATH != "" ]; then
      # SGI/IRIX
      LD_LIBRARY64_PATH=$LD_LIBRARY64_PATH:$xp_path/lib/$MACHINE
      export LD_LIBRARY64_PATH
      #
    else
      # Other Unix/Linux
      LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$xp_path/lib/$MACHINE:$xp_path/lib/$MACHINE/missing
      export LD_LIBRARY_PATH
    fi
  fi
  if [ "$*" = "" ]; then
#   Express does not like "" as argument
    exec $xp_path/bin/$MACHINE/express $args
  else
    exec $xp_path/bin/$MACHINE/express $args "$@"
  fi
fi

#
# Check the directories in $PATH
#
for p in `echo $PATH | tr ':' ' '`; do
  if [ -x $p/express -a ! -d $p/express ]; then
    MACHINE=${MACHINE:-`basename $p`}
    # The previous value of xp_path did not work; guess again
    xp_path=$p/../..
    # set LD_LIBRARY_PATH or SHLIB_PATH
    if [ "$MACHINE" != "" ]; then
      if [ "$MACHINE" = "hp11" -o "$MACHINE" = "hp11_64" ]; then
        # HP-UX
        SHLIB_PATH=$SHLIB_PATH:$xp_path/lib/$MACHINE:$xp_path/lib/$MACHINE/missing
        export SHLIB_PATH
      elif [ "$MACHINE" = "darwin" ]; then
        # Darwin
        if [ "$DYLD_LIBRARY_PATH" = "" ]; then
          DYLD_LIBRARY_PATH=$xp_path/lib/$MACHINE
        else
          DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:$xp_path/lib/$MACHINE
        fi
        export DYLD_LIBRARY_PATH
      else
        # Other Unix/Linux
        LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$xp_path/lib/$MACHINE:$xp_path/lib/$MACHINE/missing
        export LD_LIBRARY_PATH
      fi
    fi
    if [ "$*" = "" ]; then
      # Express does not like "" as argument
      exec $p/express $args
    else
      exec $p/express $args "$@"
    fi
  fi
done

# perhaps we are in . and . is not in $PATH
if [ -x express -a ! -d express ] ; then
  xp_path=../..
# set MACHINE
  if [ "$MACHINE" = "" -a -x $xp_path/bin/xp_mach ]; then
    MACHINE=`$xp_path/bin/xp_mach`
    export MACHINE
  fi
# set LD_LIBRARY_PATH or SHLIB_PATH
  if [ "$MACHINE" != "" ]; then
    if [ "$MACHINE" = "hp11" -o "$MACHINE" = "hp11_64" ]; then
      # HP-UX
      SHLIB_PATH=$SHLIB_PATH:$xp_path/lib/$MACHINE:$xp_path/lib/$MACHINE/missing
      export SHLIB_PATH
    else
      # Other Unix/Linux
      LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$xp_path/lib/$MACHINE:$xp_path/lib/$MACHINE/missing
      export LD_LIBRARY_PATH
    fi
  fi
  if [ "$*" = "" ]; then
#   Express does not like "" as argument
    exec ./express $args
  else
    exec ./express $args "$@"
  fi
fi

echo "The AVS/Express executable cannot be found.  Please check your PATH,"
echo "MACHINE, XP_PATH and/or XP_ROOT environment variable(s)."

exit 1
