source: trunk/gsdl/bin/script/registerlanguage.pl@ 4831

Last change on this file since 4831 was 4831, checked in by mdewsnip, 21 years ago

These scripts (register*.pl) are used to support the new Greenstone language packs. When a language pack is installed, these are used to add entries to the etc/main.cfg file to register the new interfaces.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.6 KB
Line 
1#!/usr/bin/perl -w
2
3###########################################################################
4#
5# registerlanguage.pl - Add "Language" 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
30BEGIN {
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 interface language by adding a "Language"
41# entry to the $GSDLHOME/etc/main.cfg file. It:
42#
43# - Parses the three required arguments: language name, ISO 639 language
44# code, and encoding name.
45# - Checks if the language specified is already defined in main.cfg.
46# - If it isn't, add a new "Language" entry and rewrite main.cfg.
47#
48###########################################################################
49
50
51use util;
52
53
54sub main
55{
56 # Get the code of the language being registered
57 local $languagecode = shift(@_);
58 # Get the name of the language being registered
59 local $languagename = shift(@_);
60 # Get the name of the encoding for the language being registered
61 local $encodingname = shift(@_);
62
63 # Check that the necessary arguments were supplied
64 if (!$languagecode || !$languagename || !$encodingname) {
65 print STDERR "Usage: registerlanguage.pl <language-code> <language-name> <encoding-name>\n";
66 die "Error: Required argument missing.\n";
67 }
68
69 # Casefold the language code and name
70 $languagecode =~ tr/A-Z/a-z/;
71 $languagename =~ tr/A-Z/a-z/;
72
73 # Open and read the etc/main.cfg file
74 local $gsdldir = "$ENV{'GSDLHOME'}";
75 local $maincfgfile = &util::filename_cat($gsdldir, "etc", "main.cfg");
76 open(MAIN_CFG_FILE, "<$maincfgfile") or die "Error: Could not open $maincfgfile.\n";
77 local @maincfglines = <MAIN_CFG_FILE>;
78 close(MAIN_CFG_FILE);
79
80 # Check if the language is already defined in the main.cfg file
81 local $lastlangline;
82 for ($i = 0; $i < scalar(@maincfglines); $i++) {
83 local $line = $maincfglines[$i];
84
85 # Remove any nasty whitespace at the end of the line
86 $line =~ s/(\s*)$//;
87
88 # Does this line contain a Language entry?
89 if ($line =~ /^Language\s+/i) {
90 local @languageparts = split(/\s+/, $line);
91
92 # Read the language code and name
93 local $langcode = "";
94 local $langname = "";
95 for ($j = 1; $j < scalar(@languageparts); $j++) {
96 if ($languageparts[$j] =~ /^shortname=(.*)$/i) {
97 $langcode = $1;
98 }
99 if ($languageparts[$j] =~ /^longname=(.*)$/i) {
100 $langname = $1;
101 }
102 }
103
104 # Casefold the language code and name
105 $langcode =~ tr/A-Z/a-z/;
106 $langname =~ tr/A-Z/a-z/;
107
108 # No need to register a language that is already defined
109 if ($langcode eq $languagecode) {
110 # print "Note: Language with code '$languagecode'";
111 # if ($langname eq $languagename) {
112 # print " and name '$languagename'";
113 # }
114 # print " is already defined.\n";
115 return;
116 }
117
118 # Remember the last line containing a Language entry
119 $lastlangline = $i;
120 }
121 }
122
123 # The language has not been defined, so add a new entry to the main.cfg file
124 local $languagenametc = $languagename;
125 substr($languagenametc, 0, 1) =~ tr/a-z/A-Z/; # Change language name to title case
126 local $languageline = "Language shortname=$languagecode longname=$languagenametc" .
127 " default_encoding=$encodingname";
128 $maincfglines[$lastlangline] = $maincfglines[$lastlangline] . $languageline . "\n";
129
130 # Rewrite the (modified) main.cfg file
131 open(MAIN_CFG_FILE, ">$maincfgfile") or die "Error: Could not write $maincfgfile.\n";
132 foreach $line (@maincfglines) {
133 print MAIN_CFG_FILE "$line";
134 }
135 close(MAIN_CFG_FILE);
136}
137
138
139&main(@ARGV);
Note: See TracBrowser for help on using the repository browser.