#!/bin/sh

# 4ti2 -- A software package for algebraic, geometric and combinatorial
# problems on linear spaces.
# 
# Copyright (C) 2006 4ti2 team.
# Main author(s): Peter Malkin
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 

prefix=/usr
PKGLIBEXECDIR=/usr/libexec/i386-linux-gnu/4ti2

# We locate where this script is so we can call the executables.
SCRIPT=${0}
SCRIPTDIR=${SCRIPT%/*}

# We deduce the function from the script name.
FUNCTION=${SCRIPT##*/}
FUNCTION=${FUNCTION#4ti2-}

# The default executable.
EXECUTABLE=4ti2gmp

# We look for options on the command line which indicate the level of precision
# required and we call the appropriate 4ti2 executable according to the required
# precision level. The short option is `-p' and the long option is
# `--precision', and the argument to either is one of 32, 64, or `arbitrary'.
# The following regular expressions are not exactly correct since for example
# they allow misspellings of precision. However, they will match correctly
# formatted input and if the option is incorrectly formatted, the actual
# executable will pick up any errors.
if  echo $@ | grep -E -q -e '-p *32 ' ||
    echo $@ | grep -E -q -e '--p[recision]* *=? *32 '
then
    EXECUTABLE=4ti2int32
elif echo $@ | grep -E -q -e '-p *64 ' ||
     echo $@ | grep -E -q -e '--p[recision]* *=? *64 '
then
    EXECUTABLE=4ti2int64
elif echo $@ | grep -E -q -e '-p *a[rbitrary]* ' ||
     echo $@ | grep -E -q -e '--p[recision]* *=? *a[rbitrary]* '
then
    EXECUTABLE=4ti2gmp
fi

for DIR in "$SCRIPTDIR" "$PKGLIBEXECDIR/bin"; do
	if [ -x "$DIR/4ti2gmp" ]; then break; fi
done

# We check whether the 4ti2 executable exists.
if [ ! -x "$DIR/$EXECUTABLE" ]
then
    echo "Error: Unable to find 4ti2 executable \`$EXECUTABLE'"
    exit 1
fi

exec $DIR/$EXECUTABLE $FUNCTION $@
