1# Generate tv_grep from tv_grep.in.  This processing is necessary for
2# the pod documentation, which should be partly autogenerated from
3# information provided by the Grep.pm library.
4#
5# We could avoid this step if we had the documentation in a separate
6# file - but then we'd need a separate step for producing
7# documentation.  The input file tv_grep.in is also a legal Perl
8# program, so debugging should not be any harder than it was.
9#
10# $Id: tv_grep.PL,v 1.8 2017/08/18 00:32:24 knowledgejunkie Exp $
11#
12
13use IO::File;
14use XMLTV;
15
16require './filter/Grep.pm';
17
18my $out = shift @ARGV; die "no output file given" if not defined $out;
19my $in = 'filter/tv_grep.in';
20my $in_fh = new IO::File "< $in" or die "cannot read $in: $!";
21my $out_fh = new IO::File "> $out" or die "cannot write to $out: $!";
22while (<$in_fh>) {
23    if (/^\s*\@PROGRAMME_CONTENT_TESTS\s*$/) {
24	# Query XMLTV.pm to find out what keys of programme exist.
25	# This is rather a duplicate of the usage message in tv_grep
26	# itself: should unify one day.
27	#
28	my %key_type = %{XMLTV::list_programme_keys()};
29 	foreach (sort keys %key_type) {
30	    my ($arg, $matcher) = @{XMLTV::Grep::get_matcher($_)};
31	    if (not defined $arg) {
32		print $out_fh "B<--$_>\n\n";
33	    }
34	    elsif ($arg eq 'regexp') {
35		print $out_fh "B<--$_> REGEXP\n\n";
36	    }
37	    elsif ($arg eq 'empty') {
38		print $out_fh "B<--$_> ''\n\n";
39	    }
40	    else {
41		die "bad arg type from get_matcher(): $arg";
42	    }
43 	}
44    }
45    else {
46	print $out_fh $_;
47    }
48}
49close $out_fh or die "cannot close $out: $!";
50close $in_fh or die "cannot close $in: $!";
51
52