#!/bin/env python
import glob
import os
import sys
from itertools import chain
from string import Template

BASE_DIR = os.path.dirname(__file__)
TEMPLATE = Template("""
check-doc-${name}:
  extends: .check-doc
  variables:
    PACKAGE: ${package}

trigger-doc-build-${name}:
  extends: .trigger-doc-build
  variables:
    PACKAGE: ${package}

test-${name}-sqlite:
  extends: .test-sqlite
  variables:
    PACKAGE: ${package}
    TAG_PATTERN: '/^${tag_name}-.*/'

test-${name}-postgresql:
  extends: .test-postgresql
  variables:
    PACKAGE: ${package}
    TAG_PATTERN: '/^${tag_name}-.*/'
""")


def main(filename):
    if filename == '-':
        render(sys.stdout)
    else:
        with open(filename, 'w') as file:
            render(file)


def render(file):
    packages = sorted(all_packages())
    for package in packages:
        name = tag_name = package.replace('/', '-')
        if tag_name.startswith('modules-'):
            tag_name = tag_name[len('modules-'):]
        mapping = {
            'name': name,
            'package': package,
            'tag_name': tag_name,
            }
        file.write(TEMPLATE.substitute(mapping))


def all_packages():
    root_dir = os.path.dirname(BASE_DIR)
    for filename in chain(
            glob.glob('modules/*/setup.py', root_dir=root_dir)):
        yield os.path.dirname(filename)


if __name__ == '__main__':
    try:
        filename = sys.argv[1]
    except IndexError:
        filename = '-'
    main(filename)
