#!/usr/bin/env python

import os, subprocess

def run(*command, **kwargs):
    print "RUN:", " ".join(command)
    subprocess.check_call(command, **kwargs)

__dir__ = os.path.dirname(__file__)

images = {}

for name in os.listdir(__dir__):
    if "-" in name:
        with open(os.path.join(__dir__, name)) as fd:
            images[name] = fd.read()

def compare(n1, n2):
    if n1 in images[n2]:
        return -1
    elif n2 in images[n1]:
        return 1
    else:
        return cmp(n1, n2)

order = images.keys()
order.sort(compare)

for name in order:
    run("docker", "build", "-t", name, "-f", name, ".", cwd=__dir__)
