#!/usr/bin/python3

# /*
#  * Copyright (C) 2020, KylinSoft Co., Ltd.
#  *
#  * 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 3 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, see <https://www.gnu.org/licenses/>.
#  */

from SystemUpdater.UpdateManager import UpdateManager
from SystemUpdater.modules.i18n import _
from SystemUpdater.modules.dump_so import dump_proc_maps
import logging
from optparse import OptionParser
import dbus
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
import signal
import os
import sys
from SystemUpdater.configs import G_settings
from SystemUpdater.Core.LogManager import get_logfile as logfile
from SystemUpdater.constants import KYLIN_OS_NAME

#定义日志的格式 
FORMAT = "%(asctime)s [%(levelname)s]: %(message)s"
FORMAT_DEBUG = '%(asctime)-15s %(levelname)s(%(filename)s:%(lineno)d):%(message)s'
DUMP_MAPS_FILE = "/var/log/kylin-system-updater/kylin-system-updater-maps.log"
SHOW_ENV_VARS = [
    "LD_LIBRARY_PATH",
    "PYTHONPATH",
    "GI_TYPELIB_PATH",
]

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=KYLIN_OS_NAME,
                      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("-b", "--prohibit-backup",
                    default=False,
                    action="store_true", dest="prohibit_backup",
                    help=_("close auto backup in the deployment process"))
  parser.add_option ("-n","--no-update-source", action="store_false",
                    dest="enable_push", default=True,
                    help=_("Do not check for updates source when updating"))
  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()

  # 将命令行参数 加载到settings的配置项中
  G_settings.update(vars(options))

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

  # set debconf to NON_INTERACTIVE
  os.environ["DEBIAN_FRONTEND"] = "noninteractive"

  #当不存在语言变量时 默认显示中文
  if "LANGUAGE" not in os.environ:
    os.environ["LANGUAGE"] = "zh_CN.UTF-8"
  
  #当不存在语言变量时 默认显示中文
  if "LANG" not in os.environ:
    os.environ["LANG"] = "zh_CN.UTF-8"

  #做一些规范处理
  if os.environ["LANGUAGE"] == "en":
    os.environ["LANGUAGE"] = "en_US.UTF-8"
  if os.environ["LANGUAGE"] == "zh_CN:en" or os.environ["LANGUAGE"] == "zh_CN:zh":
    os.environ["LANGUAGE"] = "zh_CN.UTF-8"

  if "RUN_ROOT_DIR" not in os.environ:
    os.environ["RUN_ROOT_DIR"] = "/"

  # ensure that we are not killed when the terminal goes away e.g. on
  # shutdown
  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(f'kylin-system-updater({os.getpid()}) LANGUAGE:{os.environ["LANGUAGE"]} LANG:{os.environ["LANG"]} starting ...')
  
  for var_name in SHOW_ENV_VARS:
      value = os.environ.get(var_name, "<not set>")
      logging.info("Env %s: %s", var_name, value)

  app = UpdateManager(options)

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

  signal.signal(signal.SIGHUP, signal_handler_term)
  signal.signal(signal.SIGINT,signal_handler_term)
  signal.signal(signal.SIGTERM,signal_handler_term)

  # 将调用的so库信息 dump 出来 便于排查问题
  dump_proc_maps(DUMP_MAPS_FILE)

  app.run()