#! /bin/sh
######################################################
#
#  SaVi by Lloyd Wood (lloydwood@users.sourceforge.net),
#          Patrick Worfolk (worfolk@alum.mit.edu) and
#          Robert Thurman.
#
#  Copyright (c) 1997 by The Geometry Center.
#  Also Copyright (c) 2006 by Lloyd Wood.
#
#  This file is part of SaVi.  SaVi is free software;
#  you can redistribute it and/or modify it only under
#  the terms given in the file COPYRIGHT which you should
#  have received along with this file.  SaVi may be
#  obtained from:
#  http://savi.sourceforge.net/
#  http://www.geom.uiuc.edu/locate/SaVi 
#
######################################################
#
# Script that is always used to launch the binary SaVi executable
#
# $Id: savi,v 1.24 2006/07/17 04:37:47 lloydwood Exp $

#
# If SAVI environment variable is not defined
# then set it to be the path of this file
#
# If you copy this script from the SaVi installation and run it elsewhere,
# then you should uncomment the following line:
# SAVI=/usr/local/savi
# and replace /usr/local/savi with the location of
# your SaVi installation.
#
: ${SAVI:=`dirname "$0"`}
export SAVI


#
# Set locations of tcl and tk library code needed at runtime
# Cygwin is a special case, and we set for that further down
# The version numbers here should match the versions used in
# the src/Makefile_defs.ARCH file used to compile SaVi.
#
TCL_LIBRARY=/usr/local/lib/tcl8.4
TK_LIBRARY=/usr/local/lib/tk8.4


#
# Default executable relative location and name
#
SAVIBIN=bin/savi-bin


#
# Check for known machine types
#
if [ -f /etc/debian_version ]; then
  ARCH=debian
elif [ -f /vmlinuz -o -f /boot/vmlinuz ]; then
  ARCH=linux
elif [ -d /devices ]; then
  ARCH=sun
elif [ -d /usr/bsd -a -d /usr/sbin ]; then
  ARCH=irix
elif [ -d /cygdrive ]; then
  ARCH=cygwin
elif [ -d /sys/boot ]; then
  ARCH=freebsd
else
  ARCH=unknown
fi

params=""

if [ ${ARCH} = "cygwin" ]; then
  # Cygwin installs Insight Tcl/Tk here.
  TCL_LIBRARY=/usr/share/tcl8.4
  TK_LIBRARY=/usr/share/tk8.4
fi

#
# Sanity check $SAVI location
#
if [ ! -f "$SAVI/savi" ]; then
  echo "SaVi: a copy of this script is not at $SAVI/savi. Is \$SAVI set correctly?" >&2
fi

#
# Verify that tcl and tk directories exist
#
EDIT=no

if [ ! -d "$TCL_LIBRARY" ]; then
  echo "SaVi: specified TCL_LIBRARY $TCL_LIBRARY was not found." >&2
  echo "      Does Tcl need to be installed?" >&2
  EDIT=yes
fi

if [ ! -d "$TK_LIBRARY" ]; then
  echo "SaVi: specified TK_LIBRARY $TK_LIBRARY was not found." >&2
  echo "      Does Tk need to be installed?" >&2
  EDIT=yes
fi

if [ ${EDIT} = "yes" ]; then
  echo "      edit this script to set LIBRARY locations. Read the comments." >&2
fi

export TCL_LIBRARY
export TK_LIBRARY

#
# Do we need to complain that SaVi has not been compiled properly?
#
COMPLAIN=no
MAKEFILE=no

#
# Try specific binary, then default to filename without extension
#
if [ ! ${ARCH} = "unknown" ]; then
  if [ -f "${SAVI}/${SAVIBIN}.${ARCH}" ]; then
    # found the platform-specific executable
    SAVIBIN=$SAVIBIN.$ARCH
  else
    echo "SaVi: couldn't find platform-specific binary $SAVI/$SAVIBIN.$ARCH" >&2
    echo "      looking for generic executable binary $SAVI/$SAVIBIN" >&2
    if [ ! -f "${SAVI}/${SAVIBIN}" ]; then
      echo "SaVi: no executable binary was found." >&2
      COMPLAIN=yes
      if [ ! -f "${SAVI}/src/main.o" ]; then
         echo "SaVi: can't see expected main.o object file in the $SAVI/src/ directory." >&2
         echo "      Has the master Makefile been run to call the C Makefile?" >&2
         MAKEFILE=yes
      fi
    fi
  fi
fi

if [ ! -f "${SAVI}/tcl/tclIndex" ]; then
      echo "SaVi: couldn't find generated list of Tcl commands in $SAVI/tcl/tclIndex" >&2
      echo "      Has the master Makefile been run to call the Tcl Makefile?" >&2
      MAKEFILE=yes
      COMPLAIN=yes
fi

if [ ${MAKEFILE} = "yes" ]; then
  echo "      The master Makefile in $SAVI must run all other makefiles to make SaVi work." >&2
fi

if [ ${COMPLAIN} = "yes" ]; then
  echo "SaVi: Try compiling SaVi by making in the same directory as this script:" >&2
  echo "        cd $SAVI" >&2
  echo "        make ARCH=$ARCH" >&2
  echo "      before running this script again to launch and run the savi-bin binary." >&2
fi

#
# Find what directory this script was invoked from
#
OLDCWD=`pwd`; export OLDCWD

#
# Change directory
#
cd "$SAVI"

#
# See if we need to add a -geomview flag
#
if [ -t 1 ]; then
  exec > /dev/null
else
  case "$*" in
    *-geomview*) ;;
    *) set -- -geomview $*;;
  esac

  # send SAVI env variable to geomview
  echo "(setenv SAVI \"$SAVI\")"
fi

#
# Run binary
#
${SAVIBIN} $* ${params}
