#!/usr/bin/python
# -*- coding: utf-8; -*-
#
# (c) 2004-2007 Linbox / Free&ALter Soft, http://linbox.com
# (c) 2007-2009 Mandriva, http://www.mandriva.com
#
# $Id: mmc-agent 4851 2009-12-09 16:11:39Z cdelfosse $
#
# This file is part of Mandriva Management Console (MMC).
#
# MMC 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.
#
# MMC 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 MMC.  If not, see <http://www.gnu.org/licenses/>.

import os
import os.path
import sys
import getopt
from mmc.support.config import MMCConfigParser

import mmc.agent

if __name__ == "__main__":
    inifile = "/etc/mmc/agent/config.ini"
    try:
        opts, suivarg = getopt.getopt(sys.argv[1:], "f:d")
    except getopt.GetoptError:
        sys.exit(2)
    daemonize = True
    for option, argument in opts:
        if option == "-f":
            inifile = argument
        elif option == "-d":
            daemonize = False

    if not os.path.exists(inifile):
        print "File '%s' does not exist." % inifile
	sys.exit(3)
 
    # Load configuration file
    try:
        cp = MMCConfigParser()
        cp.read(inifile)
    except Exception, e:
        print str(e)
        sys.exit(3)
    
    # Start the daemon main loop
    sys.exit(mmc.agent.agentService(cp, inifile, daemonize))
