#!/bin/sh

dependencies=$1
modules=$2
result=$3

cat $modules | tr ' ' '\n' > $result
echo > $result~

while ! diff -q $result~ $result > /dev/null ; do
	cp $result $result~
	while read MOD ; do
		moddeps="$(grep ^$MOD: $dependencies | sed -e 's/.*://' ||:)"
		for DEP in $moddeps ; do
			if ! grep -q ^$DEP\$ $result ; then
				echo $DEP >> $result
				echo $MOD: $DEP
			fi
		done
	done < $result
done
