#!/usr/bin/env python
#
#   Simple Backup - Launcher script for indicator application (status icon)
#
#   Copyright (c)2009-2010: Jean-Peer Lorenz <peer.loz@gmx.net>
#
#   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 St, Fifth Floor, Boston, MA  02110-1301  USA


def parse_cmdline(argv):
    import optparse
    from sbackup.pkginfo import Infos
    from sbackup.util import constants
    
    usage = "Usage: %prog [options] (use -h or --help for more infos)"
    version = "%prog " + Infos.VERSION
    prog = constants.INDICATORAPP_FILE

    parser = optparse.OptionParser(usage = usage, version = version, prog = prog)
    parser.add_option("--keep-alive",
              action = "store_true", dest = "keep_alive", default = False,
              help = "don't terminate indicator if no backup/D-Bus service is present")

    parser.add_option("--legacy",
              action = "store_true", dest = "legacy_appindicator", default = False,
              help = "use legacy status icon instead of `libappindicator`")

    (options, args) = parser.parse_args(argv[1:])
    if len(args) != 0:
        parser.error("Unknown argument given")

    return options
    
    
if __name__ == '__main__':
    import sys
    import os
    from sbackup.util import system

    from sbackup.util import system
    system.set_default_environment()
    system.set_display_from_session()
    system.set_gio_env_from_session()    

    options = parse_cmdline(sys.argv)
    os.nice(5)

    appindicator_avail = False
    _session_user = system.get_user_from_env()

    if system.is_superuser():
        print "Indicator application was started as superuser (EUID=0).\n"\
              "Now dropping privileges (to user '%s')." % _session_user
        system.switch_user(uid_name = _session_user)

    if system.get_user_from_uid() != _session_user:
        print "Unable to launch indicator application as current user.\n"\
              "You must own current desktop session."
        sys.exit(1)


    from sbackup.util import get_locale_dir, get_locale_domain
    application = get_locale_domain()
    locale_dir = get_locale_dir()

    import gettext
    gettext.bindtextdomain(application, locale_dir)
    gettext.textdomain(application)

    import gtk.glade
    gtk.glade.bindtextdomain(application, locale_dir)
    gtk.glade.textdomain(application)

    try:
        import appindicator
        appindicator_avail = True
    except ImportError:
        appindicator_avail = False

    if options.legacy_appindicator is True:
        from sbackup.ui.indicator_legacy import main
    else:
        if appindicator_avail:
            from sbackup.ui.indicator import main
        else:
            print "Unable to use `libappindicator`. Falling back to legacy status icon."
            from sbackup.ui.indicator_legacy import main


    exitcode = main(options)
    sys.exit(exitcode)
