#!/usr/bin/env python3

import sys, glob, os, re

def main():
    if len(sys.argv) < 2:
        print("invalid args", file=sys.stderr)
        print("Usage: %s dirname" % sys.argv[0], file=sys.stderr)
        sys.exit(1)

    mods = [ re.sub(r'.+/prob(\d+)\.rs$', r'prob\1', file)
             for dir in sys.argv[1:] for file in glob.glob("%s/prob*.rs" % dir) ]
    mods.sort()

    for mod in mods:
        print("extern crate %s;" % mod)

    print("""
use super::problem::Problem;

pub static PROBLEMS: &'static [&'static Problem<'static>] = &[""")
    print(",\n".join(['    &Problem { id: %s, answer: %s::EXPECTED_ANSWER, solve: %s::solve}' % (int(re.sub('^prob0+', "", mod)), mod, mod) for mod in mods]))
    print("];")

    sys.exit(0)

if __name__ == '__main__':
    main()

