#! /usr/bin/env python
# -*- coding: ISO-8859-1 -*-
#
# UnderCoverService.py
# (C)2003 Romain GUY <romain.guy@jext.org>
# Licensed under LGPL
#
# 08/27/2003 - 06:20:52

"""
Win32 service for UnderCover.
"""

##########################################################################################
# MODULE INFORMATIONS
##########################################################################################

__author__    = "Romain GUY (romain.guy@jext.org)"
__version__   = "1.0"
__copyright__ = "Copyright (C)2003 Romain GUY"
__license__   = "LGPL"

##########################################################################################
# IMPORTS
##########################################################################################

import UnderCover
import UnderCoverCron

import threading
import win32serviceutil, win32service
import pywintypes, win32con, winerror

from ntsecuritycon import *

import sys, os

##########################################################################################
# PROGRAM
##########################################################################################

try:
  this_filename = __file__
except NameError:
  sys.argv[0] = os.path.abspath(sys.argv[0])
  this_filename = sys.argv[0]

sb_dir = os.path.dirname(this_filename)

sys.path.insert(0, sb_dir)
os.chdir(sb_dir)

try:
  import win32api
  try:
    win32api.GetConsoleTitle()
  except win32api.error:
    import win32traceutil
    print "UnderCover service module loading (as user %s)..." % win32api.GetUserName()
except ImportError:
  pass

##########################################################################################
# CLASSES
##########################################################################################

class Service(win32serviceutil.ServiceFramework):
  _svc_name_         = "UnderCover"
  _svc_display_name_ = "UnderCover Backup Service"

  def __init__(self, args):
    win32serviceutil.ServiceFramework.__init__(self, args)
    self.event_stopped  = threading.Event()
    self.event_stopping = threading.Event()
    self.thread         = None
    self.wrapper        = UnderCover.UnderCover()

  def SvcStop(self):
    self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
    self.event_stopping.set()
    self.cron.stop()

  def SvcDoRun(self):
    thread = threading.Thread(target = self.ServerThread)
    thread.start()

    import servicemanager
    servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STARTED, (self._svc_name_, ''))

    try:
      self.event_stopping.wait()
      for i in range(20):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        self.event_stopped.wait(1)
        if self.event_stopped.isSet():
          break
    except KeyboardInterrupt:
      pass
    
    servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STOPPED, (self._svc_name_, ''))

  def ServerThread(self):
    try:
      try:
        if self.wrapper.start():
          self.cron = UnderCoverCron.UnderCoverCron(self.wrapper)
          self.cron.doCron()
      except:
        message = "UnderCover service failed with an unexpected error\r\n\r\n" + ob.getvalue()
        print message
        import servicemanager
        servicemanager.LogErrorMsg(message)
    finally:
      self.event_stopping.set()
      self.event_stopped.set()

if __name__=='__main__':
  win32serviceutil.HandleCommandLine(Service)