#!/bin/sh
##
##  This file is part of pyFormex 2.3  (Mon Feb 22 15:38:03 CET 2021)
##  pyFormex is a tool for generating, manipulating and transforming 3D
##  geometrical models by sequences of mathematical operations.
##  Home page: http://pyformex.org
##  Project page:  http://savannah.nongnu.org/projects/pyformex/
##  Copyright 2004-2020 (C) Benedict Verhegghe (benedict.verhegghe@ugent.be)
##  Distributed under the GNU General Public License version 3 or later.
##
##  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 3 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, see http://www.gnu.org/licenses/.
##

#
# pyFormex startup script
#

usage() {
    cat<<EOF

Usage: $(basename $0) [-D] [PYFORMEX OPTIONS]

Options:

D: switch on debugging info for this script

EOF
}

################################
# Process command line arguments
#

OPTIND=1
ECHO=":"    # do nothing command
OPTIONS=    # no extra options
PYVER=3     # Python version

while true; do
    case $1 in
	-D ) ECHO="echo" ;;
	-W ) OPTIONS="-Wd" ;;
	* ) break ;;
    esac
    shift
done

script=$(readlink -f $0)
scriptdir=$(dirname $script)
$ECHO "pyFormex start script: $script"
$ECHO "Arguments: $@"


PYTHON="python$PYVER"
PYVER=$($PYTHON -V | sed 's/.* //')
$ECHO "Python interpreter: $PYTHON"
$ECHO "Python version: $PYVER"

# Lowest Python version accepting syntax
REQVER='3.6.0'
# Find the lowest of two version strings in dotted format
LOWEST=$(printf "$PYVER\n$REQVER\n" | sort -V | head -n 1)
$ECHO "Minimal Python version: $LOWEST"
if [ "$LOWEST" = "$PYVER" ]; then
    # Python3 version too old
    echo "Alas, your version of Python is $PYVER."
    echo "However, pyFormex requires Python $REQVER or higher."
    echo "Please install a newer Python version to continue."
    exit
fi


if [ -f "$scriptdir/startup.py" ]; then
    # If we are executing the pyformex script from a pyFormex source tree,
    # make sure we always use the pyformex from that tree.
    #MYPYTHONPATH="$(dirname $scriptdir)"
    #[ -n "$PYTHONPATH" ] && MYPYTHONPATH="$MYPYTHONPATH:$PYTHONPATH"
    #$ECHO "MYPYTHONPATH=$MYPYTHONPATH"
    set exec $PYTHON $OPTIONS $scriptdir/startup.py "$script" "$@"
else
    # We are executing the script from an installed bin directory
    set exec $PYTHON $OPTIONS -s -m pyformex.startup "$script" "$@"
fi
if [ "$ECHO" = "echo" ]; then
    printf 'Executing command:\n'
    printf '"%b" ' "$@"
    printf '\n'
fi
"$@"

# End
