#!/usr/bin/env python

"""Wrapper script that runs a pkgcore script from sys.path."""

from importlib import import_module
import os
import sys

if __name__ == '__main__':
    try:
        from pkgcore.util import commandline
        name = os.path.basename(sys.argv[0]).replace("-", "_")
        script = import_module('pkgcore.scripts.%s' % (name,))
    except ImportError as e:
        sys.stderr.write(str(e) + '!\n')
        sys.stderr.write(
            'Verify that snakeoil and pkgcore are properly installed '
            'and/or PYTHONPATH is set correctly for python %s.\n' %
            (".".join(map(str, sys.version_info[:3])),))
        if '--debug' in sys.argv:
            raise
        sys.stderr.write('Add --debug to the commandline for a traceback.\n')
        sys.exit(1)

    subcommands = getattr(script, 'argparser', None)
    commandline.main(subcommands)
