#!/bin/sh

# This script locates the directory it is installed in, and sets up
# environment variables to point to that directory.  

# locate the directory in which we are installed
mypath=
case $0 in
    /*) mypath=$0;;
    */*) mypath=$0;;
    *)  old_ifs=$IFS
        IFS=:
        # normalize the path
        # (there's a special case where, if the path begins or ends
        # with a colon, it is equivalent to the path beginning or
        # ending with a "."
        search_path=`echo $PATH | sed 's/::/:.:/g;s/^:/.:/;s/:$/:./'`
        for sp in $search_path; do
          if [ -x "$sp/$0" ]; then
            mypath="$sp/$0"
            break
          fi
        done
        IFS=$old_ifs
        ;;
esac

if [ x$mypath = x ]; then
    echo "Error -- unable to determine the directory in which $0 is installed"
    exit 1
else
    # mypath is now the name of the executable
    # we want to strip off the executable name and the 'bin' directory
    # to get the top-level directory we're installed in
    mypath=`dirname $mypath`
    mypath=`cd $mypath; pwd`
    myimmediatedir=`basename $mypath`
    if [ x$myimmediatedir != xbin ]; then
      echo "Error -- I was expecting to be installed in a directory named 'bin'"
      exit 1
    fi
    paths_to_remove=1
    paths_removed=0
    while [ $paths_removed -lt $paths_to_remove ]; do
      mypath=`dirname $mypath`
      paths_removed=`expr $paths_removed + 1`
    done
    # by this point, mypath has to be valid, even if it is /
fi

if [ X`uname -s` = XLinux ]; then
	if [ -x /lib/libc.so.6 ]; then
		glibc_version=`/lib/libc.so.6 |head -1 | sed 's/.*version '//g | cut -f1 -d,`
		case "$glibc_version" in
			*2.2.93*)
				echo "Warning: SynaptiCAD products will not run correctly with glibc version 2.2.93"
				echo "which you appear to have installed.  You must upgrade to a newer version."
				echo "See details in the README file"
				;;
			*);;
		esac
	fi
fi

SYNCAD_HOME="$mypath"
export SYNCAD_HOME

SYNCAD_VERSION=17.01o
export SYNCAD_VERSION

DOTSYNCAD=$HOME/.SynaptiCAD/17.01
export DOTSYNCAD

if [ ! -d $DOTSYNCAD ]; then
    mkdir -p $DOTSYNCAD
fi

#if [ -d "$SYNCAD_HOME/gcc-4.1.2-2.3.6-i686" ]; then
#    LD_LIBRARY_PATH="$SYNCAD_HOME/gcc-4.1.2-2.3.6-i686/i686-syncad-linux-gnu/sys-root/lib:$SYNCAD_HOME/gcc-4.1.2-2.3.6-i686/lib:$LD_LIBRARY_PATH"
#fi

# if REQUIRESGCC is set, we will put the path to our copy of gcc
# at the beginning of the PATH to force our software to use the version
# of gcc we ship with.  This should be true for our simulator.
# If false, we'll put our copy of gcc at the end of the PATH so we'll
# default to using the system's gcc unless it's missing. This should be
# the case for the GUI and simwrap, to allow SystemC and sugar
# compilations to use the builtin gcc.
REQUIRESGCC=false

LD_LIBRARY_PATH="$SYNCAD_HOME/lib:$SYNCAD_HOME/bin/i686:$SYNCAD_HOME/bin/i686:$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH


if [ X$REQUIRESGCC = Xtrue ]; then
  PATH=$SYNCAD_HOME/gcc-4.1.2-2.3.6-i686/bin:$PATH
else
  PATH=$PATH:$SYNCAD_HOME/gcc-4.1.2-2.3.6-i686/bin
fi

PATH=$SYNCAD_HOME/bin:$PATH
export PATH

if [ "X$LANG" = "X" -a "X$LC_ALL" = "X" ]; then
    LC_ALL=en_US
    export LC_ALL
fi

# determine the application name
appname=$SYNCAD_HOME/bin/i686/lminterface.bin
apparg=

if [ -x "$appname" ]; then
  if [ "X$1" = "X--debugenv" ]; then
    SYNCAD_INSIDE_DEBUG_ENVIRONMENT=1
    export SYNCAD_INSIDE_DEBUG_ENVIRONMENT
    echo "Launching a new shell with the environment configured for SynaptiCAD"
    echo "command-line products."
    echo "Type 'exit' to leave this debugging environment and return to your "
    echo "normal environment"
    $SHELL
    echo "Leaving SynaptiCAD environment"
    exit 0
  elif [ "X$1" = "X--debugcmd" ]; then
    if [ "X$2" = "X" ]; then
      echo "You must give a command to run with --debugcmd"
      exit 1
    else
      SYNCAD_INSIDE_DEBUG_ENVIRONMENT=1
      export SYNCAD_INSIDE_DEBUG_ENVIRONMENT
      echo "Launching command \"$2\" in the SynaptiCAD environment"
      $2
      echo "Done running command \"$2\", leaving SynaptiCAD environment"
      exit 0
    fi
  fi
  exec "$appname" $apparg "$@"
fi

echo "Error -- Unable to find a complete installation"
exit 1



