#! /usr/bin/env python

# Anemon Dhcp
# Copyright (C) 2001-2002 Mathieu Ignacio -- mignacio@april.org
#
# 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.
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA

import sys
from pydhcplib.dhcp_network import *
from optparse import OptionParser
from anemon.server import sysconf
from anemon.server import event_logger


parser = OptionParser()
parser.add_option("-C", "--config", dest="config", help="Alternate configuration file",
                  action="store", type="string")

""" Dhcp Network Protocol configuration options """
parser.add_option("-w", "--listen-address", dest="listen_address",
                  help="Set listen address (0.0.0.0 for all addresses)",
                  action="store", type="int")
parser.add_option("-x", "--server-listen-port", dest="server_listen_port",
                  help="Set server listen port",
                  action="store", type="int")
parser.add_option("-y", "--client-listen-port", dest="client_listen_port",
                  help="Set client listen port",
                  action="store", type="int")
parser.add_option("-z", "--default-giaddr", dest="default_giaddr",
                  help="Set default network request (ex: 192.168.0.0/24)",
                  action="store", type="string")

""" Logging system options """
parser.add_option("-A", "--log-level", dest="log_level", help="Default log level (between 0 and 50)",
                  action="store", type="string")

parser.add_option("-B", "--log-destination", dest="log_destination", help="Log destination",
                  action="store", type="string")

""" Database options """
parser.add_option("-u", "--database-uri", dest="database_uri", help="Database uri (like mysql://dhcpd@localhost/dhcpd)",
                  action="store", type="string")



(options, args) = parser.parse_args()



""" Load configuration file"""

if (options.config!=None) : sysconf.LoadConf(options.config)
else : sysconf.LoadConf("/etc/anemon/server.conf")




"""Replace server.conf options with command line options """
if (options.database_uri!=None ): sysconf.database_uri = options.database_uri
if (options.log_level!=None ): sysconf.log_level = options.log_level
if (options.log_destination!=None ): sysconf.log_destination = options.log_destination
if (options.server_listen_port!=None ): sysconf.server_listen_port = options.server_listen_port
if (options.client_listen_port!=None ): sysconf.client_listen_port = options.client_listen_port
if (options.listen_address!=None ): sysconf.listen_address = options.listen_address
if (options.default_giaddr!=None ): sysconf.default_giaddr = options.default_giaddr


""" Init logger 'Log' variable before import any other anemon modules """
event_logger.init(sysconf.log_type,sysconf.log_level,sysconf.log_options)


""" Start server """
try : import anemon.server.main
except : event_logger.exception("Main exception. Anemon server stopped.")
    


