Bug 10645: adding missing zebra languages to koha-create manpage
[koha.git] / debian / scripts / koha-create
1 #!/bin/bash
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 usage="Usage: $0 [--create-db|--request-db|--populate-db|--use-db] \
23     [--marcflavor marc21|normarc|unimarc] \
24     [--zebralang en|es|fr|nb|ru|uk] \
25     [--auth-idx dom|grs1] [--biblio-idx dom|grs1] \
26     [--defaultsql /path/to/some.sql] \
27     [--configfile /path/to/config] [--passwdfile /path/to/passwd] \
28     [--database database] [--adminuser n] instancename"
29
30 die() {
31     echo "$@" 1>&2
32     exit 1
33 }
34
35 # UPPER CASE VARIABLES - from configfile or default value
36 # lower case variables - generated within this script
37 generate_config_file() {
38     touch "$2"
39     chown "root:$username" "$2"
40     chmod 0640 "$2"
41     sed -e "s/__KOHASITE__/$name/g" \
42         -e "s/__OPACPORT__/$OPACPORT/g" \
43         -e "s/__INTRAPORT__/$INTRAPORT/g" \
44         -e "s/__OPACSERVER__/$opacdomain/g" \
45         -e "s/__INTRASERVER__/$intradomain/g" \
46         -e "s/__ZEBRA_PASS__/$zebrapwd/g" \
47         -e "s/__ZEBRA_MARC_FORMAT__/$ZEBRA_MARC_FORMAT/g" \
48         -e "s/__ZEBRA_LANGUAGE__/$ZEBRA_LANGUAGE/g" \
49         -e "s/__BIBLIOS_INDEXING_MODE__/$BIBLIOS_INDEXING_MODE/g" \
50         -e "s/__AUTHORITIES_INDEXING_MODE__/$AUTHORITIES_INDEXING_MODE/g" \
51         -e "s/__ZEBRA_BIBLIOS_CFG__/$ZEBRA_BIBLIOS_CFG/g" \
52         -e "s/__ZEBRA_AUTHORITIES_CFG__/$ZEBRA_AUTHORITIES_CFG/g" \
53         -e "s/__START_BIBLIOS_RETRIEVAL_INFO__/`echo $START_BIBLIOS_RETRIEVAL_INFO`/g" \
54         -e "s/__END_BIBLIOS_RETRIEVAL_INFO__/`echo $END_BIBLIOS_RETRIEVAL_INFO`/g" \
55         -e "s/__START_AUTHORITIES_RETRIEVAL_INFO__/`echo $START_AUTHORITIES_RETRIEVAL_INFO`/g" \
56         -e "s/__END_AUTHORITIES_RETRIEVAL_INFO__/`echo $END_AUTHORITIES_RETRIEVAL_INFO`/g" \
57         -e "s/__DB_NAME__/$mysqldb/g" \
58         -e "s/__DB_HOST__/$mysqlhost/g" \
59         -e "s/__DB_USER__/$mysqluser/g" \
60         -e "s/__DB_PASS__/$mysqlpwd/g" \
61         -e "s/__UNIXUSER__/$username/g" \
62         -e "s/__UNIXGROUP__/$username/g" \
63         -e "s/__PLUGINS_DIR__/\/var\/lib\/koha\/$name\/plugins/g" \
64         "/etc/koha/$1" > "$2"
65
66 }
67
68 getmysqlhost() {
69     awk '
70         /^\[/ { inclient = 0 }
71         /^\[client\]/ { inclient = 1 }
72         inclient && /^ *host *=/ { print $3 }' \
73         /etc/mysql/koha-common.cnf
74 }
75
76 getinstancemysqlpassword() {
77     xmlstarlet sel -t -v 'yazgfs/config/pass' "/etc/koha/sites/$1/koha-conf.xml"
78 }
79
80 getinstancemysqluser() {
81     xmlstarlet sel -t -v 'yazgfs/config/user' "/etc/koha/sites/$1/koha-conf.xml"
82 }
83
84 getinstancemysqldatabase() {
85     xmlstarlet sel -t -v 'yazgfs/config/database' "/etc/koha/sites/$1/koha-conf.xml"
86 }
87
88 set_biblios_indexing_mode()
89 {
90     local indexing_mode=$1
91     local marc_format=$2
92
93     case $indexing_mode in
94         "dom")
95             START_BIBLIOS_RETRIEVAL_INFO=`cat <<EOF
96     <xi:include href="\/etc\/koha\/$marc_format-retrieval-info-bib-dom.xml"\n
97                 xmlns:xi="http:\/\/www.w3.org\/2001\/XInclude">\n
98      <xi:fallback>\n
99       <retrievalinfo>
100 EOF`
101
102             END_BIBLIOS_RETRIEVAL_INFO=`cat <<EOF
103       <\/retrievalinfo>\n
104      <\/xi:fallback>\n
105     <\/xi:include>
106 EOF`
107             BIBLIOS_INDEXING_MODE="dom"
108             ZEBRA_BIBLIOS_CFG="zebra-biblios-dom.cfg"
109             ;;
110         "grs1")
111             START_BIBLIOS_RETRIEVAL_INFO="    <retrievalinfo>"
112             END_BIBLIOS_RETRIEVAL_INFO="    <\/retrievalinfo>"
113             BIBLIOS_INDEXING_MODE="grs1"
114             ZEBRA_BIBLIOS_CFG="zebra-biblios.cfg"
115             ;;
116         *)
117             die "Error: '$indexing_mode' is not a valid indexing mode for bibliographic records."
118             ;;
119     esac
120 }
121
122
123 set_authorities_indexing_mode()
124 {
125     local indexing_mode=$1
126     local marc_format=$2
127
128     case $indexing_mode in
129         "dom")
130             START_AUTHORITIES_RETRIEVAL_INFO=`cat <<EOF
131     <xi:include href="\/etc\/koha\/$marc_format-retrieval-info-auth-dom.xml"\n
132                 xmlns:xi="http:\/\/www.w3.org\/2001\/XInclude">\n
133      <xi:fallback>\n
134       <retrievalinfo>
135 EOF`
136
137             END_AUTHORITIES_RETRIEVAL_INFO=`cat <<EOF
138       <\/retrievalinfo>\n
139      <\/xi:fallback>\n
140     <\/xi:include>\n
141 EOF`
142             AUTHORITIES_INDEXING_MODE="dom"
143             ZEBRA_AUTHORITIES_CFG="zebra-authorities-dom.cfg"
144             ;;
145         "grs1")
146             START_AUTHORITIES_RETRIEVAL_INFO="    <retrievalinfo>"
147             END_AUTHORITIES_RETRIEVAL_INFO="    <\/retrievalinfo>"
148             AUTHORITIES_INDEXING_MODE="grs1"
149             ZEBRA_AUTHORITIES_CFG="zebra-authorities.cfg"
150             ;;
151         *)
152             die "Error: '$indexing_mode' is not a valid indexing mode for authority records."
153             ;;
154     esac
155 }
156
157 # Set defaults and read config file, if it exists.
158 DOMAIN=""
159 OPACPORT="80"
160 OPACPREFIX=""
161 OPACSUFFIX=""
162 INTRAPORT="8080"
163 INTRAPREFIX=""
164 INTRASUFFIX=""
165 DEFAULTSQL=""
166 ZEBRA_MARC_FORMAT="marc21"
167 ZEBRA_LANGUAGE="en"
168 ADMINUSER="1"
169 PASSWDFILE="/etc/koha/passwd"
170
171 # Indexing mode variables (default is DOM)
172 BIBLIOS_INDEXING_MODE="dom"
173 AUTHORITIES_INDEXING_MODE="dom"
174
175 START_BIBLIOS_RETRIEVAL_INFO=""
176 END_BIBLIOS_RETRIEVAL_INFO=""
177 START_AUTHORITIES_RETRIEVAL_INFO=""
178 END_AUTHORITIES_RETRIEVAL_INFO=""
179
180 if [ -e /etc/koha/koha-sites.conf ]
181 then
182     . /etc/koha/koha-sites.conf
183 fi
184
185 [ $# -ge 2 ] && [ $# -le 16 ] || die $usage
186
187 TEMP=`getopt -o crpm:l:d:f:b:a: -l create-db,request-db,populate-db,use-db,marcflavor:,auth-idx:,biblio-idx:,zebralang:,defaultsql:,configfile:,passwdfile:,database:,adminuser: \
188      -n "$0" -- "$@"`
189
190 # Note the quotes around `$TEMP': they are essential!
191 eval set -- "$TEMP"
192
193 # Temporary variables for the command line options
194 CLO_ZEBRA_MARC_FORMAT=""
195 CLO_ZEBRA_LANGUAGE=""
196 CLO_DEFAULTSQL=""
197 CLO_ADMINUSER=""
198 CLO_BIBLIOS_INDEXING_MODE=""
199 CLO_AUTHORITIES_INDEXING_MODE=""
200
201
202 while true ; do
203         case "$1" in
204                 -c|--create-db) op=create ; shift ;;
205                 -r|--request-db) op=request ; shift ;;
206                 -p|--populate-db) op=populate ; shift ;;
207         -u|--use-db) op=use ; shift ;;
208                 -m|--marcflavor) CLO_ZEBRA_MARC_FORMAT="$2" ; shift 2 ;;
209                 -l|--zebralang) CLO_ZEBRA_LANGUAGE="$2" ; shift 2 ;;
210                 --auth-idx) CLO_AUTHORITIES_INDEXING_MODE="$2" ; shift 2 ;;
211                 --biblio-idx) CLO_BIBLIOS_INDEXING_MODE="$2" ; shift 2 ;;
212                 -d|--defaultsql) CLO_DEFAULTSQL="$2" ; shift 2 ;;
213                 -f|--configfile) configfile="$2" ; shift 2 ;;
214         -s|--passwdfile) CLO_PASSWDFILE="$2" ; shift 2 ;;
215         -b|--database) CLO_DATABASE="$2" ; shift 2 ;;
216                 -a|--adminuser) CLO_ADMINUSER="$2" ; shift 2 ;;
217                 --) shift ; break ;;
218                 *) die "Internal error processing command line arguments" ;;
219         esac
220 done
221
222 # Load the configfile given on the command line
223 if [ "$configfile" != "" ]
224 then
225     if [ -e "$configfile" ]
226     then
227         . "$configfile"
228     else
229         die "$configfile does not exist.";
230     fi
231 fi
232
233 # Make sure options from the command line get the highest precedence
234 if [ "$CLO_ZEBRA_MARC_FORMAT" != "" ]
235 then
236     ZEBRA_MARC_FORMAT="$CLO_ZEBRA_MARC_FORMAT"
237 fi
238 if [ "$CLO_ZEBRA_LANGUAGE" != "" ]
239 then
240     ZEBRA_LANGUAGE="$CLO_ZEBRA_LANGUAGE"
241 fi
242 if [ "$CLO_DEFAULTSQL" != "" ]
243 then
244     DEFAULTSQL="$CLO_DEFAULTSQL"
245 fi
246 if [ "$CLO_ADMINUSER" != "" ]
247 then
248     ADMINUSER="$CLO_ADMINUSER"
249 fi
250 if [ "$CLO_PASSWDFILE" != "" ]
251 then
252     PASSWDFILE="$CLO_PASSWDFILE"
253 fi
254
255 if [ "$CLO_BIBLIOS_INDEXING_MODE" !=  "" ]; then
256     BIBLIOS_INDEXING_MODE=$CLO_BIBLIOS_INDEXING_MODE
257 fi
258
259 set_biblios_indexing_mode $BIBLIOS_INDEXING_MODE $ZEBRA_MARC_FORMAT
260
261
262 if [ "$CLO_AUTHORITIES_INDEXING_MODE" !=  "" ]; then
263     AUTHORITIES_INDEXING_MODE=$CLO_AUTHORITIES_INDEXING_MODE
264 fi
265
266 set_authorities_indexing_mode $AUTHORITIES_INDEXING_MODE $ZEBRA_MARC_FORMAT
267
268 name="$1"
269
270 opacdomain="$OPACPREFIX$name$OPACSUFFIX$DOMAIN"
271 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
272
273
274 if [ -f $PASSWDFILE ] && [ `cat $PASSWDFILE | grep "^$name:"` ]
275 then
276     passwdline=`cat $PASSWDFILE | grep "^$name:"`
277     mysqluser=`echo $passwdline | cut -d ":" -f 2`
278     mysqlpwd=`echo $passwdline | cut -d ":" -f 3`
279     mysqldb=`echo $passwdline | cut -d ":" -f 4`
280 fi
281
282 # The order of precedence for MySQL database name is:
283 # default < passwd file < command line
284 if [ "$mysqldb" = "" ]
285 then
286     mysqldb="koha_$name"
287 fi
288 if [ "$CLO_DATABASE" != "" ]
289 then
290     mysqldb="$CLO_DATABASE"
291 fi
292
293 if [ "$mysqluser" = "" ]
294 then
295     mysqluser="koha_$name"
296 fi
297 mysqlhost="$(getmysqlhost)"
298
299 if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
300 then
301     if [ "$mysqlpwd" = "" ]
302     then
303         mysqlpwd="$(pwgen -s 16 1)"
304     fi
305 else
306     mysqlpwd="$(getinstancemysqlpassword $name)"
307 fi
308
309
310 if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
311 then
312     # Create new user and group.
313     username="$name-koha"
314     if getent passwd "$username" > /dev/null
315     then
316         die "User $username already exists."
317     fi
318     if getent group "$username" > /dev/null
319     then
320         die "Group $username already exists."
321     fi
322     adduser --no-create-home --disabled-login \
323         --gecos "Koha instance $username" \
324         --home "/var/lib/koha/$name" \
325         --quiet "$username"
326
327     # Create the site-specific directories.
328     koha-create-dirs "$name"
329
330     # Generate Zebra database password.
331     zebrapwd="$(pwgen -s 16 1)"
332     # Future enhancement: make this configurable for when your db is on
333     # another server.
334     mysql_hostname="localhost"
335     # Set up MySQL database for this instance.
336     if [ "$op" = create ]
337     then
338         mysql --defaults-extra-file=/etc/mysql/koha-common.cnf <<eof
339 CREATE DATABASE \`$mysqldb\`;
340 CREATE USER \`$mysqluser\`@'$mysql_hostname' IDENTIFIED BY '$mysqlpwd';
341 CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
342 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
343 FLUSH PRIVILEGES;
344 eof
345     fi #`
346
347     if [ "$op" = use ]
348     then
349         mysql --defaults-extra-file=/etc/mysql/koha-common.cnf --force <<eof
350 CREATE USER \`$mysqluser\`@'$mysql_hostname' IDENTIFIED BY '$mysqlpwd';
351 CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
352 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
353 FLUSH PRIVILEGES;
354 eof
355     fi #`
356
357     # Generate and install Apache site-available file and log dir.
358     generate_config_file apache-site.conf.in \
359         "/etc/apache2/sites-available/$name"
360     mkdir "/var/log/koha/$name"
361     chown "$username:$username" "/var/log/koha/$name"
362
363
364     # Generate and install main Koha config file.
365     generate_config_file koha-conf-site.xml.in \
366         "/etc/koha/sites/$name/koha-conf.xml"
367
368     # Generate and install Zebra config files.
369     generate_config_file zebra-biblios-site.cfg.in \
370         "/etc/koha/sites/$name/zebra-biblios.cfg"
371     generate_config_file zebra-biblios-dom-site.cfg.in \
372         "/etc/koha/sites/$name/zebra-biblios-dom.cfg"
373     generate_config_file zebra-authorities-site.cfg.in \
374         "/etc/koha/sites/$name/zebra-authorities.cfg"
375     generate_config_file zebra-authorities-dom-site.cfg.in \
376         "/etc/koha/sites/$name/zebra-authorities-dom.cfg"
377     generate_config_file zebra.passwd.in \
378         "/etc/koha/sites/$name/zebra.passwd"
379
380
381     # Create a GPG-encrypted file for requesting a DB to be set up.
382     if [ "$op" = request ]
383     then
384         touch "$name-db-request.txt"
385         chmod 0600 "$name-db-request.txt"
386         cat > "$name-db-request.txt" << eof
387 Please create a MySQL database and user on $mysqlhost as follows:
388
389 database name: $mysqldb
390 database user: $mysqluser
391      password: $mysqlpwd
392
393 Thank you.
394 eof
395
396         echo "See $name-db-request.txt for database creation request."
397         echo "Please forward it to the right person, and then run"
398         echo "$0 --populate-db $name"
399         echo "Thanks."
400     fi
401 fi
402
403
404 if [ "$op" = create ] || [ "$op" = populate ]
405 then
406     # Re-fetch the passwords from the config we've generated, allows it
407     # to be different from what we set, in case the user had to change
408     # something.
409     mysqluser=$(getinstancemysqluser $name)
410     mysqldb=$(getinstancemysqldatabase $name)
411     # Use the default database content if that exists.
412     if [ -e "$DEFAULTSQL" ]
413     then
414         # Populate the database with default content.
415         zcat "$DEFAULTSQL" |
416         sed "s/__KOHASITE__/$name/g" |
417         mysql --host="$mysqlhost" --user="$mysqluser" --password="$mysqlpwd" "$mysqldb"
418
419
420         # Change the default user's password.
421         staffpass="$(pwgen 12 1)"
422         staffdigest=$(echo -n "$staffpass" |
423                       perl -e '
424                             use Digest::MD5 qw(md5_base64); 
425                             while (<>) { print md5_base64($_), "\n"; }')
426         mysql --host="$mysqlhost" --user="$mysqluser" \
427 --password="$mysqlpwd" <<eof
428 USE \`$mysqldb\`;
429 UPDATE borrowers 
430 SET password = '$staffdigest' 
431 WHERE borrowernumber = $ADMINUSER;
432 eof
433         #`
434         echo "staff user password is '$staffpass' but keep that secret"
435
436         # Upgrade the database schema, just in case the dump was from an 
437         # old version.
438         koha-upgrade-schema "$name"
439     else
440         echo "Koha instance is empty, no staff user created."
441     fi
442 fi
443
444
445 if [ "$op" = create ] || [ "$op" = populate ] || [ "$op" = use ]
446 then
447     # Reconfigure Apache.
448     a2ensite "$name"
449     service apache2 restart
450
451     # Start Zebra.
452     koha-start-zebra "$name"
453 fi
454
455
456 if [ "$op" = request ]
457 then
458     koha-disable "$name"
459 fi
460
461 echo <<eoh
462
463 Email for this instance is disabled. When you're ready to enable it, use:
464 koha-email-enable $name
465 eoh