A port of a port of AlanBBOS (20123) rewritten (quickly) in TypeScript