#!/usr/bin/python3

import sys, os

if len(sys.argv) < 5:
    print("usage: %s infile.beancount matches.beancount nonmatches.beancount <matchstr> [matchstr...]", file=sys.stderr)
    print("infile.beancount will be split into two files: one that matches the match strings you passed, and another that does not match.", file=sys.stderr)
    sys.exit(os.EX_USAGE)

matchers = sys.argv[4:]
infile = open(sys.argv[1], "r")
chunk = None
lastone = None

def match(text):
    return all (m in text for m in matchers)


def start_condition(line):
    return line.startswith("20") or line.startswith(";")

def end_condition(line):
    return not bool(line.rstrip("\n"))


with open(sys.argv[2], "w") as matched:
    with open(sys.argv[3], "w") as notmatched:
        for line in infile:
            if chunk is not None:
                chunk += line
                if end_condition(line):
                    # Chunk over.  We print.
                    lastone = (matched if match(chunk) else notmatched)
                    lastone.write(chunk)
                    chunk = None
            else:
                if start_condition(line):
                    chunk = line
                else:
                    matched.write(line)
                    notmatched.write(line)

        if chunk is not None:
            (matched if match(chunk) else notmatched).write(chunk)
