#!/usr/bin/env python

import importlib
import os
import pkgutil
import re
import sys

from ripe.atlas.tools import commands
from ripe.atlas.tools.exceptions import RipeAtlasToolsException


class RipeAtlas(object):

    def __init__(self):
        self.command = None
        self.args = []
        self.kwargs = {}

    @staticmethod
    def _get_available_commands():
        """
        Get a list of commands that we can execute.  By default, we have a fixed
        that we make available in this directory, but the user can create her
        own plugins and store them at ~/.config/ripe-atlas-tools/commands/.  If
        we find any files there, we add them to the list here.
        """

        paths = [os.path.dirname(commands.__file__)]

        if "HOME" in os.environ:
            paths += [os.path.join(
                os.environ["HOME"], ".config", "ripe-atlas-tools", "commands")]

        r = [package_name for _, package_name, _ in pkgutil.iter_modules(paths)]
        r.remove("base")

        return r

    def _setup_command(self):

        caller = os.path.basename(sys.argv[0])
        shortcut = re.match('^a(ping|traceroute|dig|sslcert|ntp|http)$', caller)

        available_commands = self._get_available_commands()
        if shortcut:
            self.command = "measure"
            sys.argv.insert(1, self._translate_shortcut(shortcut.group(1)))
        else:
            if len(sys.argv) < 2 or sys.argv[1] in ("-h", "--help"):
                raise RipeAtlasToolsException(
                    "Usage: ripe-atlas <{}> [arguments]".format(
                        "|".join(set(available_commands) - {"shibboleet"})
                    )
                )
            self.command = sys.argv.pop(1)

    @staticmethod
    def _translate_shortcut(shortcut):
        if shortcut == "dig":
            return "dns"
        return shortcut

    def main(self):

        self._setup_command()

        try:

            module = importlib.import_module(
                "ripe.atlas.tools.commands.{}".format(self.command))

            #
            # If the imported module contains a `Factory` class, execute that
            # to get the `cmd` we're going to use.  Otherwise, we expect there
            # to be a `Command` class in there.
            #

            if hasattr(module, "Factory"):
                cmd = module.Factory(*self.args, **self.kwargs).create()
            else:
                cmd = module.Command(*self.args, **self.kwargs)

            cmd.init_args()
            cmd.run()

        except ImportError:

            raise RipeAtlasToolsException("No such command.")


if __name__ == '__main__':
    try:
        sys.exit(RipeAtlas().main())
    except RipeAtlasToolsException as e:
        e.write()
        raise SystemExit()
