#! /bin/sh
#############################################################################
# $RCSfile$
#
# $Author: jlehtone $
# $Revision: 3678 $
# $Date: 2005-04-13 11:34:35 +0300 (Wed, 13 Apr 2005) $
#
#############################################################################
#
# Script for starting up 'Bodil'. This script determines the binary to be
# run depending on the current platform. Also sets up the variable BODILDIR
# which is very fundamental.
#
#############################################################################

#############################################################################
# get the operating-system version. The ARCH variable's name should not be
# changed, as it is used from within Bodil too. We also set a special
# variable to indicate the real hardware under the hood. This is used when
# there are differences between different machines with the same ARCH but
# different hardware capabilities (stereo).
#
ARCH=`uname`
HARDWARE=`uname -m`

#############################################################################
# Is it a '32-bit Irix'?
#
if [ $ARCH = "IRIX" ]; then
	#
	# we're running on a 32/n32-machine
	ARCH="irix"
	ARCHSTEREO="irix"
    LANG="en_US.ISO8859-1"
    export LANG
    LC_ALL="en_US.ISO8859-1"
    export LC_ALL

    # The IRIX version of registration key
    BODIL_KEY=demo
fi

#############################################################################
# Is it a '64-bit Irix'? We don't really make any difference here as to
# which version of Irix we're on. It apparently is no big difference
# between the 32 and 64-bit optimizations.
#
if [ $ARCH = "IRIX64" ]; then
	#
	# we're running on a 64-machine
	ARCH="irix"
	ARCHSTEREO="irix64"
    LANG="en_US.ISO8859-1"
    export LANG
    LC_ALL="en_US.ISO8859-1"
    export LC_ALL

    # The IRIX version of registration key
    BODIL_KEY=demo
fi

#############################################################################
# Is it a 'Linux'?
#
if [ $ARCH = "Linux" ]; then
	#
	# we're running a Linux on a x86
	ARCH="linux-i386"
	ARCHSTEREO="linux-i386"

    # The LINUX version of registration key
    BODIL_KEY=demo
fi

##############################################################################
# Find out the basepath (BODILDIR). This part is very critical, as we must
# get this right or Bodil won't work ok.
#
# But, this is a Good Thing to do, as we don't need any environment variables
# or anything, just plug and run (p'n'r?).
#
##############################################################################
TRUEFILE=$0
DIRPART=`dirname $TRUEFILE | sed 'sT^\./TT'`

# First make absolute path to called file
FIRSTCHAR=`echo $DIRPART | cut -c 0-1`
if [ $FIRSTCHAR != "/" ]; then
  # ok, it's a relative path, so add the PWD to the relative
  # path and we'll have the absolute path.
  FIRSTCHAR=`echo $DIRPART | cut -c 0-3`
  BASEDIR=$PWD

  # Shorten unnecessary long path
  while [ $FIRSTCHAR = "../" ]; do
      BASEDIR=`dirname $BASEDIR`
      DIRPART=`echo $DIRPART | sed 'sT^\.\./TT'`
      FIRSTCHAR=`echo $DIRPART | cut -c 0-3`
  done
  if [ $DIRPART = ".." ]; then
      DIRPART=`dirname $BASEDIR`
  elif [ $DIRPART = "." ]; then
      DIRPART=$BASEDIR
  else
      DIRPART=$BASEDIR/$DIRPART
  fi
fi

TRUEFILE=$DIRPART/`basename $0`

# Check if call was via soft link
#
while [ -h $TRUEFILE ]
# dereference links until we have path to this script
do TRUEFILE=`ls -l $TRUEFILE | sed 's/^.* -> //'`
#  echo Pass1: $TRUEFILE
  FIRSTCHAR=`echo $TRUEFILE | cut -c 0-1`
  if [ $FIRSTCHAR != "/" ]; then
#      echo Pass2: $TRUEFILE
      FIRSTCHAR=`echo $TRUEFILE | cut -c 0-3`
      while [ $FIRSTCHAR = "../" ]; do
	  DIRPART=`dirname $DIRPART`
	  TRUEFILE=`echo $TRUEFILE | sed 'sT^\.\./TT'`
	  FIRSTCHAR=`echo $TRUEFILE | cut -c 0-3`
      done
	# ok, it's a relative path, so add the DIRPART to the relative
	# path and we'll have the absolute path.
      TRUEFILE=$DIRPART/$TRUEFILE
  fi
done

# Now we should have absolute and compact script location
TRUEFILE=`dirname $TRUEFILE`
#
# One last thing, we want the parent of script location
#
BODILDIR=`dirname $TRUEFILE`

LIBCPPDIR=
#LIBCPPDIR=/opt/gcc/lib:
#if [ ! -z "${LIBCPPDIR}" ] ; then
#	LIBCPPDIR=/opt/lib/gcc322/${LIBCPPDIR}:
#	echo ${LIBCPPDIR}
#fi

##############################################################################
# Check wether the user already has a 'LD_LIBRARY_PATH'
#
if [ -z "${LD_LIBRARY_PATH}" ] ; then

	#
	# not defined, so define it
	LD_LIBRARY_PATH=${LIBCPPDIR}${BODILDIR}/lib/${ARCH}:${BODILDIR}/lib/plugins/${ARCH}
else

	#
	# already defined, append to the path
	LD_LIBRARY_PATH="${LD_LIBRARY_PATH}":${LIBCPPDIR}${BODILDIR}/lib/${ARCH}:${BODILDIR}/lib/plugins/${ARCH}
fi


if [ $ARCH = "irix" ]; then
    if [ -z "${LD_LIBRARYN32_PATH}" ] ; then
        LD_LIBRARYN32_PATH=${LIBCPPDIR}${BODILDIR}/lib/${ARCH}:${BODILDIR}/lib/plugins/${ARCH}
    else
        LD_LIBRARYN32_PATH="${LD_LIBRARYN32_PATH}":${LIBCPPDIR}${BODILDIR}/lib/${ARCH}:${BODILDIR}/lib/plugins/${ARCH}
    fi
    export LD_LIBRARYN32_PATH
fi

#############################################################################
if [ -x ${BODILDIR}/bin/${ARCH}/homodge ] ; then
    HOMODGE=${BODILDIR}/bin/${ARCH}/homodge
    export HOMODGE
fi

PATH=${BODILDIR}/bin/${ARCH}:${PATH}

#############################################################################
# Export the definitions to subsequent commands
#
export ARCH ARCHSTEREO HARDWARE BODILDIR LD_LIBRARY_PATH PATH BODIL_KEY


#############################################################################
# Finally execute Bodil
#
if [ -f /etc/redhat-release ] && grep -q Shrike /etc/redhat-release ; then
    # avoid NPTL on RH9
    LD_ASSUME_KERNEL=2.4.19 ${BODILDIR}/bin/${ARCH}/Bodil $* &
elif [ -f /etc/redhat-release ] && grep -q Yarrow /etc/redhat-release ; then
    # avoid NPTL on Fedora
    LD_ASSUME_KERNEL=2.4.19 ${BODILDIR}/bin/${ARCH}/Bodil $* &
else
    ${BODILDIR}/bin/${ARCH}/Bodil $* &
fi

