#! /bin/sh

# Walk the octave-forge tree (starting in the root) searching for 
# all the directories that are supposed to be installed.
# Add each directory and any data subdirectories to the LOADPATH for octave.
# Add any bin directories to the EXEC_PATH.
# Add any DLL directories to the system PATH.
# Set LD_LIBRARY_PATH and DYLD_LIBRARY_PATH for any .so and .dylib files found.
#
# You must call this from the root of the octave-forge tree, using, e.g.,
# 	admin/run_forge octave --norc -q
#
# Normally this is called for you, using the run target from make:
#       make run

ROOT=`pwd`
BINPATH="$PATH"
LDPATH="$LD_LIBRARY_PATH"
DYLDPATH="$DYLD_LIBRARY_PATH"
OCTPATH="$OCTAVE_PATH"
# XXX FIXME XXX strictly speaking, the default octave bin directories
# should be in the middle of EXECPATH and PATH, but it should be safe
# to put them at the end since octave probably isn't overriding anything
# on the system.  We may also want to pick up the OCTAVE_EXEC_PATH if
# there is one.
EXECPATH="$PATH:"
for f in FIXES main/* extra/* nonfree/*; do
    # exclude CVS directories, only include directories, skip NOINSTALL
    if test $f = ${f%/CVS} -a -d $f -a ! -f $f/NOINSTALL; then 
	OCTPATH="$ROOT/$f:$OCTPATH"

	# if there is install data, include in on the path
	if test -d "$f/data"; then OCTPATH="$ROOT/$f/data:$OCTPATH"; fi

	# make sure we can find supporting binaries
	if test -d "$f/bin"; then EXECPATH="$ROOT/$f/bin:$EXECPATH"; fi
	if test -d "$f/scripts"; then EXECPATH="$ROOT/$f/scripts:$EXECPATH"; fi

	# supporting libraries need to be available as well
	check=`echo $f/*.dll`
	if test "$check" != "$f/*.dll"; then BINPATH="$ROOT/$f:$BINPATH"; fi
	check=`echo $f/*.so`
	if test "$check" != "$f/*.so"; then LDPATH="$ROOT/$f:$LDPATH"; fi
	check=`echo $f/*.dylib`
	if test "$check" != "$f/*.dylib"; then DYLDPATH="$ROOT/$f:$DYLDPATH"; fi
    fi
done
export LD_LIBRARY_PATH="$LDPATH" 
export DYLD_LIBRARY_PATH="$DYLDPATH" 
export PATH="$BINPATH" 
export OCTAVE_PATH="$OCTPATH"
export OCTAVE_EXEC_PATH="$EXECPATH"
$*
