#!/usr/bin/python3

from SystemUpdater.UpdateManager import UpdateManager
from gettext import gettext as _
import logging
from optparse import OptionParser
import dbus
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
import signal
import os
import sys
import gettext

from SystemUpdater.Core.LogManager import get_logfile as logfile

gettext.bindtextdomain('kylin-system-updater', '/usr/share/locale')
gettext.textdomain('kylin-system-updater')
_ = gettext.gettext

#定义日志的格式 
FORMAT = "%(asctime)s [%(levelname)s]: %(message)s"

FORMAT_DEBUG = '%(asctime)-15s %(levelname)s(%(filename)s:%(lineno)d):%(message)s'

def signal_handler_term(signal, frame):
    # type: (int, object) -> None
    logging.warning("SIGTERM received, will stop")
    app.dbus_send.Quit(None)

if __name__ == "__main__":
  # Begin parsing of options
  parser = OptionParser()
  parser.add_option ("-d", "--debug", action="store_true", default=False,
                    help=_("Show debug messages"))
  parser.add_option("-r", "--replace",
                    default=False,
                    action="store_true", dest="replace",
                    help=_("Quit and replace an already running "
                            "daemon"))
  parser.add_option("", "--sysroot", default=None,
                      action="store", type="string", dest="sysroot",
                      help=_("Import sysroot path in the given path"))
  parser.add_option("", "--os", default=None,
                      action="store", type="string", dest="os",
                      help=_("Import os name in the given string"))
  parser.add_option("-p", "--prohibit-shutdown",
                    default=False,
                    action="store_true", dest="prohibit_shutdown",
                    help=_("close auto shutdown"))
  parser.add_option("--no-check-network",
                    default=True,
                    action="store_false", dest="check_network",
                    help=_("Quit and close check network"))

  (options, args) = parser.parse_args()

  if os.getuid() != 0:
      print(_("You need to be root to run this application"))
      sys.exit(1)

  # ensure that we are not killed when the terminal goes away e.g. on
  # shutdown
  signal.signal(signal.SIGHUP, signal.SIG_IGN)
  signal.signal(signal.SIGINT,signal_handler_term)

  if options.debug:
    logging.basicConfig(format=FORMAT,level=logging.INFO,datefmt='%m-%d,%H:%M:%S')
  else:
    logging.basicConfig(format=FORMAT,level=logging.INFO,datefmt='%m-%d,%H:%M:%S',filename = logfile(),filemode = 'a')

  logging.info('kylin-system-updater starting ...')

  app = UpdateManager(options)

  app.run()