#!/usr/bin/perl -w

if (@ARGV < 1) {
	print "ootouch [--files <regex: .*\\.cxx>] <strings to find> [...]\n";
	exit (1);
}

my @regexps = ();
my $arg;
$filter = '';
while ($arg = shift @ARGV) {
    if ($arg eq '--files') {
	$filter = shift @ARGV;
    } else {
	push (@regexps, $arg);
    }
}

if (!$filter) {
    $filter = ".*\\.cxx";
}

sub touch_dir
{
    my $path = shift;
    my $regexp = shift;
    my $DIR;
    my $name;

    opendir ($DIR, $path) || die "Lost $path: $!";
    
    while ($name = readdir ($DIR)) {
	my $full_path = "$path/$name";

	($name eq '.' || $name eq '..') && next;
	-d $full_path && touch_dir ($full_path, $regexp);

	$name =~ m/$filter/ || next;

	my $file;
	open ($file, $full_path) || die "Can't open $full_path: $!";
	while (<$file>) {
	    if (/$regexp/) {
		print "Touch: '$full_path'\n";
		my $now = time;
		utime $now, $now, $full_path;
		last;
	    }
	}
	close ($file);
    }

    closedir ($DIR);
}

my $cwd;
chomp ($cwd = `pwd`);

my $str;
printf "Touching '$filter' files, ...\n";
for $str (@regexps) {
    touch_dir ($cwd, $str);
}
