#!/usr/bin/env python

# PyBridge -- online contract bridge made easy.
# Copyright (C) 2004-2007 PyBridge Project.
#
# 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; either version 2
# of the License, or (at your option) any later version.
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.


import os
import sys
from optparse import OptionParser


def main():
    PYTHON_VERSION = sys.version_info[:2]

    # Some version dependencies.
    PYTHON_REQUIRED = (2, 3)
    PYGTK_REQUIRED = '2.0'
    TWISTED_REQUIRED = '2.0.0'

    # Check version requirements.

    if PYTHON_VERSION < PYTHON_REQUIRED:
        raise SystemExit, "Error: Python %d.%d+ required" % PYTHON_REQUIRED

    if hasattr(sys, 'frozen'):  # For py2exe distribution.
        os.environ['PATH'] += ";lib;"
    else:
        import pygtk
        try:
            pygtk.require(PYGTK_REQUIRED)
        except AssertionError:
            raise SystemExit, "Error: PyGTK %s+ required" % PYGTK_REQUIRED

    import twisted.copyright
    if twisted.copyright.version < TWISTED_REQUIRED:
        raise SystemExit, "Error: Twisted Core %s+ required" % TWISTED_REQUIRED

    try:  # If PyBridge is installed system-wide, this finds it automatically.
        import pybridge
    except ImportError:  # Locate the PyBridge package.
        currentdir = os.path.dirname(os.path.abspath(sys.argv[0]))
        basedir = os.path.abspath(os.path.join(currentdir, '..'))
        # The package path should be relative to the base directory.
        if os.path.exists(os.path.join(basedir, 'lib', 'python')):
            pythonpath = os.path.join(basedir, 'lib', 'python')
        else:
            pythonpath = basedir

        sys.path.insert(0, pythonpath)  # Place PyBridge package in PYTHONPATH.
        try:
            import pybridge
        except ImportError:
            raise SystemExit, "Fatal error: could not locate PyBridge installation."

    parser = OptionParser(version="PyBridge %s" % pybridge.__version__)
    options, args = parser.parse_args()

    import pybridge.ui
    pybridge.ui.run()


if __name__ == '__main__':
    main()

