#!/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
    mypath=`dirname $mypath`
    # 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=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
# In earlier versions, this script used to detect whether to run 32-bit or 
# 64-bit simx, based on the platform.  Now we only support 64-bit simx, 
# so this script just runs the 64-bit version of simx directly.

exec $SYNCAD_HOME/bin/simx64 "$@"
