#!/bin/bash
#
# cot - Main executable for the Common OVF Tool suite
#
# August 2013, Glenn F. Matthews
# Copyright (c) 2013-2014 the COT project developers.
# See the COPYRIGHT.txt file at the top-level directory of this distribution
# and at https://github.com/glennmatthews/cot/blob/master/COPYRIGHT.txt.
#
# This file is part of the Common OVF Tool (COT) project.
# It is subject to the license terms in the LICENSE.txt file found in the
# top-level directory of this distribution and at
# https://github.com/glennmatthews/cot/blob/master/LICENSE.txt. No part
# of COT, including this file, may be copied, modified, propagated, or
# distributed except according to the terms contained in the LICENSE.txt file.

''':'
# Find the current working directory and source any helper file provided
dir="$( dirname "$0" )"
if [ -f "$dir/cot_helper.sh" ]; then
  . "$dir/cot_helper.sh"
fi
# Now proceed to find the right python version...
if type python3 >/dev/null 2>/dev/null; then
  exec python3 "$0" "$@"
elif type python2.7 >/dev/null 2>/dev/null; then
  exec python2.7 "$0" "$@"
elif type python-2.7.1 >/dev/null 2>/dev/null; then
  exec python-2.7.1 "$0" "$@"
else
  exec python "$0" "$@"
fi
'''

import sys
# We require Python 2.7
if not hasattr(sys, "hexversion") or sys.hexversion < 0x020700f0:
    sys.stderr.write("Your version of python is too old:\n")
    sys.stderr.write(sys.executable)
    sys.stderr.write("\n")
    sys.stderr.write(sys.version)
    sys.stderr.write("\nPython 2.7 or later is required to run COT\n")
    sys.exit(1)
# Different servers may have different Python versions,
# which may generate different compiled bytecode, causing errors
# when a user on a different server tries to load it.
# Hence, we have to take the slight performance hit of never saving the
# compiled bytecode to disk. :-(
sys.dont_write_bytecode = True

# Fix up sys.path to point to the COT module
import os
path = os.path.abspath(sys.argv[0])
while os.path.dirname(path) != path:
    if os.path.exists(os.path.join(path, 'COT', '__init__.py')):
        sys.path.insert(0, path)
        break
    path = os.path.dirname(path)

# Set up the CLI
import COT.cli

if __name__ == "__main__":
    COT.cli.main()
