from distutils.command.config \
    import \
        config as old_config

from bento.core.platforms \
    import \
        get_scheme
from bento.distutils.utils \
    import \
        run_cmd_in_context

class config(old_config):
    def __init__(self, *a, **kw):
        old_config.__init__(self, *a, **kw)

    def initialize_options(self):
        old_config.initialize_options(self)

    def finalize_options(self):
        old_config.finalize_options(self)

    def _get_install_scheme(self):
        pkg = self.distribution.pkg
        install = self.get_finalized_command("install")

        ## FIXME: this should be centralized with other scheme-related stuff
        #scheme = get_scheme(sys.platform)[0]
        #scheme["pkgname"] = pkg.name
        #py_version = sys.version.split()[0]
        #scheme["py_version_short"] = py_version[:3]
        scheme = {}
        if hasattr(install, "install_dir"):
            scheme["sitedir"] = install.install_dir
        scheme["prefix"] = install.install_base

        return scheme

    def run(self):
        from bento.commands.configure import ConfigureCommand
        from bento.commands.context import ConfigureYakuContext

        dist = self.distribution

        scheme = self._get_install_scheme()
        argv = []
        for k, v in scheme.iteritems():
            argv.append("--%s=%s" % (k, v))

        run_cmd_in_context(ConfigureCommand, "configure", argv, ConfigureYakuContext,
                           dist.run_node, dist.top_node, dist.pkg)
        #from bento._config import BENTO_SCRIPT
        #from bento.commands.configure import ConfigureCommand
        #from bento.commands.options import OptionsContext
        #from bento.compat.api import relpath

        #from bento.commands.configure import _setup_options_parser

        #package_options = PackageOptions.from_file(BENTO_SCRIPT)

        #dist = self.distribution
        #top_node = dist.top_node

        #cmd = ConfigureCommand()
        #options_ctx = OptionsContext.from_command(cmd)
        #_setup_options_parser(options_ctx, package_options)

        #ctx = ConfigureYakuContext(argv, options_ctx, dist.pkg, dist.run_node)
        #ctx.package_options = package_options

        #cmd_funcs = [(cmd.run, dist.top_node.abspath())]

        #try:
        #    while cmd_funcs:
        #        cmd_func, local_dir = cmd_funcs.pop(0)
        #        local_node = top_node.find_dir(relpath(local_dir, top_node.abspath()))
        #        ctx.pre_recurse(local_node)
        #        try:
        #            cmd_func(ctx)
        #        finally:
        #            ctx.post_recurse()

        #    cmd.shutdown(ctx)
        #finally:
        #    ctx.shutdown()

@@ -589,267 +589,8 @@
 %22)%0A%0A
-        ## FIXME: this should be centralized with other scheme-related stuff%0A        #scheme = get_scheme(sys.platform)%5B0%5D%0A        #scheme%5B%22pkgname%22%5D = pkg.name%0A        #py_version = sys.version.split()%5B0%5D%0A        #scheme%5B%22py_version_short%22%5D = py_version%5B:3%5D%0A
     
@@ -1257,1199 +1257,4 @@
 kg)%0A
-        #from bento._config import BENTO_SCRIPT%0A        #from bento.commands.configure import ConfigureCommand%0A        #from bento.commands.options import OptionsContext%0A        #from bento.compat.api import relpath%0A%0A        #from bento.commands.configure import _setup_options_parser%0A%0A        #package_options = PackageOptions.from_file(BENTO_SCRIPT)%0A%0A        #dist = self.distribution%0A        #top_node = dist.top_node%0A%0A        #cmd = ConfigureCommand()%0A        #options_ctx = OptionsContext.from_command(cmd)%0A        #_setup_options_parser(options_ctx, package_options)%0A%0A        #ctx = ConfigureYakuContext(argv, options_ctx, dist.pkg, dist.run_node)%0A        #ctx.package_options = package_options%0A%0A        #cmd_funcs = %5B(cmd.run, dist.top_node.abspath())%5D%0A%0A        #try:%0A        #    while cmd_funcs:%0A        #        cmd_func, local_dir = cmd_funcs.pop(0)%0A        #        local_node = top_node.find_dir(relpath(local_dir, top_node.abspath()))%0A        #        ctx.pre_recurse(local_node)%0A        #        try:%0A        #            cmd_func(ctx)%0A        #        finally:%0A        #            ctx.post_recurse()%0A%0A        #    cmd.shutdown(ctx)%0A        #finally:%0A        #    ctx.shutdown()%0A

remove dead code.