#!/usr/bin/python
# -*- coding: utf-8; -*-
#
# (c) 2014 Mandriva, http://www.mandriva.com/
#
# This file is part of Pulse 2
#
# Pulse 2 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.
#
# Pulse 2 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 Pulse 2; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.

"""
A generic util to create several packages including all needed dependencies
and parameter config files.

Based on templates and on a config file, for each defined platform generates
the agents packs including needed postinstall scripts.

Options:
 --host=HOST   : Inventory server name or IP
 --port=PORT   : Inventory server port
 --enablessl   : Enabling the SSL protocol for inventory
 --tag=TAG     : Tag for inventory
 --cm-host=HOST: Connection manager host
 --cm-port     : Connection manager port
 --cm-only     : Generates only SmartAgent pack
 --help        : Displays this help
"""

import os
import sys
import uuid
import datetime
import plistlib
import fileinput

from distutils import dir_util, file_util, archive_util
from subprocess import Popen, PIPE
from ConfigParser import RawConfigParser


class ParameterError(Exception):
    def __init__(self, arg):
        Exception.__init__(self)
        self._arg = arg

    def __repr__(self):
        return "Invalid parameter '%s'" % self._arg


MODULE_NAME = os.path.splitext(os.path.basename(__file__))[0]
MODULE_DIR = os.path.dirname(os.path.realpath(__file__))
CONFIG_FILE = os.path.join(MODULE_DIR, "agents.conf")
INVENTORY_URL_FILE = "inventory.url"

verbose_mode = False

class Config(object):
    """
    Config parser reader.

    Excluding section [main] including common parameters,
    remaining sections represents all the platforms.
    Name of sections defines the name of platform folder
    and includes related options.

    Example:
    agents.conf
    [main]
    attr1 = value1
    ..
    ..
    [win32]
    version = 2.3.5
    ..
    [deb]
    package_name = pulse2-agents-installer
    ..
    ..
    ..

    # element in [main] section will be referenced as a direct attribute:

    >>> self.attr1

    # other elements:

    >>> self.deb.package_name
    >>> self.win32.version

    """

    def __init__(self, path=CONFIG_FILE):
        self.path = path
        self.cp = RawConfigParser()
        self.cp.read(path)
        self.parse()

    def parse(self):
        """Parsing of all the sections and attaching its to this instance """

        for section in self.cp.sections():
            if section == "main":
                for option in self.cp.options(section):
                    value = self.cp.get(section, option)
                    setattr(self, option, value)
            else:
                attrs = {}
                for option in self.cp.options(section):
                    value = self.cp.get(section, option)
                    attrs[option] = value

                section_type = type("Section", (object,), attrs)
                setattr(self, section, section_type)


    def generate_vnc_password(self, section):
        password = str(uuid.uuid4()).replace("-","")[:16]
        self.cp.set(section, "vnc_password", password)
        self._save()
        return password


    def increment_release(self):
        """ Increments the release number (can be called on each build)."""

        try:
            actual = self.cp.getint("main", "release")
        except TypeError:
            print "Release number must be integer type"
            sys.exit(1)

        self.cp.set("main", "release", actual + 1)
        self._save()

    def _save(self):
        self.cp.write(open(self.path, "wb"))





class InventoryURLGen(object):
    """Detects the inventory URL."""

    inv_ini_path = os.path.join("/",
                               "etc",
                               "mmc",
                               "pulse2",
                               "inventory-server",
                               "inventory-server.ini")
    pkg_ini_path = os.path.join("/",
                               "etc",
                               "mmc",
                               "pulse2",
                               "package-server",
                               "package-server.ini")

    host = None
    port = None

    enablessl = False
    tag = None

    def __init__(self, host=None, port=None, enablessl=False, tag=None):

        if host is None:
            self.pkg_check()
        else:
            self.host = host

        if port is None:
            self.inv_check()
        else:
            self.port = int(port)

        if enablessl in ["True", "1", "true"]:
            self.enablessl = True

        self.tag = tag




    def inv_check(self):

        try:
            from pulse2.inventoryserver.config import Pulse2OcsserverConfigParser
        except ImportError:
            self.port = 9999
        else:
            config = Pulse2OcsserverConfigParser()
            config.setup(self.inv_ini_path)
            self.port = config.port
            self.enablessl = config.enablessl

    def pkg_check(self):

        try:
            from pulse2.package_server.config import P2PServerCP
        except ImportError:
            self.host = self.probe_fqdn()
        else:
            config = P2PServerCP()
            config.setup(self.pkg_ini_path)
            host = config.public_ip
            if host == "":
                self.host = self.probe_fqdn()
            else:
                self.host = config.public_ip


    def probe_fqdn(self):
        p = Popen(["hostname", "-f"], stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if err:
            print "ERROR: %s" % err
        fqdn = out.split("\n")
        return fqdn[0]



    def get_url(self):
        if self.enablessl:
            proto = "https"
        else:
            proto = "http"

        return "server = %s://%s:%s" % (proto, self.host, self.port)

    def get_tag(self):
        if self.tag:
            return "tag = %s" % self.tag



class AbsCreator(object):
    """
    A main frame for the package creators.

    Defines some common methods to create the packages, including
    the control file generators and a final packaging.
    """
    # configuration reference
    config = None

    # export directory and config section reference
    platform_directory = None

    # name of package
    package_name = None

    # relative path for copying some files with a config content
    resources_path_list = []

    # list of required packages
    dependencies = []

    def __init__(self, config, inventory_url, tag):
        """
        @param config: config instance
        @type config: Config

        @param inventory_url: detected URL of inventory service
        @type inventory_url: str

        @param tag: inventory tag
        @type tag: str
        """
        self.config = config
        self.inventory_url = inventory_url
        self.tag = tag

        # get the platform : config.<platform>
        platform_section = getattr(config, self.platform_directory)

        # config.<platform>.version
        if hasattr(platform_section, "version"):
            self.version = getattr(platform_section, "version")

        self.release = self.config.release


        print "Creating [%s] agent pack" % self.platform_directory


    @property
    def dir_current(self):
        """Current directory"""
        return os.path.dirname(os.path.abspath(__file__))

    @property
    def dir_template(self):
        """Directory with template related to platform"""
        return os.path.join(self.dir_current,
                            self.config.templates_dir,
                            self.platform_directory,
                            self.config.template_pkg_name,
                            )

    @property
    def dir_destination(self):
        """Directory used to export of package"""
        return os.path.join(self.dir_current,
                            self.platform_directory,
                            self.package_dir_name,
                            )

    @property
    def dir_resources(self):
        """Files with the config content will be placed here"""
        return os.path.join(self.dir_destination, *self.resources_path_list)


    def create_dummy_package(self):
        """Creates a empty package in the export directory """

        dir_util.mkpath(self.dir_destination)

        dir_util.copy_tree(self.dir_template,
                           self.dir_destination,
                           )

    def control_file_generate(self):
        """
        Creates the control file of package related to platform.

        Will be overriden.
        """
        pass

    def copy_ssh_key(self):
        """SSH public key copying into the resource folder"""
        file_util.copy_file(self.config.ssh_key_pub,
                            self.dir_resources,
                            )

    def create_inventory_url_file(self):
        """
        Creating the inventory config file which will be used
        to configure fusioninventory agent
        """
        path = os.path.join(self.dir_resources,
                            INVENTORY_URL_FILE)

        if self.tag:
            content = "\n".join([self.inventory_url, self.tag])
        else:
            content = self.inventory_url

        with open(path, "w") as f:
            f.write(content)

    def build(self):
        """Build the package (to be override)."""
        pass

    def remove(self):
        """Removes the temporary package tree """
        dir_util.remove_tree(self.dir_destination)


    def generate(self):
        """Package generating, step-by-step with a given workflow"""
        if os.path.exists(self.dir_destination):
            self.remove()
        self.create_dummy_package()
        self.control_file_generate()

        self.copy_ssh_key()
        self.create_inventory_url_file()
        self.build()
        self.remove()


class MacCreator(AbsCreator):
    """ Creates a package for OS X"""

    platform_directory = "mac"
    package_name = "Pulse2AgentsInstaller"
    package_dir_name = "Pulse2AgentsInstaller.pkg"
    dependencies = ["fusioninventory-agent"]
    resources_path_list = ["Contents",
                           "Resources",
                           ]
    postflight_file = "%s/%s/Contents/Resources/postflight" % (platform_directory,
                                                               package_dir_name)


    def control_file_generate(self):
        """
        OSX package (.pkg) Info.plist.

        Creates Info.plist file which contents all necessary package info.
        """
        path = os.path.join(self.dir_destination,
                            "Contents",
                            )

        version_splitted = self.config.mac.version.split(".")
        version_maj, version_min = version_splitted[0:2]

        product_info = {
            "CFBundleDevelopmentRegion" : "English",
            "CFBundleGetInfoString": "Pulse2 Agents Installer for OS X %s" % self.config.mac.version,
            "CFBundleIdentifier": self.config.mac.package_id ,
            "CFBundleName": self.package_name,
            "CFBundleIconFile": "header.png",
            "IFMajorVersion": version_maj,
            "IFMinorVersion": version_min,
            "IFPkgBuildDate" : datetime.datetime.now(),
            "IFPkgBuildVersion": "10H574",
            "IFPkgCreator": "",
            "FPkgFlagAllowBackRev": False,
            "IFPkgFlagAuthorizationAction": "AdminAuthorization",
            "IFPkgFlagBackgroundAlignment": "bottomright",
            "IFPkgFlagBackgroundScaling": "tofit",
            "IFPkgFlagDefaultLocation": "/",
            "IFPkgFlagFollowLinks" : False,
        #    "IFPkgFlagInstalledSize": 50468,
            "IFPkgFlagIsRequired": False,
            "IFPkgFlagOverwritePermissions": False,
            "IFPkgFlagRelocatable": False,
            "IFPkgFlagRestartAction": "NoRestart",
            "IFPkgFlagRootVolumeOnly": True,
            "IFPkgFlagUpdateInstalledLanguages": False,
            "IFPkgFormatVersion": 0.10000000149011612
            }

        filename = os.path.join(path, "Info.plist")
        f = open(filename, "w")
        f.close()

        plistlib.writePlist(product_info, filename)

    def build(self):

#        cmd = ["genisoimage",
#                "-V",
#                self.config.mac.package_name,
#                "-D",
#                "-R",
#                "-apple",
#                "-no-pad",
#                "-o",
#                "%s.dmg" % self.config.mac.package_name,
#                self.platform_directory,
#                ]
#        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
#        out, err = p.communicate()
#        if err:
#            print "ERROR: %s" % err
#        print "os x dmg create: %s" % out
        self._declare_vnc_password()

        postflight = os.path.join(self.dir_current, self.postflight_file)
        os.chmod(postflight, 0755)

        origin_dir = self.dir_current
        destination = os.path.join(self.dir_current,
                                   self.platform_directory,
                                   )
        os.chdir(destination)

        archive_util.make_archive(self.package_name,
                                  "tar",
                                  None,
                                  None,
                                  verbose=1
                                  )
        os.chdir(origin_dir)
        print "[mac] package %s built successfully" % self.package_name


    def _declare_vnc_password(self):
        password = self.config.generate_vnc_password(self.platform_directory)
        postflight = os.path.join(self.dir_current, self.postflight_file)
        with open(postflight, "wt") as fout:
            with open("%s.in" % postflight, "rt") as fin:
                for line in fin:
                    if "@vnc_password@" in line:
                        fout.write(line.replace("@vnc_password@", password))
                    else:
                        fout.write(line)






class DebCreator(AbsCreator):
    """ Creates a package for debian based platforms"""

    platform_directory = "deb"
    package_name = "pulse2-agents-installer"
    package_dir_name = package_name
    dependencies = ["fusioninventory-agent",
                     "openssh-server",
                     "x11vnc"]

    resources_path_list = ["opt",]


    def control_file_generate(self):
        content = """Section: misc
Priority: optional
Homepage: %s
Package: %s
Version: %s-%s
Architecture: all
Installed-Size: 0
Maintainer: %s
Depends: %s
Description: A metapackage to install and configure Pulse2 agents
""" % (
               self.config.homepage,
               self.package_name,
               self.version,
               self.release,
               self.config.maintainer,
               ", ".join(self.dependencies),
               )

        path = os.path.join(self.dir_destination,
                            "DEBIAN",
                            "control")

        with open(path, "w") as f:
            f.write(content)

    def build(self):
        origin_dir = self.dir_current
        destination = os.path.join(self.dir_current,
                                   self.platform_directory,
                                   )
        os.chdir(destination)

        cmd = ["dpkg", "--build", self.package_name]
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if err:
            print "ERROR: %s" % err
        if verbose_mode:
            print "[deb]: %s" % out

        print "[deb] package %s built successfully" % self.package_name

        os.chdir(origin_dir)

class RpmCreator(AbsCreator):

    platform_directory = "rpm"
    package_name = "pulse2-agents-installer"
    package_dir_name = package_name
    dependencies = ["fusioninventory-agent",
                     "openssh-server",
                     "tigervnc-server"]

    resources_path_list = ["SOURCES"]

    def _head_line(self, name, value):
        return "%define " + "%s %s" % (name, value)


    def control_file_generate(self):

        var_header = []
        var_header.append(self._head_line("name", self.package_name))
        var_header.append(self._head_line("version", self.version))
        var_header.append(self._head_line("release", self.release))
        var_header.append(self._head_line("requires", ", ".join(self.dependencies)))
        var_header.append(self._head_line("opt_dir", "opt"))

        specs = """
%define _topdir %(echo $PWD)/
Summary: A meta-package to install and configure Pulse2 agents
Name: %{name}
Version: %{version}
Release: %{release}
Group: System/Configuration
License: GPLv3
Source0: %{name}-%{version}.tar
BuildArch: noarch
BuildRoot: %{_tmppath}/%{name}-%{version}-root
Requires: %{requires}

%description
 A meta-package to install and configure Pulse2 agents

%prep
%setup -q

%build

%install
rm -rf $RPM_BUILD_ROOT
install -d $RPM_BUILD_ROOT/%{opt_dir}/%{name}
install postinst $RPM_BUILD_ROOT/%{opt_dir}/%{name}/postinst
install id_rsa.pub $RPM_BUILD_ROOT/%{opt_dir}/%{name}/id_rsa.pub
install inventory.url $RPM_BUILD_ROOT/%{opt_dir}/%{name}/inventory.url

%clean
rm -rf $RPM_BUILD_ROOT

%files
%dir /%{opt_dir}/%{name}/postinst

%defattr(-,root,root,-)
/%{opt_dir}/%{name}/postinst
/%{opt_dir}/%{name}/id_rsa.pub
/%{opt_dir}/%{name}/inventory.url

%post
chmod +x /%{opt_dir}/%{name}/postinst
/%{opt_dir}/%{name}/postinst
rm -rf /%{opt_dir}/%{name}/
        """
        content = "\n".join(var_header) + "\n" + specs


        path = os.path.join(self.dir_destination,
                            "SPECS",
                            "%s.spec" % self.package_name)

        with open(path, "w") as f:
            f.write(content)


    def _create_source(self):
        """
        Creates a directory in the SOURCES/ folder and moves all
        sources files into. This directory will be packaged as tarball.
        """
        origin_dir = self.dir_current
        destination = os.path.join(self.dir_destination, *self.resources_path_list)
        # cd rpm/package_name/SOURCES

        os.chdir(destination)
        r_files = os.listdir(destination)

        #name_version = "%s-%s" % (self.config.rpm.package_name, self.version)
        name_version = "%s-%s" % (self.package_name, self.version)

        # mkdir rpm/package_name/SORCES/package_name-0.1
        dir_util.mkpath(name_version)

        # mv * name_version/
        for r_file in r_files:
            file_util.move_file(r_file, name_version)

        # tar package_name-0.1.tar package_name-0.1
        archive_util.make_archive(name_version,
                                  "tar",
                                  None,
                                  None,
                                  verbose=1
                                  )
        os.chdir(origin_dir)

    def build(self):
        """ Builds a RPM package."""

        # moves source files into an archive
        self._create_source()

        origin_dir = self.dir_current

        # fo to the package directory
        os.chdir(self.dir_destination)

        cmd = ["rpmbuild",
               "-ba",
               os.path.join("SPECS",
                            "%s.spec" % self.package_name,
                            )
               ]
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()
        if err:
            print "ERROR: %s" % err
        if verbose_mode:
            print "rpm: %s" % out

        os.chdir(origin_dir)

        rpm_name = "%s-%s-%s.noarch.rpm" % (self.package_name,
                                            self.version,
                                            self.release,
                                            )

        rpms_dir = os.path.join(self.platform_directory,
                                self.package_name,
                                "RPMS",
                                "noarch",
                                rpm_name,
                                )

        file_util.move_file(rpms_dir, self.platform_directory)

        print "[rpm] package %s built successfully" % self.package_name


class DebServerCreator(DebCreator):
    platform_directory = "deb"
    package_name = "pulse2-agents-installer-nordp"
    package_dir_name = "pulse2-agents-installer-nordp"
    dependencies = ["fusioninventory-agent",
                    "openssh-server",
                    "linuxvnc",
                    ]

class RpmServerCreator(RpmCreator):

    platform_directory = "rpm"
    package_name = "pulse2-agents-installer-nordp"
    package_dir_name = "pulse2-agents-installer-nordp"
    dependencies = ["fusioninventory-agent",
                    "openssh-server",
                    ]
class SuseCreator(RpmCreator):

    platform_directory = "rpm"
    package_name = "pulse2-agents-installer-suse"
    package_dir_name = "pulse2-agents-installer-suse"
    dependencies = ["fusioninventory-agent",
                    "openssh",
                    "tigervnc-server"]

class SuseServerCreator(RpmCreator):

    platform_directory = "rpm"
    package_name = "pulse2-agents-installer-suse-nordp"
    package_dir_name = "pulse2-agents-installer-suse-nordp"
    dependencies = ["fusioninventory-agent",
                    "openssh",
                    ]



class Win32Creator(AbsCreator):
    """
    Creates a agent pack for Windows.

    Because the packaging on this platform is completelly different,
    we call only a building bash script.
    """

    platform_directory = "win32"


    def generate(self):
        path = os.path.join(self.dir_current,
                            self.platform_directory,
                            self.config.win32.generate_script
                            )
        cmd = [path]
        if self.tag :
            cmd.append("--tag=%s" % self.tag)
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()

        if verbose_mode:
            print out


        print "[win32] package built successfully"



class PackagesGenerator(object):
    """The Package creators will be called here. """
    def __init__(self, inventory_url, tag):
        """
        @param inventory_url: URL of inventory server with port number
        @type inventory_url: str

        @param tag: inventory tag
        @type tag: str
        """

        self.config = Config(CONFIG_FILE)
        self.inventory_url = inventory_url
        self.tag = tag

    def generate_all(self):
        """All the creators must be called here."""
        for creator in [MacCreator,
                        DebCreator,
                        RpmCreator,
                        DebServerCreator,
                        RpmServerCreator,
                        SuseCreator,
                        SuseServerCreator,
                        Win32Creator,
                        ]:

            m = creator(self.config,
                        self.inventory_url,
                        self.tag
                        )
            m.generate()

        self.config.increment_release()


class SmartAgentPrepare(object):

    AGENT_FOLDER = "agent"
    INSTALLER = "installer.sh"
    DEFAULTS_FILE = "pulse2agent.defaults"
    VPN_INSTALLER_FOLDER = "vpn"
    VPN_VARIABLES_FILE = "vpn-variables"

    vpn_variables_pattern = {"VPN_SERVER_PUBLIC_IP": "",
                             "VPN_TAP_ADDRESS": "",
                            }

    def __init__(self, host, port):

        self.host = host
        self.port = port


    def set_all(self):
        for method in [self.vpn_defaults_get,
                       self.installer_set,
                       self.defaults_set,
                       self.create_archive,
                       ]:
            succeed = method()
            if not succeed:
                print "ERROR: An error occurred during execute %s" % method.__name__
                return 1
        return 0



    def installer_set(self):

        installer = os.path.join(MODULE_DIR, self.INSTALLER)
        file_util.copy_file("%s.in" % installer, installer)
        print "INFO: Installer create..."
        return self.replace({"DOMAIN": self.host}, installer)


    def defaults_set(self):
        defaults = os.path.join(MODULE_DIR,
                                self.AGENT_FOLDER,
                                self.DEFAULTS_FILE)
        pattern = {"PULSE2_CM_SERVER": self.vpn_variables_pattern["VPN_TAP_ADDRESS"],
                   "PULSE2_CM_PORT": self.port,
                   "VPN_SERVER_PUBLIC_IP" : self.vpn_variables_pattern["VPN_SERVER_PUBLIC_IP"],
                   "VPNCMD_PATH": "/opt/vpnclient/vpncmd",
                   "PULSE2_CM_LOG_PATH": "/var/log/pulse2agent.log",
                   }

        file_util.copy_file("%s.in" % defaults, defaults)
        print "INFO: Defaults prepare"
        return self.replace(pattern, defaults)


    def vpn_defaults_get(self):
        var_file = os.path.join(MODULE_DIR,
                                self.VPN_INSTALLER_FOLDER,
                                self.VPN_VARIABLES_FILE,
                                )
        if os.path.exists(var_file):
            with open(var_file) as f:
                for line in f.readlines():
                    for variable in self.vpn_variables_pattern:
                        if variable in line and "=" in line:
                            _, value = line.split("=")
                            value = value.strip().replace('"','')
                            self.vpn_variables_pattern[variable] = value
            return True
        else:
            print "WARNING: %s not found!" % var_file
            return False


    def replace(self, pattern, in_script):
        """
        Replaces all occurences based on pattern.

        @param pattern: key as template expression, value as new string
        @type pattern: dict

        @return: True if all occurences replaced
        @rtype: bool
        """
        replaced_items = 0
        for line in fileinput.input(in_script, inplace=1):
            for (old, new) in pattern.iteritems():
                search_exp = "@@%s@@" % old
                if search_exp in line:
                    line = line.replace(search_exp, new)
                    replaced_items += 1
            sys.stdout.write(line)

        return replaced_items == len(pattern)


    def create_archive(self):
        print "INFO: Agent archive create..."
        name = os.path.join(MODULE_DIR, self.AGENT_FOLDER)
        #archive_util.make_archive(self.AGENT_FOLDER,
        archive_util.make_archive(name,
                                  "gztar",
                                  MODULE_DIR,
                                  self.AGENT_FOLDER,
                                  verbose=1
                                  )
        return True

class BuildSoftEtherClient(object):
    current_directory = None
    path = ["vpn", "softether"]
    command = "build.sh"

    def __init__(self):
        print "INFO: Prepare SoftEther VPN Client installer for Windows ..."
        current_directory = os.path.dirname(os.path.abspath(__file__))
        self.build()
        os.chdir(current_directory)


    def build(self):
        destination = os.path.join(*self.path)
        # cd vpn/softether/
        os.chdir(destination)

        cmd = ["sh", "build.sh"]
        p = Popen(cmd, stdout=PIPE, stderr=PIPE)
        out, err = p.communicate()

        print "INFO: (build of SoftEther VPN Client) %s" % out
        if len(err) > 0:
            print "ERROR: (build of SoftEther VPN Client) %s" % err







def args_parse(script_path, args):
    """
    Arguments parsing.

    @param script_path: pathname defined by __file__
    @type script_path: str

    @param args: list of command line arguments
    @type args: list

    @return: keywords for InventoryURLGen
    @rtype: dict
    """

    module = os.path.splitext(os.path.basename(script_path))[0]

    allowed_args = [module,
                    "--host",
                    "--port",
                    "--enablessl",
                    "--tag",
                    "--cm-host",
                    "--cm-port",
                    "--cm-only",
                    "-h",
                    "--help",
                    "--verbose"
                    ]

    kwargs = {}

    if len(args) > 1:
        for arg in args:
            if not any([a in arg for a in allowed_args]):
                print __doc__
                sys.exit(0)
            if arg == "--verbose" :
                verbose_mode = True
                continue
            if arg in ["-h", "--help"]:
                print __doc__
                sys.exit(0)
            if "=" in arg:
                key, value = arg.split("=")
                key = key.replace("--", "")
                kwargs[key] = value

    return kwargs


if __name__ == "__main__":

    kwargs = args_parse(__file__, sys.argv)
    inv = InventoryURLGen(**kwargs)
    inventory_url = inv.get_url()
    print "INFO: Inventory URL: %s" % inventory_url
    tag = inv.get_tag()

    if not "--cm-only" in sys.argv:
        pgen = PackagesGenerator(inventory_url, tag)
        pgen.generate_all()

    #if "cm-host" in kwargs:
    #    cm_host = kwargs["cm-host"]
    #else:
    #    cm_host = inv.host

    #if "cm-port" in kwargs:
    #    cm_port = kwargs["cm-port"]
    #else:
    #    cm_port = "8443" # TODO - check it in cm.ini

    #BuildSoftEtherClient()
    #sm = SmartAgentPrepare(cm_host, cm_port)
    #sys.exit(sm.set_all())
