#!/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

# We have encountered a few systems where the default stack size is set to
# a remarkably large value, like 2GB.  Our process typically creates three
# threads, and super-large stack sizes prevent us from creating these helper
# threads because it would require at least 6GB of memory, and that's not 
# possible in a 32-bit process.  Ensure the stack size is something
# reasonable here.  Limits are in kilobytes, Linux default is ~10MB
current_stack_limit=`ulimit -s`
# if current limit is above 20MB, try to reduce it
if [ $current_stack_limit = unlimited ] || [ $current_stack_limit -gt 20480 ]; then
    ulimit -s 10240
    current_stack_limit=`ulimit -s`
    # if it's still above 100MB, warn
    if [ $current_stack_limit -gt 102400 ]; then
	echo "Warning: the current default stack size on this system ($current_stack_limit kB)"
	echo "is considerably larger than the default value of ~10240, and extremely"
	echo "large values will reduce the number of threads an application can create."
	echo "Please lower the limit to around 10MB using the 'ulimit' command"
	echo "before launching SynaptiCAD products to prevent errors."
    fi
fi

SYNCAD_HOME="$mypath"
export SYNCAD_HOME

SYNCAD_VERSION=19.00c
export SYNCAD_VERSION

DOTSYNCAD=$HOME/.SynaptiCAD/19.00
export DOTSYNCAD

TCL_LIBRARY="$SYNCAD_HOME/tcl/lib/tcl8.5"
export TCL_LIBRARY

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

#if [ -d "$SYNCAD_HOME/gcc-4.1.2-2.3.6-x86_64" ]; then
#    LD_LIBRARY_PATH="$SYNCAD_HOME/gcc-4.1.2-2.3.6-x86_64/x86_64-syncad-linux-gnu/sys-root/lib:$SYNCAD_HOME/gcc-4.1.2-2.3.6-x86_64/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/x86_64:$SYNCAD_HOME/bin/i686:$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH


if [ X$REQUIRESGCC = Xtrue ]; then
  PATH=$SYNCAD_HOME/gcc-x86_64/bin:$PATH
else
  PATH=$PATH:$SYNCAD_HOME/gcc-x86_64/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/x86_64/simxloader.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



