1 | #!/usr/bin/perl -w
|
---|
2 |
|
---|
3 | ###########################################################################
|
---|
4 | #
|
---|
5 | # registermacrofile.pl - Add macrofile entry to etc/main.cfg file
|
---|
6 | #
|
---|
7 | # A component of the Greenstone digital library software
|
---|
8 | # from the New Zealand Digital Library Project at the
|
---|
9 | # University of Waikato, New Zealand.
|
---|
10 | #
|
---|
11 | # Copyright (C) 1999 New Zealand Digital Library Project
|
---|
12 | #
|
---|
13 | # This program is free software; you can redistribute it and/or modify
|
---|
14 | # it under the terms of the GNU General Public License as published by
|
---|
15 | # the Free Software Foundation; either version 2 of the License, or
|
---|
16 | # (at your option) any later version.
|
---|
17 | #
|
---|
18 | # This program is distributed in the hope that it will be useful,
|
---|
19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
21 | # GNU General Public License for more details.
|
---|
22 | #
|
---|
23 | # You should have received a copy of the GNU General Public License
|
---|
24 | # along with this program; if not, write to the Free Software
|
---|
25 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
26 | #
|
---|
27 | ###########################################################################
|
---|
28 |
|
---|
29 |
|
---|
30 | BEGIN {
|
---|
31 | die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
|
---|
32 | unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
|
---|
33 | }
|
---|
34 |
|
---|
35 |
|
---|
36 | ###########################################################################
|
---|
37 | #
|
---|
38 | # CODE SYNOPSIS
|
---|
39 | #
|
---|
40 | # This script registers a new macrofile by adding it to the "macrofiles"
|
---|
41 | # list in the $GSDLHOME/etc/main.cfg file. It:
|
---|
42 | #
|
---|
43 | # - Parses the required argument: macrofile name.
|
---|
44 | # - Checks if the macrofile specified is already defined in main.cfg.
|
---|
45 | # - If it isn't, add a new macrofile entry and rewrite main.cfg.
|
---|
46 | #
|
---|
47 | ###########################################################################
|
---|
48 |
|
---|
49 |
|
---|
50 | use util;
|
---|
51 |
|
---|
52 |
|
---|
53 | sub main
|
---|
54 | {
|
---|
55 | # Get the name of the macrofile being registered
|
---|
56 | local $macrofilename = shift(@_);
|
---|
57 |
|
---|
58 | # Check that the necessary arguments were supplied
|
---|
59 | if (!$macrofilename) {
|
---|
60 | print STDERR "Usage: registermacrofile.pl <macrofile-name>\n";
|
---|
61 | die "Error: Required argument missing.\n";
|
---|
62 | }
|
---|
63 |
|
---|
64 | # Casefold the macrofile name
|
---|
65 | $macrofilename =~ tr/A-Z/a-z/;
|
---|
66 |
|
---|
67 | # Open and read the etc/main.cfg file
|
---|
68 | local $gsdldir = "$ENV{'GSDLHOME'}";
|
---|
69 | local $maincfgfile = &util::filename_cat($gsdldir, "etc", "main.cfg");
|
---|
70 | open(MAIN_CFG_FILE, "<$maincfgfile") or die "Error: Could not open $maincfgfile.\n";
|
---|
71 | local @maincfglines = <MAIN_CFG_FILE>;
|
---|
72 | close(MAIN_CFG_FILE);
|
---|
73 |
|
---|
74 | # Check if the macrofile is already specified in the main.cfg file
|
---|
75 | local $lastmacroline;
|
---|
76 | local $inmacrofiles = 0;
|
---|
77 | for ($i = 0; $i < scalar(@maincfglines); $i++) {
|
---|
78 | local $line = $maincfglines[$i];
|
---|
79 |
|
---|
80 | # Remove any nasty whitespace at the end of the line
|
---|
81 | $line =~ s/(\s*)$//;
|
---|
82 |
|
---|
83 | # Does this line signal the start of the list of macrofiles?
|
---|
84 | if ($line =~ /^macrofiles\s+/i) {
|
---|
85 | $inmacrofiles = 1;
|
---|
86 | $line =~ s/^macrofiles\s+//i;
|
---|
87 | }
|
---|
88 |
|
---|
89 | if ($inmacrofiles == 1) {
|
---|
90 | # Remove any whitespace before the macros
|
---|
91 | $line =~ s/^\s*//;
|
---|
92 |
|
---|
93 | # If there isn't a trailing '\' on this line, it's the last line
|
---|
94 | if ($line !~ /\\$/) {
|
---|
95 | $lastmacroline = $i;
|
---|
96 | $inmacrofiles = 0;
|
---|
97 | }
|
---|
98 | # Otherwise remove the '\'
|
---|
99 | else {
|
---|
100 | $line =~ s/\\$//;
|
---|
101 | }
|
---|
102 |
|
---|
103 | # Casefold the line
|
---|
104 | $line =~ tr/A-Z/a-z/;
|
---|
105 | local @macrofiles = split(/\s+/, $line);
|
---|
106 |
|
---|
107 | # No need to register a macrofile that is already defined
|
---|
108 | for ($j = 0; $j < scalar(@macrofiles); $j++) {
|
---|
109 | if ($macrofiles[$j] eq $macrofilename) {
|
---|
110 | # print "Note: Macrofile '$macrofilename' is already defined.\n";
|
---|
111 | return;
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | # The macrofile has not been defined, so add a new entry to the main.cfg file
|
---|
118 | $maincfglines[$lastmacroline] =~ s/(\s*)$//;
|
---|
119 | if (length($maincfglines[$lastmacroline]) > 60) {
|
---|
120 | $maincfglines[$lastmacroline] .= " \\\n";
|
---|
121 | $maincfglines[$lastmacroline] .= " ";
|
---|
122 | }
|
---|
123 | $maincfglines[$lastmacroline] .= " $macrofilename\n";
|
---|
124 |
|
---|
125 | # Rewrite the (modified) main.cfg file
|
---|
126 | open(MAIN_CFG_FILE, ">$maincfgfile") or die "Error: Could not write $maincfgfile.\n";
|
---|
127 | foreach $line (@maincfglines) {
|
---|
128 | print MAIN_CFG_FILE "$line";
|
---|
129 | }
|
---|
130 | close(MAIN_CFG_FILE);
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | &main(@ARGV);
|
---|