Bug 6540 - Make ZEBRA_MARC_FORMAT and ZEBRA_LANGUAGE configurable for koha-create
[koha_ffzg] / debian / scripts / koha-create
1 #!/bin/sh
2 #
3 # koha-create -- Create a new Koha instance.
4 # Copyright 2010  Catalyst IT, Ltd
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
20 set -e
21
22
23 die() {
24     echo "$@" 1>&2
25     exit 1
26 }
27
28
29 generate_config_file() {
30     touch "$2"
31     chown "root:$username" "$2"
32     chmod 0640 "$2"
33     sed -e "s/__KOHASITE__/$name/g" \
34         -e "s/__OPACPORT__/80/g" \
35         -e "s/__INTRAPORT__/$INTRAPORT/g" \
36         -e "s/__OPACSERVER__/$domain/g" \
37         -e "s/__INTRASERVER__/$intradomain/g" \
38         -e "s/__ZEBRA_PASS__/$zebrapwd/g" \
39         -e "s/__ZEBRA_MARC_FORMAT__/$ZEBRA_MARC_FORMAT/g" \
40         -e "s/__ZEBRA_LANGUAGE__/$ZEBRA_LANGUAGE/g" \
41         -e "s/__DB_NAME__/$mysqldb/g" \
42         -e "s/__DB_HOST__/$mysqlhost/g" \
43         -e "s/__DB_USER__/$mysqluser/g" \
44         -e "s/__DB_PASS__/$mysqlpwd/g" \
45         -e "s/__UNIXUSER__/$username/g" \
46         -e "s/__UNIXGROUP__/$username/g" \
47         "/etc/koha/$1" > "$2"
48 }
49
50 getmysqlhost() {
51     awk '
52         /^\[/ { inclient = 0 }
53         /^\[client\]/ { inclient = 1 }
54         inclient && /^ *host *=/ { print $3 }' \
55         /etc/mysql/koha-common.cnf
56 }
57
58 getinstancemysqlpassword() {
59     sed -n '/<pass>/s:.*>\(.*\)</pass>.*:\1:p' \
60         "/etc/koha/sites/$1/koha-conf.xml"
61 }
62
63
64 # Set defaults and read config file, if it exists.
65 DOMAIN=""
66 INTRAPORT="8080"
67 INTRAPREFIX=""
68 INTRASUFFIX=""
69 DEFAULTSQL=""
70 ZEBRA_MARC_FORMAT="marc21"
71 ZEBRA_LANGUAGE="en"
72 if [ -e /etc/koha/koha-sites.conf ]
73 then
74     . /etc/koha/koha-sites.conf
75 fi
76
77
78 # Parse command line.
79 [ "$#" = 2 ] || 
80     die "Usage: $0 [--create-db|--request-db|--populate-db] instancename"
81 case "$1" in
82   --create-db) op=create ;;
83   --request-db) op=request ;;
84   --populate-db) op=populate ;;
85   *) die "Usage: $0 [--create-db|--request-db|--populate-db] instancename" ;;
86 esac
87
88 name="$2"
89 domain="$name$DOMAIN"
90 if [ "$INTRAPORT" = 80 ] || [ "$INTRAPORT" = "" ]
91 then
92     intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
93 else
94     intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN:$INTRAPORT"
95 fi
96
97
98 mysqldb="koha_$name"
99 mysqlhost="$(getmysqlhost)"
100 mysqluser="koha_$name"
101
102 if [ "$op" = create ] || [ "$op" = request ]
103 then
104     mysqlpwd="$(pwgen -1)"
105 else
106     mysqlpwd="$(getinstancemysqlpassword $name)"
107 fi
108
109
110 if [ "$op" = create ] || [ "$op" = request ]
111 then
112     # Create new user and group.
113     username="$name-koha"
114     if getent passwd "$username" > /dev/null
115     then
116         die "User $username already exists."
117     fi
118     if getent group "$username" > /dev/null
119     then
120         die "Group $username already exists."
121     fi
122     adduser --no-create-home --disabled-login \
123         --gecos "Koha instance $username" \
124         --home "/var/lib/koha/$name" \
125         --quiet "$username"
126
127     # Create the site-specific directories.
128     koha-create-dirs "$name"
129
130     # Generate Zebra database password.
131     zebrapwd="$(pwgen -1)"
132     # Set up MySQL database for this instance.
133     if [ "$op" = create ]
134     then
135         mysql --defaults-extra-file=/etc/mysql/koha-common.cnf <<eof
136 CREATE DATABASE \`$mysqldb\`;
137 CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
138 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
139 FLUSH PRIVILEGES;
140 eof
141     fi
142
143     # Generate and install Apache site-available file and log dir.
144     generate_config_file apache-site.conf.in \
145         "/etc/apache2/sites-available/$name"
146     mkdir "/var/log/koha/$name"
147     chown "$username:$username" "/var/log/koha/$name"
148
149
150     # Generate and install main Koha config file.
151     generate_config_file koha-conf-site.xml.in \
152         "/etc/koha/sites/$name/koha-conf.xml"
153
154     # Generate and install Zebra config files.
155     generate_config_file zebra-biblios-site.cfg.in \
156         "/etc/koha/sites/$name/zebra-biblios.cfg"
157     generate_config_file zebra-authorities-site.cfg.in \
158         "/etc/koha/sites/$name/zebra-authorities.cfg"
159     generate_config_file zebra-authorities-dom-site.cfg.in \
160         "/etc/koha/sites/$name/zebra-authorities-dom.cfg"
161     generate_config_file zebra.passwd.in \
162         "/etc/koha/sites/$name/zebra.passwd"
163
164
165     # Create a GPG-encrypted file for requesting a DB to be set up.
166     if [ "$op" = request ]
167     then
168         touch "$name-db-request.txt"
169         chmod 0600 "$name-db-request.txt"
170         cat > "$name-db-request.txt" << eof
171 Please create a MySQL database and user on $mysqlhost as follows:
172
173 database name: $mysqldb
174 database user: $mysqluser
175      password: $mysqlpwd
176
177 Thank you.
178 eof
179
180         echo "See $name-db-request.txt for database creation request."
181         echo "Please forward it to the right person, and then run"
182         echo "$0 --populate-db $name"
183         echo "Thanks."
184     fi
185 fi
186
187
188 if [ "$op" = create ] || [ "$op" = populate ]
189 then
190     # Use the default database content if that exists.
191     if [ -e "$DEFAULTSQL" ]
192     then
193         # Populate the database with default content.
194         zcat "$DEFAULTSQL" |
195         sed "s/__KOHASITE__/$name/g" |
196         mysql --host="$mysqlhost" --user="$mysqluser" --password="$mysqlpwd"
197
198
199         # Change the default user's password.
200         staffpass="$(pwgen -1)"
201         staffdigest=$(echo -n "$staffpass" |
202                       perl -e '
203                             use Digest::MD5 qw(md5_base64); 
204                             while (<>) { print md5_base64($_), "\n"; }')
205         mysql --host="$mysqlhost" --user="$mysqluser" \
206 --password="$mysqlpwd" <<eof
207 USE \`$mysqldb\`;
208 UPDATE borrowers 
209 SET password = '$staffdigest' 
210 WHERE borrowernumber = 3;
211 eof
212
213         echo "staff user password is '$staffpass' but keep that secret"
214
215         # Upgrade the database schema, just in case the dump was from an 
216         # old version.
217         koha-upgrade-schema "$name"
218     else
219         echo "Koha instance is empty, no staff user created."
220     fi
221 fi
222
223
224 if [ "$op" = create ] || [ "$op" = populate ]
225 then
226     # Reconfigure Apache.
227     a2ensite "$name"
228     service apache2 restart
229
230     # Start Zebra.
231     koha-start-zebra "$name"
232 fi
233
234
235 if [ "$op" = request ]
236 then
237     koha-disable "$name"
238 fi
239
240 echo <<eoh
241
242 Email for this instance is disabled. When you're ready to enable it, use:
243 koha-email-enable $name
244 eoh