#!/usr/bin/env python
#     Copyright 2012, Kay Hayen, mailto:kayhayen@gmx.de
#
#     Part of "Nuitka", an optimizing Python compiler that is compatible and
#     integrates with CPython, but also works on its own.
#
#     If you submit patches or make the software available to licensors of
#     this software in either form, you automatically them grant them a
#     license for your part of the code under "Apache License 2.0" unless you
#     choose to remove this notice.
#
#     Kay Hayen uses the right to license his code under only GPL version 3,
#     to discourage a fork of Nuitka before it is "finished". He will later
#     make a new "Nuitka" release fully under "Apache License 2.0".
#
#     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, version 3 of the License.
#
#     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/>.
#
#     Please leave the whole of this copyright notice intact.
#

"""

This is the main program of Nuitka, it checks the options and then translates one or more
modules to a C++ source code using Python C/API in a build directory compiles it to either
an executable or an extension module.

"""

import sys, os

# LIBDIR trick start (marker for removal on platforms that don't need it)
libdir = '@LIBDIR@'

# Two cases:
if libdir != '@' 'LIBDIR' '@':
    # Changed by our distutils hook, then use the given path.

    if not os.path.isabs( libdir ):
        libdir = os.path.join( os.path.dirname( os.path.realpath( __file__ ) ), libdir )
        libdir = os.path.abspath( libdir )

    sys.path.insert( 0, libdir )
else:
    # Unchanged, running from checkout, use the parent directory, the nuitka package ought be there.
    sys.path.insert( 0, os.path.join( os.path.dirname( __file__ ), ".." ) )
# LIBDIR trick end (marker for removal on platforms that don't need it)

import logging
logging.basicConfig( format = 'Nuitka:%(levelname)s:%(message)s' )

from nuitka import MainControl, SyntaxErrors, Options

positional_args = Options.getPositionalArgs()

assert len( positional_args ) > 0

filename = Options.getPositionalArgs()[0]

# Turn that source code into a node tree structure.
try:
    tree = MainControl.createNodeTree(
        filename = filename
    )
except (SyntaxError, IndentationError) as e:
    sys.exit( SyntaxErrors.formatOutput( e ) )

if Options.shallDumpBuiltTree():
    MainControl.dumpTree( tree )
elif Options.shallDumpBuiltTreeXML():
    MainControl.dumpTreeXML( tree )
elif Options.shallDisplayBuiltTree():
    MainControl.displayTree( tree )
else:
    MainControl.compileTree( tree )
