I have come to believe this is one of those problems that is not to be optimally solved with a script, but a programming language lbrtchx