1 | #!/usr/bin/perl -w
|
---|
2 |
|
---|
3 | ###########################################################################
|
---|
4 | #
|
---|
5 | # registerencoding.pl - Add "Encoding" 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 encoding by adding an "Encoding" entry to
|
---|
41 | # the $GSDLHOME/etc/main.cfg file. It:
|
---|
42 | #
|
---|
43 | # - Parses the three required arguments: encoding name, encoding
|
---|
44 | # description, and mapfile name.
|
---|
45 | # - Checks if the encoding specified is already defined in main.cfg.
|
---|
46 | # - If it isn't, add a new "Encoding" entry and rewrite main.cfg.
|
---|
47 | #
|
---|
48 | ###########################################################################
|
---|
49 |
|
---|
50 |
|
---|
51 | use util;
|
---|
52 |
|
---|
53 |
|
---|
54 | sub main
|
---|
55 | {
|
---|
56 | # Get the short name of the encoding being registered
|
---|
57 | local $encodingname = shift(@_);
|
---|
58 | # Get the long name of the encoding being registered
|
---|
59 | local $encodingdesc = shift(@_);
|
---|
60 | # Get the name of the map file for the encoding being registered
|
---|
61 | local $mapfilename = shift(@_);
|
---|
62 |
|
---|
63 | # Read any extra arguments
|
---|
64 | local $option_multibyte_encoding = 0;
|
---|
65 | local $optionalarg = shift(@_);
|
---|
66 | while ($optionalarg) {
|
---|
67 | if ($optionalarg eq "-multibyte") { $option_multibyte_encoding = 1; }
|
---|
68 | $optionalarg = shift(@_);
|
---|
69 | }
|
---|
70 |
|
---|
71 | # Check that the necessary arguments were supplied
|
---|
72 | if (!$encodingname || !$encodingdesc || !$mapfilename) {
|
---|
73 | print STDERR "Usage: registerencoding.pl <encoding-name> <encoding-desc> <mapfile-name> [-multibyte]\n";
|
---|
74 | die "Error: Required argument missing.\n";
|
---|
75 | }
|
---|
76 |
|
---|
77 | # Casefold the encoding name
|
---|
78 | $encodingname =~ tr/A-Z/a-z/;
|
---|
79 |
|
---|
80 | # Open and read the etc/main.cfg file
|
---|
81 | local $gsdldir = "$ENV{'GSDLHOME'}";
|
---|
82 | local $maincfgfile = &util::filename_cat($gsdldir, "etc", "main.cfg");
|
---|
83 | open(MAIN_CFG_FILE, "<$maincfgfile") or die "Error: Could not open $maincfgfile.\n";
|
---|
84 | local @maincfglines = <MAIN_CFG_FILE>;
|
---|
85 | close(MAIN_CFG_FILE);
|
---|
86 |
|
---|
87 | # Check if the encoding is already defined in the main.cfg file
|
---|
88 | local $lastencline;
|
---|
89 | for ($i = 0; $i < scalar(@maincfglines); $i++) {
|
---|
90 | local $line = $maincfglines[$i];
|
---|
91 |
|
---|
92 | # Remove any nasty whitespace at the end of the line
|
---|
93 | $line =~ s/(\s*)$//;
|
---|
94 |
|
---|
95 | # Does this line contain an Encoding entry?
|
---|
96 | if ($line =~ /^Encoding\s+/i) {
|
---|
97 | local @encodingparts = split(/\s+/, $line);
|
---|
98 |
|
---|
99 | # Read the encoding name
|
---|
100 | local $encname = "";
|
---|
101 | for ($j = 1; $j < scalar(@encodingparts); $j++) {
|
---|
102 | if ($encodingparts[$j] =~ /^shortname=(.*)$/i) {
|
---|
103 | $encname = $1;
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | # Casefold the encoding name
|
---|
108 | $encname =~ tr/A-Z/a-z/;
|
---|
109 |
|
---|
110 | # No need to register an encoding that is already defined
|
---|
111 | if ($encname eq $encodingname) {
|
---|
112 | # print "Note: Encoding with name '$encodingname' is already defined.\n";
|
---|
113 | return;
|
---|
114 | }
|
---|
115 |
|
---|
116 | # Remember the last line containing an Encoding entry
|
---|
117 | $lastencline = $i;
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | # The encoding has not been defined, so add a new entry to the main.cfg file
|
---|
122 | local $encodingline = "Encoding shortname=$encodingname \"longname=$encodingdesc\"" .
|
---|
123 | " map=$mapfilename";
|
---|
124 | if ($option_multibyte_encoding == 1) {
|
---|
125 | $encodingline = $encodingline . " multibyte";
|
---|
126 | }
|
---|
127 | $maincfglines[$lastencline] = $maincfglines[$lastencline] . $encodingline . "\n";
|
---|
128 |
|
---|
129 | # Rewrite the (modified) main.cfg file
|
---|
130 | open(MAIN_CFG_FILE, ">$maincfgfile") or die "Error: Could not write $maincfgfile.\n";
|
---|
131 | foreach $line (@maincfglines) {
|
---|
132 | print MAIN_CFG_FILE "$line";
|
---|
133 | }
|
---|
134 | close(MAIN_CFG_FILE);
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | &main(@ARGV);
|
---|