#! /usr/bin/env python
# -*- coding: ISO-8859-1 -*-
#
# undercover.py
# (C)2003 Romain GUY <romain.guy@jext.org>
# Licensed under LGPL
#
# 08/26/2003 - 21:09:48

"""
undercover is a management script for UnderCoverServer.
"""

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

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

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

import os.path
import sys
import time
import UnderCoverServer
import xmlrpclib

from xml.dom.minidom import parse

##########################################################################################
# GLOBALS
##########################################################################################

try:
  True, False
except:
  True, False = 1, 0

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

class UnderCover(object):
  def __init__(self):
    self._deleteRemote = False
    self._serverPort   = 4620
    self._logFileTTL   = 15
    self._chunkSize    = 32768

    self.loadConfiguration()

    self._calls = { "start" : self.start, "stop": self.stop, "restart": self.restart }

  def call(self, name):
    if name in self._calls: self._calls[name]()

  def backup(self, remotePath, localPaths):
    msg = "Backup processing"
    try:
      self._server = xmlrpclib.ServerProxy("http://localhost:" + str(self._serverPort))
      self._server.backup(remotePath, localPaths)
      print msg, "[OK]"
      return True
    except:
      print msg, "[FAIL]"
      return False

  def start(self):
    msg = "Starting UnderCover server"
    try:
      UnderCoverServer.UnderCoverServer(self._deleteRemote, self._serverPort, \
                                        self._logFileTTL, self._chunkSize)
      print msg, "[OK]"
      return True
    except:
      print msg, "[FAIL]"
      return False

  def stop(self):
    msg = "Stopping UnderCover server"
    try:
      self._server = xmlrpclib.ServerProxy("http://localhost:" + str(self._serverPort))
      self._server.stop()
      print msg, "[OK]"
      return True
    except:
      print msg, "[FAIL]"
      return False

  def restart(self):
    self._calls["stop"]()
    time.sleep(1.0)
    self._calls["start"]()

  def loadConfiguration(self):
    if os.path.exists("undercoverserver.conf"):
      try:
        dom  = parse("undercoverserver.conf")
        root = dom.documentElement
  
        self._deleteRemote = root.getElementsByTagName("deleteRemote")[0].firstChild.nodeValue == "true" and True or False
        self._serverPort   = int(root.getElementsByTagName("serverPort")[0].firstChild.nodeValue)
        self._logFileTTL   = int(root.getElementsByTagName("logFileTTL")[0].firstChild.nodeValue)
        self._chunkSize    = int(root.getElementsByTagName("chunkSize")[0].firstChild.nodeValue)
  
        dom.unlink()
      except:
        print "Corrupted configuration file, using default values."

if __name__ == '__main__':
  if len(sys.argv) > 1 and sys.argv[1] in ("start", "stop", "restart"):
    wrapper = UnderCover()
    wrapper.call(sys.argv[1])
  else:
    print "undercover management script"
    print "(C)2003 Romain GUY <romain.guy@jext.org>"
    print
    print "undercover.py start|stop|restart"

# end of file