#!/bin/sh

# Changed for Debian/GNU: fix to Debian paths

# where Oz resides:

howcalled="$0"
cmd=`basename "$howcalled"`

OZHOME=/usr/share/mozart
: ${OZPLATFORM=`"/usr/lib/mozart/bin/ozplatform"`}
: ${OZEMULATOR="/usr/lib/mozart/platform/$OZPLATFORM/emulator.exe"}
: ${OZVERSION="1.4.0"}
: ${OZ_DOTOZ="$HOME/.oz/$OZVERSION"}
: ${OZ_LD_LIBRARY_PATH="$OZ_DOTOZ/platform/$OZPLATFORM/lib:/usr/lib/mozart/platform/$OZPLATFORM/lib${LD_LIBRARY_PATH:+:}${LD_LIBRARY_PATH}"}
: ${OZ_DYLD_LIBRARY_PATH="$OZ_DOTOZ/platform/$OZPLATFORM/lib:/usr/lib/mozart/platform/$OZPLATFORM/lib${DYLD_LIBRARY_PATH:+:}${DYLD_LIBRARY_PATH}"}
export OZHOME

LD_LIBRARY_PATH="$OZ_LD_LIBRARY_PATH"
export LD_LIBRARY_PATH
DYLD_LIBRARY_PATH="$OZ_DYLD_LIBRARY_PATH"
export DYLD_LIBRARY_PATH

# set OZPATH & PATH
if test -z "${OZ_PI}"
then
  # where Oz searches for files:
  if test -z "${OZPATH}"
  then
     OZPATH=.
  fi
  OZPATH="${OZPATH}:/usr/share/mozart/share"
  export OZPATH
  # increment path
  PATH="/usr/lib/mozart/bin:${PATH}"
  export PATH
  OZ_PI=1
  export OZ_PI
fi


case "$cmd" in

   oz)
	if test -z "${OZEMACS}"
	then
	    IFS="${IFS=   }"; saveifs="$IFS"; IFS="$IFS:"
	    for name in emacs xemacs lemacs; do
		for dir in $PATH; do
		    test -z "$dir" && dir=.
		    if test -f $dir/$name; then
			# Not all systems have dirname.
			OZEMACS=$dir/$name
			break 2
		    fi
		done
	    done
	    IFS="$saveifs"
	    if test -z "${OZEMACS}"
	    then
		echo "Cannot find emacs" 1>&2
		echo "Try setting environment variable OZEMACS" 1>&2
		exit 1
	    fi
	fi
	## Changed for Debian/GNU: elisp files will be found on default path
	exec "$OZEMACS" -l oz.elc -f run-oz "$@"
	;;

   ozengine)  # that is the real thing
	if test "$1" = '--gui'; then
	    OZGUI=--gui
	    shift
	else
	    OZGUI=
	fi
	if test $# -lt 1
	then
	    echo "usage: ozengine [--gui] <url> <args>" 1>&2;
	    exit 1;
	fi
	url=$1
	shift
	exec "$OZEMULATOR" $OZGUI -u "$url" -- "$@"
	;;

   oztool)
	## Changed for Debian/GNU
	exec sh "/usr/lib/mozart/platform/$OZPLATFORM/oztool.sh" "$@"
	;;
   text2pickle)
	## Changed for Debian/GNU
	exec "/usr/lib/mozart/platform/$OZPLATFORM/text2pickle.exe" "$@"
	;;

   pickle2text|oldpickle2text)
	## Changed for Debian/GNU
 	exec "$OZEMULATOR" --pickle2text "$@" | 
	      "/usr/lib/mozart/platform/$OZPLATFORM/text2pickle.exe" --textmode
	;;

   *)
	echo "Unknown invocation of Oz via '$cmd'" 1>&2
	exit 1
	;;
esac
