#!/usr/bin/env python3

import os
import sys

import parse_csv

cur_dir = os.path.dirname(os.path.realpath(__file__))


def utf_stdout(str_):
    sys.stdout.buffer.write(str_.encode("utf-8"))


if __name__ == "__main__":
    if len(sys.argv) != 2 or sys.argv[1] not in {"blocks", "lang"}:
        print("Usage: {} blocks|lang".format(sys.argv[0]))
        sys.exit(1)
    db = parse_csv.db(os.path.join("src", "csv", "railtypes.csv"))
    db.finalize()
    if sys.argv[1] == "blocks":
        output = '\n'.join([block.nml() for block in db.blocks()])
    elif sys.argv[1] == "lang":
        all_strings = ["{} :{}".format(v, k) for k, v in db.strings.items()]
        output = '\n'.join(sorted(all_strings))
    utf_stdout(output)
    utf_stdout('\n')
