#!/usr/local/bin/perl -w

#           formake.prl = FORmat MAKEfiles
# This is a simple utility for making sure that makefiles
# have the right format: tabs before rules, backslash followed
# only by newline at the end of lines to be continued.
#
#     E.F., late 1996
#

foreach $file (@ARGV)
{
   if (! -T $file)
   {
      print "$file: not a text file or not found\n";
      next FILE;
   }
   print "$file\n";
   ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
         $atime, $mtime, $ctime, $blksize, $blocks) =
         stat($file);
   $tmpfile = "_" . substr($file,1);
   if (-s $tmpfile)
   {
      $tmpfile = "__" . substr($file,2);
      if (-s $tmpfile)
      {
         die "Can't make temporary file name for $file\n";
      }
   }
   open(OUT, ">$tmpfile") || die "Can't open $tmpfile for $file\n";
   open(IN, "<$file") || die "Can't open $file\n";
   binmode(OUT);
   binmode(IN);
   while ( <IN> )
   {
      s/^\s+(\S+)/\t$1/;
      s/[ \t]+$//;
      print OUT;
   }
   close(IN);
   close(OUT);
   unlink($file) || die "Can't unlink $file\n";
   rename($tmpfile, $file) || die "Can't rename $tmpfile to $file\n";
   utime ($atime, $mtime, $file) || die "Can't fix time of $file\n";
}

