# ------------------------------------------------------------------------------
#  Rips tabs out of a document.  $tabsize should be set to the size in spaces
#  that the original tab would have produced.
#
#  change $path to the path your file is in, $infile to the file to process
#  and $outfile to the name of the outputfile.
# 
#  there is a bug such that if the % symbol is in the source document this
#  routine does strange things... I haven't been able to fix it...
#  one suggestion would be to do a global search and replace of % with something
#  before running this script on a document .
#
# ------------------------------------------------------------------------------ 
#!/usr/bin/perl
die "usage: $0 path infile outfile ..\n" unless @ARGV;
$tab = "\t";
$tabsize = 5;  # TAB SIZE IN ORIGINAL DOCUMENT

$path = $ARGV[0];
$infile = $ARGV[1];
$outfile = $ARGV[2];
$i = 0;
foreach $one(@ARGV){

  print "ARGV $i is $one";
  $i++;
}


open("THISFILE", "+<".$path.$infile);
open("OUTFILE", ">".$path.$outfile);

@myfile = <THISFILE>;

foreach $line (@myfile) 
{
   $newline = "";
   $stream = $line;
   $head = "";
   while(  length($stream) > 0 )      
    {
      ($istab, $tail) = $stream =~ /(.)(.*)/;
       if( $istab eq $tab )
       {
          $newline = $newline.$head;
          $numspaces = $tabsize - (length($newline) % $tabsize);
          $whitespace = "";
          for( $spaces = 1; $spaces <= $numspaces; $spaces++ )
          {
             $whitespace = $whitespace." ";
          }
          $stream = $tail;
          $newline = $newline.$whitespace;
          $head = "";
       }
       else
       {
       $head = $head.$istab;
       $stream = $tail;
       }
   }
   $newline = $newline.$head.$stream."\n";
   printf( OUTFILE $newline );
}
close( THISFILE ); close(OUTFILE);






