#!/usr/bin/env python
from   __future__ import print_function
import os
import re
import sys
import operator
import functools
from   jiveplot.functional import drap

if '-v' in sys.argv:
    print("$Id: jplotter,v 1.3 2014-08-08 15:38:40 jive_cc Exp $")
    sys.exit( 0 )

if '-h' in sys.argv:
    print("Usage: {0} [-h] [-d] [-v] [-c <cmd>]".format(os.path.basename(sys.argv[0])))
    print("""
    where:
        -h       print this message and exit"
        -v       print version and exit
        -c <cmd> execute <cmd> as if typed at the keyboard. if present
                 multiple times, commands are executed in command line
                 order. jplotter exits after the last command is executed.
        -d       turn on debug output [prints stack trace upon error]
        ppgplot=/path/to/home/of{/ppgplot/__init__.py}
            load ppgplot module from '/path/to/home/of' if that path exists
""")
    sys.exit( 0 )

# look for an entries "ppgplot=..." and insert them into the system path
# this map() is used for its side-effect(s) so for Py3 we must drain the
# iterable. functional.drap(...) combines drain() + map()
drap(lambda mo: sys.path.insert(0, mo.group(1)) if os.path.isdir(mo.group(1)) else None,
     filter(operator.truth, map(re.compile(r'^ppgplot=(.+)$').search, sys.argv[1:])))

# gobble all '-c <cmd>' commands together
def collect_cmds(acc, x):
    if acc.state is not None:
        # previous argument was '-c' so append this argument to the list
        # that is, if it's not an option itself
        if x[0]=='-' or x.startswith('ppgplot='):
            raise RuntimeError("-c option expects a command, not option '{0}'".format(x))
        acc.cmds.append(x)
        acc.state = None
    else:
        # do we see '-c'?
        if x == '-c':
            acc.state = x
    return acc
acc = functools.reduce(collect_cmds, sys.argv[1:], type('',(),{'state':None, 'cmds':list()})())
if acc.state is not None:
    raise RuntimeError("-c option missing argument")


# we run interactively from the commandline
from jiveplot import (command, jplotter)
jplotter.run_plotter(command.scripted(acc.cmds) if acc.cmds else command.readkbd("jcli"), debug=('-d' in sys.argv))
