#! /usr/bin/env python
# (c) Copyright 2009-2012. CodeWeavers, Inc.

import os

# Portable which(1) implementation
def which(path, app):
    """Looks for an executable in the specified directory list.

    path is an os.pathsep-separated list of directories and app is the
    executable name. If app contains a path separator then path is ignored.
    If the file is not found, then None is returned.
    """
    if os.path.isabs(app):
        if os.path.isfile(app) and os.access(app, os.X_OK):
            return app
    elif os.sep in app or (os.altsep and os.altsep in app):
        app_path = os.path.join(os.getcwd(), app)
        if os.path.isfile(app_path) and os.access(app_path, os.X_OK):
            return app_path
    else:
        for directory in path.split(os.pathsep):
            if directory == "":
                continue
            app_path = os.path.join(directory, app)
            if os.path.isfile(app_path) and os.access(app_path, os.X_OK):
                return app_path
    return None

import sys
def locate_cx_root():
    """Locate where CrossOver is installed.

    We start by locating our own python script file and walking back up the
    path, traversing symbolic links on the way. Then we verify what we have
    found the right directory by checking for the presence of the cxmenu
    script.
    """
    # pylint: disable-msg=I0011,W0601,W0603
    global CX_ROOT
    if "CX_DEVELOP_ROOT" in os.environ:
        CX_ROOT = os.environ["CX_DEVELOP_ROOT"]
        return

    # figure out argv0
    argv0 = which(os.environ["PATH"], sys.argv[0])
    if not argv0:
        argv0 = sys.argv[0]
        if not os.path.isabs(argv0):
            argv0 = os.path.join(os.getcwd(), argv0)

    # traverse the symbolic links
    dir0 = os.path.dirname(argv0)
    while True:
        if dir0.endswith("/lib"):
            bindir = dir0[0:-3] + "bin"
        else:
            bindir = dir0
        landmark = os.path.join(bindir, "cxmenu")
        if os.path.isfile(landmark):
            break
        if not os.path.islink(argv0):
            break
        argv0 = os.readlink(argv0)
        if not os.path.isabs(argv0):
            argv0 = os.path.join(dir0, argv0)
        dir0 = os.path.dirname(argv0)

    # compute CX_ROOT
    CX_ROOT = os.path.dirname(os.path.normpath(bindir))

    # check CX_ROOT
    landmark = os.path.join(CX_ROOT, "bin", "cxmenu")
    if not os.path.isfile(landmark) or not os.access(landmark, os.X_OK):
        sys.stderr.write("%s:error: could not find CrossOver in '%s'\n" % (os.path.dirname(sys.argv[0]), CX_ROOT))
        sys.exit(1)

    sys.path.append(os.path.join(CX_ROOT, "lib", "python"))

locate_cx_root()
import cxutils
cxutils.CX_ROOT = CX_ROOT


import distversion
import traceback
import time

import checkgtk
if checkgtk.check_gtk(warn_gtk=False, warn_display=False) != checkgtk.OK:
    # Displaying the updatecheck dialog is not essential so just exit if we
    # cannot bring up the GUI.
    sys.exit(1)

import gobject
import gtk
import gtk.glade
gtk.gdk.threads_init()

import cxproduct
import cxlog
import cxopt

import cxguitools


class UsageDialogController(object):

    mainWindow = None
    xml = None

    modeldialogxml = None
    animateFunctions = []

    def __init__(self):

        try:
            gladepath = os.environ["CX_GLADEPATH"]
        except KeyError:
            gladepath = os.path.join(cxutils.CX_ROOT, "lib", "python", "glade")

        self.gladefile = os.path.join(gladepath, "usagedialog.glade")

        if not gtk.glade.textdomain() == "crossover":
            locale_path = os.path.join(CX_ROOT, "share", "locale")
            gtk.glade.bindtextdomain("crossover", locale_path)
            gtk.glade.textdomain("crossover")
        self.xml = gtk.glade.XML(self.gladefile)
        self.xml.signal_autoconnect(self)
        self.mainWindow = self.xml.get_widget('UsageDialog')

        try:
            self.xml.get_widget("UsageDialog").set_icon(
                cxguitools.get_std_icon('cxregister'))
        except gobject.GError:
            cxlog.warn("Couldn't load icon:\n%s" % traceback.format_exc())

    def usage_cancel(self, _caller):
        gtk.main_quit()

    def log_usage_clicked(self, _caller):
        cxproduct.save_setting('CrossOver', 'ReportWineUsage', '1')
        gtk.main_quit()

    def no_usage_clicked(self, _caller):
        cxproduct.save_setting('CrossOver', 'ReportWineUsage', '0')
        gtk.main_quit()

def prompt_for_usage_logging():
    UsageDialogController()


def main():
    # Parse the command line options
    opt_parser = cxopt.Parser(usage="%prog [--help]",
                              description="Checks for CrossOver updates.")
    (_options, args) = opt_parser.parse_args()
    if args:
        opt_parser.error("unexpected argument '%s'" % args[0])

    config = cxproduct.get_config()
    wconfig = config.get_save_config()
    if wconfig is None:
        # We won't be able to save the result so there's no point
        cxlog.err("found no writable configuration file")
        return 1
    wconfig.lock_file()

    try:
        next_hook = int(config['CrossOver'].get('NextUpdateHookDate', "0"))
    except ValueError:
        next_hook = 0
    now = time.time()
    if now < next_hook:
        # Another cxupdatecheck process is running already
        wconfig.save_and_unlock_file()
        return

    # Update the time for the next cxupdatecheck run
    wconfig['CrossOver']['NextUpdateHookDate'] = '%d' % int(now + 60*60*24)
    # We can now let other processes modify the configuration file
    wconfig.save_and_unlock_file()

    if distversion.REPORT_USAGE_URL != "":
        # Don't ask the first day
        if next_hook != 0 and 'ReportWineUsage' not in config['CrossOver']:
            cxguitools.set_default_icon('cxregister')
            prompt_for_usage_logging()
            gtk.main()

if __name__ == "__main__":
    sys.exit(main())
