#!/bin/sh
##
##  This file is part of pyFormex 2.0  (Mon Sep 14 12:29:05 CEST 2020)
##  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
#
# This scripts allows (and strips) the options:   [ -D ]   [ -2 | -3 ]
# -D: switch on debugging info for this script
# -2 | -3: force Python2 or Python3 command interpreter
# All remaining command line argument are passed to pyFormex.
#

if [ "$1" = "-D" ]; then
    ECHO="echo"
    shift
else
    ECHO=":"  # do nothing
fi

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

if [ "$1" = "-2" ]; then
    PYVER=2
    shift
elif [ "$1" = "-3" ]; then
    PYVER=3
    shift
else
    PYVER=${PYFORMEX_PYTHON}
fi

if [ "$PYVER" = "2" ]; then
    # it was requested, either with -2 or setting PYFORMEX_PYTHON
    echo "This version of pyFormex requires Python3."
    echo "It is not possible to run it with Python2."
    echo "See the file 'p3.rst' for info on migrating"
    echo "to the Python3 version."
    exit
fi
PYVER=3

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 $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 $scriptdir/startup.py "$script" "$@"
else
    # We are executing the script from an installed bin directory
    set exec $PYTHON -s -m pyformex.startup "$script" "$@"
fi
if [ "$ECHO" = "echo" ]; then
    printf 'Executing command:\n'
    printf '"%b" ' "$@"
    printf '\n'
fi
"$@"

# End
