#!/bin/sh
#
# This script starts xcircuit under the Tcl interpreter,
# reading commands from a special .wishrc script which
# launches magic and retains the Tcl interactive interpreter.
#

export DISPLAY=:0
export PATH="$PATH:/usr/local/lib"

# Check for X11 server; start it if not running
chkserver=`ps | grep XWin | wc -l`
if [ $chkserver = 0 ]; then
   if [ -f /usr/X11R6/bin/XWin_GL.exe ]; then
      (/usr/X11R6/bin/XWin_GL -multiwindow &)
   elif [ -f /usr/X11R6/bin/XWin.exe ]; then
      (/usr/X11R6/bin/XWin -multiwindow &)
   else
      echo "X Server not running and cannot find server XWin or XWin_GL"
      exit 1
   fi
fi

TKCON=true

for i in $@ ; do
   case $i in
      -noc*) TKCON=;;
   esac
done

loclibdir=${XCIRCUIT_LIB_DIR:="/usr/local/lib/xcircuit-3.2"}
export XCIRCUIT_LIB_DIR

if [ $TKCON ]; then

   if [ ! -f ${loclibdir}/"tkcon.tcl" ]; then
      loclibdir=${loclibdir}/tcl
   fi

   exec ${loclibdir}/"tkcon.tcl" \
	-eval "source ${loclibdir}/"console.tcl"" \
        -slave "package require Tk; set argc $#; set argv [list $*]; \
        source ${loclibdir}/"xcircuit.tcl""
else

   export HOME=${loclibdir}
   if [ ! -f ${HOME}/.wishrc ]; then
      export HOME=${HOME}/tcl
   fi
   if [ ! -f ${HOME}/.wishrc ]; then
      echo "Error: no .wishrc startup file.  Check xcircuit installation"
      echo "or setenv XCIRCUIT_LIB_DIR (default ${loclibdir}) appropriately."
   fi 
#
# Make sure that the following points to your current wish
# executable, whereever it may be (this version assumes that
# "wish" is in the current search path).
#
   exec /usr/local/bin/wish -- $@

fi
