#!/usr/bin/env python2.7
# vim: tabstop=4 softtabstop=4 shiftwidth=4 textwidth=80 smarttab expandtab
"""
* Copyright (C) 2011   Sangoma Technologies Corp.
* All Rights Reserved.
*
* Author(s)
* William Adam <william.adam@sangoma.com>
*
* This code is Sangoma Technologies Confidential Property.
* Use of and access to this code is covered by a previously executed
* non-disclosure agreement between Sangoma Technologies and the Recipient.
* This code is being supplied for evaluation purposes only and is not to be
* used for any other purpose.
"""
import sys
import os
import glob
import json
import re
from optparse import OptionParser

base = os.path.dirname(os.path.realpath(__file__))
# Create OUR libs path
lib_path = os.path.normpath(base + '/../libs')
# Append OUR lib to path
sys.path.append(lib_path)
# Ready to play with our libs :)

# Create logger
import logging
import logging.handlers

logger = logging.getLogger(os.path.basename(os.path.abspath(sys.argv[0])))
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
logger.addHandler(handler)


def main(argv):

    parser = OptionParser()
    # Define args
    parser.add_option("--package", dest="safe_package",
                      help="path to safepy python package")

    # Parse args
    (options, args) = parser.parse_args()

    # import the requested safepy module
    package_name = options.safe_package
    # default to product_release
    if package_name is None:
        package_name = 'product_release'

    # import the requested safepy module
    module = __import__(package_name)

    # create product
    product = module.Product()
    # TODO: need to be able to call 'application' instead of 'nsc'
    version = product.nsc.version.retrieve()

    # Banner
    print "Welcome to " + version['full_name'] + " - " + version['product_version']

    # Start interactive console
    import code
    code.interact(local=locals())


if __name__ == '__main__':
    try:
        sys.exit(main(sys.argv))
    except SystemExit:
        raise
    except Exception, e:
        sys.stderr.write("Exception caught: %s\n" % (e))
        raise
