#!/usr/bin/perl


foreach $file (@ARGV) {

    if ( $file =~ /(.*).tcs$/ ) {

	$outfile = "$1.dat";
	@dep = (calc_tcs_deps($file));
    
	print "$outfile: @dep\n";
    }
}


sub find_tcs_includes {
    
    my $f = shift @_;

    if ( ! defined $includes{$f} ) {
	if ( -f $f ) {
	    $includes{$f} = 
		[split ' ', `awk '/^include/ {printf \$2 " "}' < $f`];
	} else {
	    $includes{$f} = [];
	}
    }
    
    @{$includes{$f}};
}


sub calc_tcs_deps {
    
    my $f = shift @_;

    if (! defined $deps{$f} ) {

	%hash = 
	    map { $_, 1 } ($f, find_tcs_includes($f), 
			   map {calc_tcs_deps($_) } find_tcs_includes($f));
	
	
	$deps{$f} = [ keys %hash ];
    }

    sort @{$deps{$f}};
}
