#!/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. 

# We locate where this script is so we can call the executables.
SCRIPT=`which "$0"`
DIR=`dirname "$SCRIPT"`
FUNCTION=`basename "$SCRIPT"`

# The default executable.
EXECUTABLE=4ti2int64

# 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

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

# We add the path $DIR to load shared libraries for the libglpk
# libgmp, and libgmpxx libraries.
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$DIR

"$DIR"/$EXECUTABLE $FUNCTION $@
