1#!/usr/local/bin/perl -w
2#
3# Merger to generate tv_grab_fi
4#
5use 5.008;
6use strict;
7use warnings;
8
9use File::Basename;
10
11# output file name
12my($outfile) = @ARGV
13  or die "no output file specified!";
14
15# working directory
16my $dir = dirname($0);
17
18# output file
19open(my $ofh, ">", $outfile)
20  or die "can't open output file: $!";
21
22# source modules
23my @sources = ( sort(<$dir/fi/*.pm>), sort(<$dir/fi/source/*.pm>));
24print "Found modules: ", map({ basename($_) . " " } @sources), "\n";
25
26# open main script
27open(my $ifh, "<", "$dir/tv_grab_fi.pl")
28  or die "can't open main script file: $!";
29
30# Merge
31my @versions;
32while (<$ifh>) {
33
34  # version marker
35  if (my($version) = /^use constant VERSION => \'\$Id: (\S+ \S+ \S+ \S+)/) {
36    push(@versions, $version);
37
38  # insert marker for source modules
39  } elsif (/^\# INSERT: SOURCES/) {
40
41    print $ofh <<END_OF_MERGE_TEXT;
42#
43#                   This is the merged version of the script.
44#
45#                !!! DO NOT EDIT - YOUR CHANGES WILL BE LOST !!!
46#
47#          Any changes should be done to the original modules instead.
48#
49###############################################################################
50END_OF_MERGE_TEXT
51
52    foreach my $source (@sources) {
53      open(my $sfh, "<", $source)
54	or die "can't open source module '$source': $!";
55      print "Inserting module '", basename($source), "'\n";
56      while (<$sfh>) {
57	push(@versions, $1) if /^\# VERSION: \$Id: (\S+ \S+ \S+ \S+)/;
58	next if 1../^\# INSERT FROM HERE /;
59	next if /^__END__/..0; # right side always false -> cut to the end
60	print $ofh $_;
61
62	# Don't insert the code if source module has been disabled
63	print($ofh <<END_OF_DISABLED_TEXT), last
64}
65
66# THIS DATA SOURCE HAS BEEN DISABLED!
67
681;
69END_OF_DISABLED_TEXT
70	  if /^\s*our\s+\$ENABLED\s*=\s*0;/;
71
72      }
73      close($sfh);
74      print $ofh "\n###############################################################################\n";
75    }
76
77  # delete marker for code
78  } elsif (/^\# CUT CODE START/../^\# CUT CODE END/) {
79
80  # insert version string
81  } elsif (/^use XMLTV::Version /) {
82    my $version = 'generated from\n\t' .
83      join('\n\t', map {
84	sprintf("%-25s  %-5s  %10s  %8s", /^(\S+),v (\S+) (\S+) (\S+)$/)
85      } @versions);
86    s/VERSION;$/"$version";/;
87    print $ofh $_;
88  # normal line
89  } else {
90    print $ofh $_;
91  }
92}
93
94# check for write errors
95close($ofh)
96  or die "error while writing to output file: $!";
97
98# set executable flag
99chmod(0755, $outfile);
100
101# That's all folks...
102print "Merge done.\n";
103exit 0;
104