#! /usr/bin/python

import sys, os.path
from twisted.python import usage

# preamble to allow bin/flogtool to be run out of an uninstalled source tree
where = os.path.realpath(sys.argv[0]).split(os.sep)
if len(where) >= 2 and where[-2] == "bin":
    base = os.sep.join(where[:-2])
if (os.path.exists(os.path.join(base, "setup.py")) and
    os.path.exists(os.path.join(base, "foolscap"))):
    sys.path.insert(0, base)

from foolscap.logging.tail import TailOptions
from foolscap.logging.gatherer import GatherOptions
from foolscap.logging.dumper import DumpOptions
from foolscap.logging.web import WebViewerOptions

class Options(usage.Options):
    synopsis = "Usage: flogtool (tail|gather|dump) args.."

    subCommands = [
        ("tail", None, TailOptions, "follow logs of the target node"),
        ("gather", None, GatherOptions,
         "run as a daemon, record all logs to the current directory"),
        ("dump", None, DumpOptions,
         "dump the logs recorded by 'logtool gather'"),
        ("web-viewer", None, WebViewerOptions,
         "view the logs through a web page"),
        ]

    def postOptions(self):
        if not hasattr(self, 'subOptions'):
            raise usage.UsageError("must specify a command")

    def opt_help(self):
        print self.synopsis
        sys.exit(0)


def dispatch(command, options):
    if command == "tail":
        from foolscap.logging.tail import LogTail
        lt = LogTail()
        lt.run(options.target_furl)

    elif command == "gather":
        from foolscap.logging.gatherer import LogGatherer
        lg = LogGatherer()
        lg.run()

    elif command == "dump":
        from foolscap.logging.dumper import LogDumper
        ld = LogDumper()
        ld.run(options)

    elif command == "web-viewer":
        from foolscap.logging.web import WebViewer
        wv = WebViewer()
        wv.run(options)

    else:
        print "unknown command '%s'" % command
        raise NotImplementedError

if __name__ == '__main__':
    config = Options()
    try:
        config.parseOptions()
    except usage.error, e:
        print "%s:  %s" % (sys.argv[0], e)
        print
        c = getattr(config, 'subOptions', config)
        print str(c)
        sys.exit(1)

    command = config.subCommand
    so = config.subOptions
    dispatch(command, so)
