#!/usr/local/bin/perl
eval "exec /usr/local/bin/perl -S $0 $*"
    if $running_under_some_shell;
			# this emulates #! processing on NIH machines.
			# (remove #! line above if indigestible)

$command = "g++ -MM";
$include = "";

while ($_ = $ARGV[0], /^-/) {
  shift;
  last if /^--$/;
  /^(-I.*)/ && ($include .= " " . $1);
}

$mark = "# DO NOT DELETE THIS LINE -- pasdepend uses it";

open (OLD, "< Makefile");
open (NEW, "> Makefile.new");
while (<OLD>) {
  last if (/^$mark/);
  print NEW;
}
close (OLD);

print NEW "\n", $mark, "\n\n";

foreach $file (@ARGV) {
  do depend ($file);
}
close (NEW);

system ("mv Makefile Makefile.bak");
system ("mv Makefile.new Makefile");

sub depend {
  local ($file) = $_[0];
  open (PIPE, "$command $include $file |");
  while (<PIPE>) {
    print NEW;
  }
  close (PIPE);
  print NEW "\n";
}

