#!/bin/sh
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# shell script to find a suitable Python interpreter and run cqlsh.py

# parse arguments
PARAMS=""

# Use the Python that is specified in the env
if [ -n "$CQLSH_PYTHON" ]; then
    USER_SPECIFIED_PYTHON="$CQLSH_PYTHON"
fi

while [ $# -gt 0 ]; do
    case "$1" in
        --python)
            if [ $# -lt 2 ]; then
                echo "You must specify a python interpreter path with the --python option"
                exit 1
            fi
            USER_SPECIFIED_PYTHON="$2"
            shift
            shift
            ;;
        --)
            shift
            break
            ;;
        *)
            PARAMS="$PARAMS $1"
            shift
            ;;
    esac
done

# get a version string for a Python interpreter
get_python_version() {
    interpreter=$1
    version=$(command -v "$interpreter" > /dev/null 2>&1 && $interpreter -c "import os; print('{}.{}'.format(os.sys.version_info.major, os.sys.version_info.minor))")
    echo "$version"
}

# test whether a version string matches one of the supported versions for cqlsh
is_supported_version() {
    version=$1
    if [ "$version" = "3.6" ] || [ "$version" = "2.7" ]; then
        echo "supported"
    else
        echo "unsupported"
    fi
}

run_if_supported_version() {
    interpreter="$1"
    params="$2"
    version=$(get_python_version "$interpreter")
    if [ -n "$version" ]; then
        if [ "$(is_supported_version "$version")" = "supported" ]; then
            # We need the params to be unquoted, otherwise the shell will just interpret it as one giant string
            # shellcheck disable=SC2086
            exec "$interpreter" "$($interpreter -c "import os; print(os.path.dirname(os.path.realpath('$0')))")/cqlsh.py" $params
            exit
        fi
    fi
}


if [ "$USER_SPECIFIED_PYTHON" != "" ]; then
    # run a user specified Python interpreter
    run_if_supported_version "$USER_SPECIFIED_PYTHON" "$PARAMS"
else
    # try unqualified python first, then python3, then python2.7
    for interpreter in python python3 python2.7; do
        run_if_supported_version "$interpreter" "$PARAMS"
    done
fi

echo "No appropriate Python interpreter found." >&2
exit 1
