#!/usr/bin/python
#
# Copyright (C) 2010 Red Hat, Inc.
# Authors:
# Thomas Woerner <twoerner@redhat.com>
#
# 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, see <http://www.gnu.org/licenses/>.
#
# python fork magic derived from setroubleshoot
# Copyright (C) 2006,2007,2008,2009 Red Hat, Inc.
# Authors:
#   John Dennis <jdennis@redhat.com>
#   Dan Walsh <dwalsh@redhat.com>

import os, sys
DATADIR = '/usr/share/firewalld'
sys.path.append(DATADIR)

import gobject
import glib
import dbus
import dbus.service
import dbus.mainloop.glib
import slip.dbus
import slip.dbus.service
import pickle

import syslog, traceback, types
#import hashlib, random
import firewall
import firewall_config
from firewall_functions import active_firewalld
from firewall_error import *
from logger import log, FileLog

debug = 0
fork = True

if "--debug" in sys.argv[1:]:
    debug = 1
    fork = False
if "--verbose" in sys.argv[1:]:
    debug = 1

log_file = FileLog(firewall_config.VARLOGFILE, "a")
log.setDateFormat("%Y-%m-%d %H:%M:%S")
log.setFormat("%(date)s %(label)s%(message)s")
log.setInfoLogging("*", log.syslog, [ log.FATAL, log.ERROR ])
log.addInfoLogging("*", log.stderr, [ log.FATAL, log.ERROR ])
log.addInfoLogging("*", log_file, [ log.FATAL, log.ERROR ])
log.setInfoLogging("*", log_file, [ log.WARNING ])
log.setDebugLogLevel(log.NO_INFO)
log.setDebugLogLevel(log.NO_DEBUG)
log.setDebugLogging("*", log_file, [ i for i in xrange(1, log.DEBUG_MAX+1) ])

if debug:
    log.setInfoLogLevel(log.INFO2)
    log.setDebugLogLevel(log.DEBUG5)
    log.addInfoLogging("*", log_file)
    log.addDebugLogging("*", log_file)

if active_firewalld():
    log.fatal(_("Not starting FirewallD, already running."))
    sys.exit(1)

try:
    if fork:
        # do the UNIX double-fork magic, see Stevens' "Advanced 
        # Programming in the UNIX Environment" for details (ISBN 0201563177)
        pid = os.fork()
        if pid > 0:
            # exit first parent
            sys.exit(0)

        # decouple from parent environment
        os.chdir("/")
        os.setsid()
        os.umask(os.umask(0077) | 0022)
    
    # write the pid file
    pid_file = "/var/run/firewalld.pid"
    f = open(pid_file, "w")
    f.write(str(os.getpid()))
    f.close()

    # import here
    from server import run_server
    run_server()

except OSError, e: 
    log.fatal(_("Fork #1 failed: %d (%s)") % (e.errno, e.strerror))
    log.error(traceback.format_exc())
    sys.exit(1)

except dbus.DBusException, e:
    log.fatal(str(e))
    log.error(traceback.format_exc())
    sys.exit(1)

except IOError, e:
    log.fatal(str(e))
    log.error(traceback.format_exc())
    sys.exit(1)

sys.exit(0)
