
#! perl -w
# This version of grep is fully featured, but is written for clarity not speed.
use RISCOS::Throwback 'throwback_info';
use RISCOS::Filespec;
use RISCOS::File 'globlist';
use Getopt::Std;

use vars qw($opt_c $opt_t $opt_v);

delete $SIG{'__DIE__'};

sub printit ($) {
  print "$ARGV: " if ($lots);	# Onlyprint the filename if more than one
  print @_;
}

if (defined $ARGV[0] and $ARGV[0] eq '-throwback') {
    $throw = 1;
    $pattern = shift;
} else {
    $throw = 0;
}
getopts ('ctv');

$pattern = shift @ARGV;

die "$0 [-throwback] [-c] [-v]  pattern [files]" unless $pattern;

@ARGV = globlist( @ARGV );

$count = 0;
$throw = 0 if $opt_t;

$lots = @ARGV > 1;

while( <> )
{
  next unless $opt_v xor /$pattern/;

  $opt_c ? $count++ : printit ($_);
  
  if ($throw)
  {
      chomp;
      throwback_info( $ARGV, $., $_ );
  }
} continue {
  if (eof) {
    printit ("$count\n") if ($opt_c && $count); 
    $count = 0;
    close(ARGV); # Reset line numbers for each file
  }
}

