#! /usr/bin/perl -w

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# currentely only RE filter are implemented

use utf8;
use encoding 'utf8';
use open ':utf8'; # input and output default layer will be UTF-8
use POSIX;
my $script="$0";
my $re_script=$script;
my $re_filters=$script;
sub list_filters {
	opendir D, $re_filters;
	my @a=readdir D;
	my $i;
	@a=grep { ($_ ne ".") && ($_ ne "..") && ($_ ne "+") } @a;
	if ($#a >= 0) {
	@a=sort @a;
	print " Available filters are:\n";
	foreach $i (@a) { print "$i\t";}
	print "\n";
	} else { print "No filters found in [$re_filters]\n"}
	closedir D;
}
$re_script=~s/[^\/]+$/th-filter-re/g;
$re_filters=~s/[^\/]+$/..\/share\/thwab-lib\/re-filters/g;
$filter=shift;
if ($filter eq "-l") {
	list_filters();
	exit(0);
} else {
	$re_filter=$re_filters."/".$filter;
	if (not -f "$re_filter") {
	  print "Filter [$re_filter] is not found\n";
	  list_filters();
	  exit(-1);
	} else {
	  open F, $re_filter;
	  while(<F>) {
	  chomp;
	  if (/^\s*#/o) {next}
	  @re_args=split "\t";
	  if ($#re_args==2) {
	    #print "calling: \'$re_script\' \'$re_args[0]\' \'$re_args[1]\' \'$re_args[2]\'\n";
	    system "\'$re_script\' \'$re_args[0]\' \'$re_args[1]\' \'$re_args[2]\'";}
	  }
	  close F;
	}
}
