#!/usr/bin/env python3
import sys
import os
try:
    DIR = os.path.abspath(os.path.normpath(os.path.join(__file__,
        '..', '..', 'tryton')))
    if os.path.isdir(DIR):
        sys.path.insert(0, os.path.dirname(DIR))
except NameError:
    pass

if hasattr(sys, 'frozen'):
    if not ('-v' in sys.argv or '--verbose' in sys.argv or
            '-l' in sys.argv or '--log-level' in sys.argv):
        sys.stdout = open(os.devnull, 'w')
        sys.stderr = open(os.devnull, 'w')
    prefix = os.path.dirname(sys.executable)
    os.environ['GTK_EXE_PREFIX'] = prefix
    os.environ['GTK_DATA_PREFIX'] = prefix
    os.environ['XDG_DATA_DIRS'] = os.path.join(prefix, 'share')
    etc = os.path.join(prefix, 'etc')
    os.environ['GDK_PIXBUF_MODULE_FILE'] = os.path.join(
            etc, 'gtk-3.0', 'gdk-pixbuf.loaders')
    os.environ['GTK_IM_MODULE_FILE'] = os.path.join(
            etc, 'gtk-3.0', 'gtk.immodules')
    os.environ['GI_TYPELIB_PATH'] = os.path.join(
            prefix, 'lib', 'girepository-1.0')
    if sys.platform == 'win32':
        # cx_freeze >= 5 put python modules under lib directory
        sys.path.append(os.path.join(prefix, 'lib'))
        os.environ.setdefault('SSL_CERT_FILE',
            os.path.join(etc, 'ssl', 'cert.pem'))
        os.environ.setdefault('SSL_CERT_DIR',
            os.path.join(etc, 'ssl', 'certs'))

    # On first launch the MacOSX app launcher may append an extra unique
    # argument starting with -psn_. This must be filtered to not crash the
    # option parser.
    if sys.platform == 'darwin':
        sys.argv = [a for a in sys.argv if not a.startswith('-psn_')]


# Disable dbusmenu to show second menu in tabs
os.environ['UBUNTU_MENUPROXY'] = '0'
# overlay-scrollbar breaks treeview height
os.environ['LIBOVERLAY_SCROLLBAR'] = '0'

from tryton.client import main
main()
