#!/usr/bin/env python
# vim: ai:sw=4:ts=4

import sys
import os
import subprocess
import re

try:
    import pystache
except ImportError:
    print(
        '''
Error: This project requires the pystache Python module in order to finish
the maintainer setup. Please install it and re-run autocontrib.

The pystache module can be installed using pip:

    pip install pystache
'''
    )
    exit(1)


class BlockTemplate():

    def __init__(self):
        self.renderer = pystache.Renderer()
        self.contribs = []
        self.shortlog = ""

    def raw_contributors(self):
        patt = re.compile(r'\d+\s+(.+)\s+<(.+)>')
        matches = patt.findall(self.shortlog)
        for item in matches:
            self.contribs.append({'name': item[0], 'email': item[1]})

    def load_shortlog(self):
        self.shortlog = subprocess.check_output(['git', 'shortlog', '-es'], universal_newlines = True)

    def process_contribs(self, tpl_data):
        self.load_shortlog()
        self.raw_contributors()
        return self.renderer.render(tpl_data, { 'contributors' : self.contribs })


def main():
    tpl_name = sys.argv[-1]
    with open(tpl_name, 'r') as tpl_file:
        tpl_contents = tpl_file.read()
    contribs_tpl = BlockTemplate()
    outfile_name = tpl_name.replace('.mustache','')
    with open(outfile_name, 'w') as outfile:
        outfile.write(contribs_tpl.process_contribs(tpl_contents))


if __name__ == '__main__':
    main()
