#!/usr/bin/env python

import os
import subprocess as sp
import sys


def update_project(url):
    project_name = os.path.basename(url.split("//")[1])
    test_dir = os.path.join("test", project_name)
    if not os.path.exists(test_dir):
        cmd = "cd test && git clone -q %s" % url
    else:
        cmd = "cd %s && git fetch -q origin && git reset -q --hard origin/master"
        cmd = cmd % test_dir
    sp.check_call(cmd, shell=True)


def check_project(url):
    project_name = os.path.basename(url.split("//")[1])
    test_dir = os.path.join("test", project_name)
    try:
        sp.check_call("./emilio -f summary %s" % test_dir, shell=True)
    except:
        pass


def main():
    if len(sys.argv) != 2:
        print "usage: %s projects.txt" % sys.argv[0]
        exit(1)

    projects = []
    with open(sys.argv[1]) as handle:
        for line in handle:
            if not line.strip():
                continue
            projects.append(line.strip())

    for project in projects:
        print "Checking project: %s" % os.path.basename(project)
        print "-" * len(os.path.basename(project))
        print
        update_project(project)
        check_project(project)
        print
        print


if __name__ == "__main__":
    main()