#!/usr/athena/bin/perl -w
#
# Reads a series of lines from stdin and attempts to locate the first
# occurrence of those lines in the file specified as the first
# argument. If the lines are found, makes a copy of that file (in
# filename.old-time) and then overwrites the file with a new version
# that has each of the stdin lines prefixed by "#SIPB_CHANGE ", and
# all of the other lines left untouched.
#
#
# With the example input file test.in:
# This
# is
# a
# test.
#
# % wc -c test.in
#     16 test.in
#
# % (echo is; echo a) | comment-out test.in
# causes test.in to then have the contents:
# This
# #SIPB_CHANGE is
# #SIPB_CHANGE a
# test.
#
#
#
# mhpower@mit.edu, 27 December 1998
#
#

@in = ();
@f = ();
$i_in = 0;
$i_f =  0;

while (defined ($c = <STDIN>))
 {
   chomp $c;
   push @in, $c;
   $i_in++;
 }
if (! defined($ARGV[0]))
  {
     die "usage: comment-out _filename_\n";
  }

$s = open INFILE, $ARGV[0];
if (! defined($s))
  {
     die "comment-out: could not open $ARGV[0] for read\n";
  }
while (defined ($c = <INFILE>))
  {
    chomp $c;
    push @f, $c;
    $i_f++;
  }
close INFILE;

if ($i_in > $i_f)
  {
     die "comment-out: stdin had more lines than the file\n";    
  }

$maxoffset = $i_f - $i_in;
for ($i = 0; $i <= $maxoffset; ++$i)
  {
    $s = &checkmatch($i);
    if ($s)
      {
         &createoutput($i);
         exit 0;
      }
  }
die "comment-out: could not find specified section in $ARGV[0]\n";


sub checkmatch
{
    my($offset) = @_;
    my $i;

    for ($i = 0; $i < $i_in; ++$i)
      {
        $s = &similarlines($in[$i], $f[$i + $offset]); 
        if (! $s)
          {
            return 0;
	  }
      }
    return 1;
}

sub createoutput
{
    my ($offset) = @_;
    my ($i, $j, $k);

    $t = &timedname();
    if (-e $t)
      {
        die "comment-out: backup file name $t already exists\n";
      }
    $newfile = $t . ".new";
    if (-e $newfile)
      {
        die "comment-out: temporary file name $t already exists\n";
      }
    $s = open OUTFILE, ">$newfile";
    if (! defined($s))
      {
         die "comment-out: could not open $t for write\n";
      }
    for ($i = 0; $i < $offset; ++$i)
      {
         print OUTFILE $f[$i] . "\n";
      }
    $k = 0;
    for ($j = $i; $k < $i_in; ++$j)
      {
         print OUTFILE "#SIPB_CHANGE " . $f[$j] . "\n";
         $k++;
     }
    for ($i = $j; $i < $i_f; ++$i)
      {
         print OUTFILE $f[$i] . "\n";
      }
    close OUTFILE or die "comment-out: could not close output file $t\n";
    rename $ARGV[0], $t;
    rename $newfile, $ARGV[0];
    # should get correct mode from old version of the file 
    chmod 0744, $ARGV[0];
}    
 
sub timedname
{
    # to ensure unique file names
    sleep 2;
    return $ARGV[0] . ".old-" . time();
}

# checks whether two strings are the same, allowing for differences in whitespace
# at the end of the strings
sub similarlines
{
    my ($s1, $s2) = @_;
    my ($i);

    &trailingws($s1);
    &trailingws($s2);

    if ($s1 eq $s2)
      {
        return 1;
      }
    return 0;
}

sub trailingws
{
    my($i);

    for ($i = length($_[0]) - 1; $i >= 0; $i--)
      {
        if (substr($_[0], $i, 1) =~ /\s/)
          {
             $_[0] = substr($_[0], 0, length($_[0]) - 1);
          }
        else
          {
             return 0;
          }
       }
}
