#!/usr/bin/env bash

# Bi-lingual shellscript/python file. When this file is ran as an executable with ./iota-status, it tries to determine the
# highest version of python which has privex-iota installed.
# Once the highest version has been found, this script simply executes itself using Python.

# Original source of bilingual script: https://stackoverflow.com/a/47886254/2648583
# Shell commands follow
# Next line is bilingual: it starts a comment in Python, and is a no-op in shell
""":"

: ${DEBUG=0}

# Find a suitable python interpreter, newest first.
for cmd in python3.9 python3.8 python3.7 python3.6 python3 ; do
    (( DEBUG )) && >&2 echo " [DBG] Checking if we have $cmd"
    
    if command -v "$cmd" &>/dev/null; then
       (( DEBUG )) &&  >&2 echo " [DBG] Found interpreter $cmd - checking if privex-iota is installed"
       
        # Make sure the privex-iota package is actually installed on that python version before we use it
        if env "$cmd" -m pip freeze 2>/dev/null | grep -q "privex-iota"; then
            (( DEBUG )) && >&2 echo " [DBG] SUCCESS - interpreter $cmd has privex-iota installed. Using $cmd"
            
            # Re-execute this script using the appropriate Python interpreter.
            exec $cmd $0 "$@"
        fi
    fi
done

>&2 echo -e "\n [!!!] CRITICAL ERROR: No python interpreter could be found which has the package 'privex-iota' installed...\n"

exit 2

":"""
# Previous line is bilingual: it ends a comment in Python, and is a no-op in shell
# Shell commands end here
# Python script follows (example commands shown)

"""
This Python binary is shipped as part of https://github.com/Privex/iota-tools

This binary should be automatically updated by the Python package manager: pip

To update:

    # Assuming you use Python 3.8
    python3.8 -m pip install -U privex-iota

Copyright::

    +===================================================+
    |                 © 2020 Privex Inc.                |
    |               https://www.privex.io               |
    +===================================================+
    |                                                   |
    |        Privex's Random IOTA Tools                 |
    |        License: X11/MIT                           |
    |                                                   |
    |        Core Developer(s):                         |
    |                                                   |
    |          (+)  Chris (@someguy123) [Privex]        |
    |                                                   |
    +===================================================+

    Privex's Random IOTA Tools
    Copyright (c) 2020    Privex Inc. ( https://www.privex.io )

    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
    files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
    modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all copies or substantial portions of
    the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
    WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
    OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

    Except as contained in this notice, the name(s) of the above copyright holders shall not be used in advertising or
    otherwise to promote the sale, use or other dealings in this Software without prior written authorization.


"""
from privex.iota import iota_status_app

iota_status_app()

