adding SMS stuff so we don't forget about it
[koha.git] / rewrite-config.PL
1 # Copyright 2007 MJ Ray
2 #
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
17 #
18 # Current maintainer MJR http://mjr.towers.org.uk/
19 # See http://www.koha.org/wiki/?page=KohaInstaller
20
21 use Sys::Hostname;
22 use Socket;
23
24 =head1 NAME
25
26 rewrite-config.PL - helper for the Koha packager and installer
27
28 =head1 SYNOPSIS
29
30         perl rewrite-config.PL configurationfile
31
32 =head1 DESCRIPTION
33
34 This helper script replaces keywords in the
35 configuration files with value either supplied through
36 the environment (with export, or by putting them on
37 the start of the make command line) or with reasonable
38 guesses worked out by the script.
39
40 =head2 KEYWORDS
41
42 The following configuration keywords are available:
43
44 PREFIX,
45 BASE_DIR, CGI_DIR, LOG_DIR,
46 MYSQL_DB, MYSQL_HOST, MYSQL_PASS, MYSQL_USER, WEBMASTER_EMAIL, WEBSERVER_DOMAIN,
47 WEBSERVER_HOST, WEBSERVER_IP, WEBSERVER_PORT, WEBSERVER_PORT_LIBRARIAN, ZEBRA_PASS, ZEBRA_USER
48
49 =head1 EXAMPLES
50
51 To override the guessed hostname and email address, run:
52
53         WEBSERVER_HOST=mysecrethostname.com.invalid \
54         WEBMASTER_EMAIL=webmaster@publichost.com make install
55
56 Note that if WEBSERVER_HOST does not resolve to an IP address, you will
57 also need to override WEBSERVER_IP.
58
59 =cut
60
61 $myhost = hostname();
62 $mydomain = $myhost;
63 $mydomain =~ s/^.*?\.//;
64 # This is set here to rescue systems with broken DNS
65 $myip = $ENV{'WEBSERVER_IP'} || inet_ntoa(scalar gethostbyname($myhost||'localhost')) || die "Cannot get our own IP address: DNS fault?";
66 $prefix = $ENV{'PREFIX'} || "/usr";
67
68 # These are our configuration guesses
69 # Keys were extracted by
70 # <grep -o '__.*__' etc/* | cut -f2 -d: | sort -u | sed -e 's/^/  "/;s/$/" => "",/'
71 %configuration = (
72   "__BASE_DIR__" => ($ENV{'INSTALLSITELIB'} || sprintf($prefix."/lib/perl5/site_perl/%vd",$^V))."/koha",
73   "__CGI_DIR__" => $prefix."/lib/cgi-bin/koha",
74   "__LOG_DIR__" => "/var/log",
75   "__MYSQL_DB__" => "koha",
76   "__MYSQL_HOST__" => $myhost,
77   "__MYSQL_PASS__" => "katikoan",
78   "__MYSQL_USER__" => "kohaadmin",
79   "__PREFIX__" => $prefix,
80   "__WEBMASTER_EMAIL__" => 'webmaster@'.$mydomain,
81   "__WEBSERVER_DOMAIN__" => $mydomain,
82   "__WEBSERVER_HOST__" => $myhost,
83   "__WEBSERVER_IP__" => $myip,
84   "__WEBSERVER_PORT__" => "80",
85   "__WEBSERVER_PORT_LIBRARIAN__" => "8080",
86   "__ZEBRA_PASS__" => "zebrastripes",
87   "__ZEBRA_USER__" => "kohauser",
88 );
89
90 # Override configuration from the environment
91 foreach $key (keys %configuration) {
92   if (defined($ENV{$key})) {
93     $configuration{$key} = $ENV{$key};
94   }
95 }
96
97 $fname = $ARGV[0];
98 $file = read_file($fname);
99 $file =~ s/__.*?__/$configuration{$&}/seg;
100 chmod 0644, $fname;
101 open(OUTPUT,">$fname") || die "Can't open $fname for write: $!";
102 print OUTPUT $file;
103 close(OUTPUT);
104
105 # Idea taken from perlfaq5
106 sub read_file($) {
107   local(*INPUT,$/);
108   open(INPUT,$_[0]) || die "Can't open $_[0] for read";
109   my $file = <INPUT>;
110   return $file;
111 }
112
113 __END__
114
115
116 =head1 SEE ALSO
117
118 Makefile.PL, ExtUtils::MakeMaker(3)
119
120 =head1 AUTHOR
121
122 MJ Ray mjr at phonecoop.coop
123
124 =cut
125