#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# This file is part of Karesansui.
#
# Copyright (C) 2009 HDE, Inc.
#
# 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.
#

import os
import sys
import logging
from optparse import OptionParser

from ksscommand import KssCommand, KssCommandException, KssCommandOptException

import __cmd__

try:
    import karesansui
    from karesansui import __version__
    from karesansui.lib.firewall.iptables import KaresansuiIpTables
    from karesansui.lib.utils import load_locale
except ImportError:
    print >>sys.stderr, "[Error] karesansui package was not found."
    sys.exit(1)

_ = load_locale()

usage = '%prog [options]'

def getopts():
    optp = OptionParser(usage=usage, version=__version__)
    optp.add_option('-a', '--action', dest='action', help=_('Action'), default=None)
    return optp.parse_args()

def chkopts(opts):
    pass

class RestoreFirewall(KssCommand):

    def process(self):
        (opts, args) = getopts()
        chkopts(opts)
        self.up_progress(10)

        kit = KaresansuiIpTables()
        try:
            kit.firewall_xml__to__iptables_config()
            self.up_progress(10)
        except:
            self.logger.error("Cannot write '%s'." % (kit.iptables_conf_file))
            raise

        self.up_progress(10)
        if opts.action is not None:
            try:
                exec("func = kit.%s" % opts.action)
                self.up_progress(10)
                ret = func()
                if ret != 0:
                    return False
                self.up_progress(30)
            except:
                self.logger.error("Unknown action '%s'." % (opts.action))
                raise

        return True

if __name__ == "__main__":
    target = RestoreFirewall()
    sys.exit(target.run())
