#!/bin/bash
##
##  SPDX-FileCopyrightText: © 2007-2024 Benedict Verhegghe <bverheg@gmail.com>
##  SPDX-License-Identifier: GPL-3.0-or-later
##
##  This file is part of pyFormex 3.5  (Thu Feb  8 19:11:13 CET 2024)
##  pyFormex is a tool for generating, manipulating and transforming 3D
##  geometrical models by sequences of mathematical operations.
##  Home page: https://pyformex.org
##  Project page: https://savannah.nongnu.org/projects/pyformex/
##  Development: https://gitlab.com/bverheg/pyformex
##  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
#

get_versions() {
    compgen -c pyformex|grep -E '^pyformex(-[0-9.]+)?$'|sort|uniq
}

list_versions() {
    [ -z "$VERSIONS" ] && VERSIONS=$(getversions)
    echo "Available pyFormex versions:"
    for ver in $VERSIONS; do
	echo "  $ver"
    done
}

exec_version() {
    # Execute VERSION option
    VERSIONS=$(get_versions)
    if [ "$VERSION" = '?' ]; then
	list_versions
	exit 0
    else
	for ver in $VERSIONS; do
	    PYFVER=pyformex-$VERSION
	    if [ "$ver" = "$PYFVER" ]; then
		echo "Using $PYFVER"
		[ "$ECHO" = "echo" ] && PYOPTS="-D $PYOPTS"
		eval "$PYFVER $PYOPTS $@"
		exit $?
	    fi
	done
	echo "No such pyFormex version: $VERSION"
	list_versions
	exit 2
    fi
    exit 3
}

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

ECHO=":"   # do nothing command (implements -D option)
PYTHON=${PYTHON:-python3}     # Python interpreter to use
PYOPTS=    # Python interpreter options
VERSION=   # A specified pyFormex version

# Undocumented PRE-pyformex options
# -D    debug this script
# -V    show installed pyformex versions
# -Vver:  use pyFormex version ver
# anything but --*, -h, -c, -m: PYTHON options
# Python option arguments should be joined to the option like: -Wdefault


while [ "${1::1}" = "-" ]; do
    case $1 in
	-- ) shift; break ;;
	--* | -h | -c | -m ) break ;;
	-D ) ECHO="echo" ;;
	-V* ) VERSION="${1:2}"
	      [ -z "$VERSION" ] && VERSION='?'
	      ;;
	* ) PYOPTS="$PYOPTS $1" ;;
    esac
    shift
done

# Remaining args are passed to pyFormex

if [ -n "$VERSION" ]; then
    exec_version
fi

script=$(readlink -f $0)
scriptdir=$(dirname $script)
PYFORMEX_PATH=${PYFORMEX_PATH:-$(dirname $scriptdir)}
PYVER=$($PYTHON -V | sed 's/.* //')
export PYFORMEX_PATH

$ECHO "Script: $0"
$ECHO "Pid: $$"
$ECHO "Cmd: $(cat /proc/$$/cmdline | tr '\000' ' ')"
$ECHO "pyFormex start script: $script"
$ECHO "PYFORMEX_PATH: $PYFORMEX_PATH"
$ECHO "Python interpreter: $PYTHON"
$ECHO "Python version: $PYVER"
$ECHO "Python options: $PYOPTS"
$ECHO "pyFormex args: $@"

# Lowest Python version accepting syntax
REQVER=3.8
# 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 1
fi

MAIN=$PYFORMEX_PATH/pyformex/__main__.py
if [ -f "$MAIN" ]; then
    $ECHO "Running pyFormex from $PYFORMEX_PATH"
    set exec $PYTHON $PYOPTS $MAIN "$script" "$@"
elif [ -f "pyformex/__init__.py" ]; then
    $ECHO "Invalid installation type. Try to run the command from another directory."
    exit 1
else
    $ECHO "Running from a script $script"
    set exec $PYTHON $PYOPTS -s -m pyformex "$script" "$@"
fi
if [ "$ECHO" = "echo" ]; then
    printf 'Executing command:\n'
    printf '"%b" ' "$@"
    printf '\n'
fi
"$@"

# End
