import sys
import os
import argparse
import importlib


class Exploit:

    def __init__(self):
        self.parser = argparse.ArgumentParser()
        self.defaultArguments()

    def defaultArguments(self):
        self.parser.add_argument("-c", "--cmd", help="command to run")
        self.parser.add_argument('-o', "--output", help="Output file")
        self.parser.add_argument('-t', "--tool", help="Run tool")
        self.parser.add_argument('tail', nargs=argparse.REMAINDER)

    def run(self):
        self.args = self.parser.parse_args()
        if self.args.tool:
            mod = importlib.import_module(self.args.tool)
            main = getattr(mod, "main")
            return main(self.args.tail)
        return self.job(self.args)

    def paramOrTail(self, params):
        for p in params:
            if hasattr(self.args, p):
                v = getattr(self.args, p)
                if v is not None:
                    return v
        return ' '.join(self.args.tail)

    def getCommand(self, moreParams=[]):
        return self.paramOrTail(['cmd'] + moreParams)

    def outfile(self):
        if not self.args.output:
            return sys.stdout
        return open(self.args.output, "wb")

    def job(self, args):
        with self.outfile() as f:
            f.write(self.getCommand(['url']) + "\n")
        raise Exception("Not Implemented")


if __name__ == "__main__":
    Exploit().run()
