#!/usr/bin/env python

__version__ = "2.0"

import optparse, sys, os.path

class InteractiveOptionParser (optparse.OptionParser):                
    def exit(self, status=0, msg=None):
        """self.error() raises an exception, so this should only be
        called when optparse intends to exit successfully."""
        if msg: print >>sys.stderr, msg

    def error(self, msg):
        """Raise an exception instead of exiting"""
        raise optparse.OptionValueError, msg

def handleArgs(argv, interactive):
    opArgs = dict(usage="%prog -n [slice] [location] [prefix]",
                  version="%prog " + "%s" % __version__)

    if interactive: parser = InteractiveOptionParser(**opArgs)
    else: parser = optparse.OptionParser(**opArgs)

    parser.add_option("-n", "--number", 
                      help="Look at time consumed by subset of simulation")

    return parser.parse_args(args=argv[1:]) + (parser,)

def main(argv=None, interactive=True):
    if argv is None: argv = sys.argv    
    options, args, parser = handleArgs(argv, interactive)    

    # Parse options, set defaults
    if len(args) >= 1: location = args[0]
    else: location = '.'
    if len(args) >= 2: prefixes = [args[1]]
    else: prefixes = ['snapshot', 'param']
    num = options.number or ':'
    
    # Find which prefix to use
    files = os.listdir(location)    
    for prefix in prefixes:
        snapshots = [ f for f in files if f.startswith(prefix)]
        if snapshots: break

    if not snapshots:
        parser.error("No Snapshots Found!")

    # Sort and prune snapshot list
    snapshots = sorted(snapshots)
    snapshots = eval('snapshots[%s]' % num)

    # Fetch times of snapshots in the list
    times = [os.path.getmtime(os.path.join(location, snapshot))
            for snapshot in snapshots]

    # Total up time spent
    total = 0.0
    last1 = times[1] - times[0]
    breaks = []    
    for i in range(len(times)-1):
        dt = times[i+1] - times[i]
        if dt > 3*last1:
            total += last1
            breaks.append(snapshots[i+1])
        else:
            total += dt
            last1 = dt

    if breaks:
        print "Breaks before",
        for br in breaks:
            print br,
        print ""
    print "From", snapshots[0], "to", snapshots[-1], "in", location
    print total/3600
            
if __name__ == '__main__':
    sys.exit(main(interactive=False))
