Bug 12750: koha-create should be able to configure the Z39.50/SRU server
[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 # Read configuration variable file if it is present
20 [ -r /etc/default/koha-common ] && . /etc/default/koha-common
21
22 set -e
23
24 # include helper functions
25 if [ -f "/usr/share/koha/bin/koha-functions.sh" ]; then
26     . "/usr/share/koha/bin/koha-functions.sh"
27 else
28     echo "Error: /usr/share/koha/bin/koha-functions.sh not present." 1>&2
29     exit 1
30 fi
31
32 usage()
33 {
34     local scriptname=$0
35     cat <<EOF
36
37 Creates new Koha instances.
38
39 Usage:
40
41   $scriptname [DB usage mode] [options] instancename
42
43 DB usage mode:
44   --create-db               Create a new database on localhost. (default).
45   --request-db              Creates a instancename-db-request.txt file where
46                             you adjust your DB settings and re-run with --populate-db.
47   --populate-db             Finish the installation you started with --request-db after
48                             you adjusted the instancename-db-request.txt file.
49   --use-db                  Use this option if you already created and populated your DB.
50
51 Options:
52   --marcflavor flavor       Set the MARC flavor. Valid values are marc21 (default),
53                             normarc and unimarc.
54   --zebralang lang          Choose the primary language for Zebra indexing. Valid
55                             values are en (default), es, fr, nb, ru and uk.
56   --auth-idx idx_mode       Set the indexing mode for authority records. Valid
57                             values are dom (default) and grs1.
58   --biblio-idx idx_mode     Set the indexing mode for bibliographic records.
59                             Valid values are dom (default) and grs1.
60   --use-memcached           Set the instance to make use of memcache.
61   --memcached-servers str   Set a comma-separated list of host:port memcached servers.
62   --memcached-prefix str    Set the desired prefix for the instance memcached namespace.
63   --enable-sru              Enable the Z39.50/SRU server (default: disabled).
64   --sru-port                Specifiy a TCP port number for the Z39.50/SRU server
65                             to listen on. (default: 7090).
66   --defaultsql some.sql     Specify a default SQL file to be loaded on the DB.
67   --configfile cfg_file     Specify an alternate config file for reading default values.
68   --passwdfile passwd       Specify an alternate passwd file.
69   --database dbname         Enforce the use of the specified DB name (64 char limit)
70   --adminuser n             Explicit the admin user ID in the DB. Relevant in
71                             conjunction with --defaultsql and --populate-db.
72   --help,-h                 Show this help.
73
74 Note: the instance name cannot be longer that 11 chars.
75
76 EOF
77 }
78
79 # UPPER CASE VARIABLES - from configfile or default value
80 # lower case variables - generated within this script
81 generate_config_file() {
82     touch "$2"
83     chown "root:$username" "$2"
84     chmod 0640 "$2"
85     sed -e "s/__KOHASITE__/$name/g" \
86         -e "s/__OPACPORT__/$OPACPORT/g" \
87         -e "s/__INTRAPORT__/$INTRAPORT/g" \
88         -e "s/__OPACSERVER__/$opacdomain/g" \
89         -e "s/__INTRASERVER__/$intradomain/g" \
90         -e "s/__ZEBRA_PASS__/$zebrapwd/g" \
91         -e "s/__ZEBRA_MARC_FORMAT__/$ZEBRA_MARC_FORMAT/g" \
92         -e "s/__ZEBRA_LANGUAGE__/$ZEBRA_LANGUAGE/g" \
93         -e "s/__SRU_BIBLIOS_PORT__/$SRU_SERVER_PORT/g" \
94         -e "s/__START_SRU_PUBLICSERVER__/$START_SRU_PUBLICSERVER/g" \
95         -e "s/__END_SRU_PUBLICSERVER__/$END_SRU_PUBLICSERVER/g" \
96         -e "s/__BIBLIOS_INDEXING_MODE__/$BIBLIOS_INDEXING_MODE/g" \
97         -e "s/__AUTHORITIES_INDEXING_MODE__/$AUTHORITIES_INDEXING_MODE/g" \
98         -e "s/__ZEBRA_BIBLIOS_CFG__/$ZEBRA_BIBLIOS_CFG/g" \
99         -e "s/__ZEBRA_AUTHORITIES_CFG__/$ZEBRA_AUTHORITIES_CFG/g" \
100         -e "s/__START_BIBLIOS_RETRIEVAL_INFO__/`echo $START_BIBLIOS_RETRIEVAL_INFO`/g" \
101         -e "s/__END_BIBLIOS_RETRIEVAL_INFO__/`echo $END_BIBLIOS_RETRIEVAL_INFO`/g" \
102         -e "s/__START_AUTHORITIES_RETRIEVAL_INFO__/`echo $START_AUTHORITIES_RETRIEVAL_INFO`/g" \
103         -e "s/__END_AUTHORITIES_RETRIEVAL_INFO__/`echo $END_AUTHORITIES_RETRIEVAL_INFO`/g" \
104         -e "s/__DB_NAME__/$mysqldb/g" \
105         -e "s/__DB_HOST__/$mysqlhost/g" \
106         -e "s/__DB_USER__/$mysqluser/g" \
107         -e "s/__DB_PASS__/$mysqlpwd/g" \
108         -e "s/__UNIXUSER__/$username/g" \
109         -e "s/__UNIXGROUP__/$username/g" \
110         -e "s/__PLUGINS_DIR__/\/var\/lib\/koha\/$name\/plugins/g" \
111         -e "s/__MEMCACHED_NAMESPACE__/$MEMCACHED_NAMESPACE/g" \
112         -e "s/__MEMCACHED_SERVERS__/$MEMCACHED_SERVERS/g" \
113         "/etc/koha/$1" > "$2"
114
115 }
116
117 getmysqlhost() {
118     awk '
119         /^\[/ { inclient = 0 }
120         /^\[client\]/ { inclient = 1 }
121         inclient && /^ *host *=/ { print $3 }' \
122         /etc/mysql/koha-common.cnf
123 }
124
125 getinstancemysqlpassword() {
126     xmlstarlet sel -t -v 'yazgfs/config/pass' "/etc/koha/sites/$1/koha-conf.xml"
127 }
128
129 getinstancemysqluser() {
130     xmlstarlet sel -t -v 'yazgfs/config/user' "/etc/koha/sites/$1/koha-conf.xml"
131 }
132
133 getinstancemysqldatabase() {
134     xmlstarlet sel -t -v 'yazgfs/config/database' "/etc/koha/sites/$1/koha-conf.xml"
135 }
136
137 check_apache_config()
138 {
139
140     # Check that mpm_itk is installed and enabled
141     if ! /usr/sbin/apachectl -M 2> /dev/null | grep -q 'mpm_itk'; then
142         # Check Apache version
143         APACHE_DISABLE_MPM_MSG=""
144         if /usr/sbin/apache2ctl -v | grep -q "Server version: Apache/2.4"; then
145             # mpm_event or mpm_worker need to be disabled first. mpm_itk depends
146             # on mpm_prefork, which is enabled if needed. See
147             # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=734865
148             if /usr/sbin/apachectl -M 2> /dev/null | grep -q 'mpm_event'; then
149                 APACHE_DISABLE_MPM_MSG=" sudo a2dismod mpm_event ;"
150             elif /usr/sbin/apachectl -M 2> /dev/null | grep -q 'mpm_worker'; then
151                 APACHE_DISABLE_MPM_MSG=" sudo a2dismod mpm_worker ;"
152             # else mpm_prefork: a2enmod mpm_itk works
153             fi
154         # else Apache 2.2: a2enmod mpm_itk works
155         fi
156
157         cat 1>&2  <<EOM
158
159 Koha requires mpm_itk to be enabled within Apache in order to run.
160 Typically this can be enabled with:
161
162    $APACHE_DISABLE_MPM_MSG sudo a2enmod mpm_itk
163 EOM
164
165         die
166     fi
167
168     # Check that mod_rewrite is installed and enabled.
169     if ! /usr/sbin/apachectl -M 2> /dev/null | grep -q 'rewrite_module'; then
170         cat 1>&2  <<EOM
171
172 Koha requires mod_rewrite to be enabled within Apache in order to run.
173 Typically this can be enabled with:
174
175     sudo a2enmod rewrite
176 EOM
177         die
178     fi
179
180     # Check that the CGI module is installed and enabled
181     # (Apache 2.4 may not have it by default.)
182     if ! /usr/sbin/apachectl -M 2> /dev/null | grep -q 'cgi_module'; then
183         cat 1>&2 << EOM
184 Koha requires mod_cgi to be enabled within Apache in order to run.
185 Typically this can be enabled with:
186
187     sudo a2enmod cgi
188 EOM
189         die
190     fi
191
192 }
193
194 set_biblios_indexing_mode()
195 {
196     local indexing_mode=$1
197     local marc_format=$2
198
199     case $indexing_mode in
200         "dom")
201             START_BIBLIOS_RETRIEVAL_INFO=`cat <<EOF
202     <xi:include href="\/etc\/koha\/$marc_format-retrieval-info-bib-dom.xml"\n
203                 xmlns:xi="http:\/\/www.w3.org\/2001\/XInclude">\n
204      <xi:fallback>\n
205       <retrievalinfo>
206 EOF`
207
208             END_BIBLIOS_RETRIEVAL_INFO=`cat <<EOF
209       <\/retrievalinfo>\n
210      <\/xi:fallback>\n
211     <\/xi:include>
212 EOF`
213             BIBLIOS_INDEXING_MODE="dom"
214             ZEBRA_BIBLIOS_CFG="zebra-biblios-dom.cfg"
215             ;;
216         "grs1")
217             START_BIBLIOS_RETRIEVAL_INFO="    <retrievalinfo>"
218             END_BIBLIOS_RETRIEVAL_INFO="    <\/retrievalinfo>"
219             BIBLIOS_INDEXING_MODE="grs1"
220             ZEBRA_BIBLIOS_CFG="zebra-biblios.cfg"
221             ;;
222         *)
223             die "Error: '$indexing_mode' is not a valid indexing mode for bibliographic records."
224             ;;
225     esac
226 }
227
228
229 set_authorities_indexing_mode()
230 {
231     local indexing_mode=$1
232     local marc_format=$2
233
234     case $indexing_mode in
235         "dom")
236             START_AUTHORITIES_RETRIEVAL_INFO=`cat <<EOF
237     <xi:include href="\/etc\/koha\/$marc_format-retrieval-info-auth-dom.xml"\n
238                 xmlns:xi="http:\/\/www.w3.org\/2001\/XInclude">\n
239      <xi:fallback>\n
240       <retrievalinfo>
241 EOF`
242
243             END_AUTHORITIES_RETRIEVAL_INFO=`cat <<EOF
244       <\/retrievalinfo>\n
245      <\/xi:fallback>\n
246     <\/xi:include>\n
247 EOF`
248             AUTHORITIES_INDEXING_MODE="dom"
249             ZEBRA_AUTHORITIES_CFG="zebra-authorities-dom.cfg"
250             ;;
251         "grs1")
252             START_AUTHORITIES_RETRIEVAL_INFO="    <retrievalinfo>"
253             END_AUTHORITIES_RETRIEVAL_INFO="    <\/retrievalinfo>"
254             AUTHORITIES_INDEXING_MODE="grs1"
255             ZEBRA_AUTHORITIES_CFG="zebra-authorities.cfg"
256             ;;
257         *)
258             die "Error: '$indexing_mode' is not a valid indexing mode for authority records."
259             ;;
260     esac
261 }
262
263
264 set_memcached()
265 {
266     local instance="$1"
267
268     if [ "$CLO_MEMCACHED_SERVERS" != "" ]; then
269         MEMCACHED_SERVERS=$CLO_MEMCACHED_SERVERS
270     else
271         if [ "$MEMCACHED_SERVERS" = "" ]; then
272             MEMCACHED_SERVERS=$DEFAULT_MEMCACHED_SERVERS
273         # else: was set by the koha-sites.conf file
274         fi
275     fi
276
277     if [ "$CLO_MEMCACHED_PREFIX" != "" ]; then
278         MEMCACHED_NAMESPACE="$CLO_MEMCACHED_PREFIX$instance"
279     else
280         if [ "$MEMCACHED_PREFIX" != "" ]; then
281             MEMCACHED_NAMESPACE="$MEMCACHED_PREFIX$instance"
282         else
283             MEMCACHED_NAMESPACE="$DEFAULT_MEMCACHED_PREFIX$instance"
284         fi
285     fi
286
287 }
288
289 enable_sru_server()
290 {
291     # remove the commenting symbols
292     START_SRU_PUBLICSERVER=""
293     END_SRU_PUBLICSERVER=""
294     if [ "$SRU_SERVER_PORT" = "" ]; then
295         # --sru-port not passed, use the default
296         SRU_SERVER_PORT=$DEFAULT_SRU_SERVER_PORT
297     fi
298 }
299
300 # Set defaults and read config file, if it exists.
301 DOMAIN=""
302 OPACPORT="80"
303 OPACPREFIX=""
304 OPACSUFFIX=""
305 INTRAPORT="8080"
306 INTRAPREFIX=""
307 INTRASUFFIX=""
308 DEFAULTSQL=""
309 ZEBRA_MARC_FORMAT="marc21"
310 ZEBRA_LANGUAGE="en"
311 ADMINUSER="1"
312 PASSWDFILE="/etc/koha/passwd"
313
314 # memcached variables
315 USE_MEMCACHED="no"
316 MEMCACHED_SERVERS=""
317 MEMCACHED_PREFIX=""
318 # hardcoded memcached defaults
319 DEFAULT_MEMCACHED_SERVERS="127.0.0.1:11211"
320 DEFAULT_MEMCACHED_PREFIX="koha_"
321
322 # SRU server variables
323 ENABLE_SRU="no"
324 SRU_SERVER_PORT=""
325 # hardcoded default SRU server port
326 DEFAULT_SRU_SERVER_PORT="7090"
327 START_SRU_PUBLICSERVER="<!--"
328 END_SRU_PUBLICSERVER="-->"
329
330 # Indexing mode variables (default is DOM)
331 BIBLIOS_INDEXING_MODE="dom"
332 AUTHORITIES_INDEXING_MODE="dom"
333
334 START_BIBLIOS_RETRIEVAL_INFO=""
335 END_BIBLIOS_RETRIEVAL_INFO=""
336 START_AUTHORITIES_RETRIEVAL_INFO=""
337 END_AUTHORITIES_RETRIEVAL_INFO=""
338
339 if [ -e /etc/koha/koha-sites.conf ]
340 then
341     . /etc/koha/koha-sites.conf
342 fi
343
344 [ $# -ge 1 ] && [ $# -le 16 ] || ( usage ; die "Error: wrong parameters" )
345
346 TEMP=`getopt -o chrpm:l:d:f:b:a: -l create-db,request-db,populate-db,use-db,use-memcached,enable-sru,sru-port:,help,marcflavor:,auth-idx:,biblio-idx:,zebralang:,defaultsql:,configfile:,passwdfile:,database:,adminuser:,memcached-servers:,memcached-prefix:, \
347      -n "$0" -- "$@"`
348
349 # Note the quotes around `$TEMP': they are essential!
350 eval set -- "$TEMP"
351
352 # Temporary variables for the command line options
353 CLO_ZEBRA_MARC_FORMAT=""
354 CLO_ZEBRA_LANGUAGE=""
355 CLO_DEFAULTSQL=""
356 CLO_ADMINUSER=""
357 CLO_BIBLIOS_INDEXING_MODE=""
358 CLO_AUTHORITIES_INDEXING_MODE=""
359 CLO_MEMCACHED_SERVERS=""
360 CLO_MEMCACHED_PREFIX=""
361
362
363 while true ; do
364     case "$1" in
365         -c|--create-db)
366             op=create ; shift ;;
367         -r|--request-db)
368             op=request ; shift ;;
369         -p|--populate-db)
370             op=populate ; shift ;;
371         -u|--use-db)
372             op=use ; shift ;;
373         --use-memcached)
374             USE_MEMCACHED="yes" ; shift ;;
375         --memcached-servers)
376             CLO_MEMCACHED_SERVERS="$2" ; shift 2 ;;
377         --memcached-prefix)
378             CLO_MEMCACHED_PREFIX="$2" ; shift 2;;
379         -m|--marcflavor)
380             CLO_ZEBRA_MARC_FORMAT="$2" ; shift 2 ;;
381         -l|--zebralang)
382             CLO_ZEBRA_LANGUAGE="$2" ; shift 2 ;;
383         --auth-idx)
384             CLO_AUTHORITIES_INDEXING_MODE="$2" ; shift 2 ;;
385         --biblio-idx)
386             CLO_BIBLIOS_INDEXING_MODE="$2" ; shift 2 ;;
387         -d|--defaultsql)
388             CLO_DEFAULTSQL="$2" ; shift 2 ;;
389         -f|--configfile)
390             configfile="$2" ; shift 2 ;;
391         -s|--passwdfile)
392             CLO_PASSWDFILE="$2" ; shift 2 ;;
393         -b|--database)
394             CLO_DATABASE="$2" ; shift 2 ;;
395         -a|--adminuser)
396             CLO_ADMINUSER="$2" ; shift 2 ;;
397         --enable-sru)
398             ENABLE_SRU="yes" ; shift ;;
399         --sru-port)
400             SRU_SERVER_PORT="$2" ; shift 2 ;;
401         -h|--help)
402             usage ; exit 0 ;;
403         --)
404             shift ; break ;;
405         *)
406             die "Internal error processing command line arguments" ;;
407     esac
408 done
409
410 # Load the configfile given on the command line
411 if [ "$configfile" != "" ]
412 then
413     if [ -e "$configfile" ]
414     then
415         . "$configfile"
416     else
417         die "$configfile does not exist.";
418     fi
419 fi
420
421 # Make sure options from the command line get the highest precedence
422 if [ "$CLO_ZEBRA_MARC_FORMAT" != "" ]
423 then
424     ZEBRA_MARC_FORMAT="$CLO_ZEBRA_MARC_FORMAT"
425 fi
426 if [ "$CLO_ZEBRA_LANGUAGE" != "" ]
427 then
428     ZEBRA_LANGUAGE="$CLO_ZEBRA_LANGUAGE"
429 fi
430 if [ "$CLO_DEFAULTSQL" != "" ]
431 then
432     DEFAULTSQL="$CLO_DEFAULTSQL"
433 fi
434 if [ "$CLO_ADMINUSER" != "" ]
435 then
436     ADMINUSER="$CLO_ADMINUSER"
437 fi
438 if [ "$CLO_PASSWDFILE" != "" ]
439 then
440     PASSWDFILE="$CLO_PASSWDFILE"
441 fi
442
443 if [ "$CLO_BIBLIOS_INDEXING_MODE" !=  "" ]; then
444     BIBLIOS_INDEXING_MODE=$CLO_BIBLIOS_INDEXING_MODE
445 fi
446
447 set_biblios_indexing_mode $BIBLIOS_INDEXING_MODE $ZEBRA_MARC_FORMAT
448
449 if [ "$ENABLE_SRU" != "no" ]; then
450     enable_sru_server
451 fi
452
453 if [ "$CLO_AUTHORITIES_INDEXING_MODE" !=  "" ]; then
454     AUTHORITIES_INDEXING_MODE=$CLO_AUTHORITIES_INDEXING_MODE
455 fi
456
457 set_authorities_indexing_mode $AUTHORITIES_INDEXING_MODE $ZEBRA_MARC_FORMAT
458
459 name="$1"
460
461 if [ "$USE_MEMCACHED" = "yes" ]; then
462     set_memcached $name
463 elif [ "$CLO_MEMCACHED_SERVERS" != "" ] || \
464      [ "$CLO_MEMCACHED_PREFIX" != "" ]; then
465
466     MSG=`cat <<EOF
467
468 Error: you provided memcached configuration switches but memcached is not enabled.
469 Please set USE_MEMCACHED="yes" on /etc/koha/koha-sites.conf or use the
470 --use-memcached option switch to enable it.
471
472 EOF`
473
474     usage ; die $MSG
475 else
476     # Unset memcached-related variables
477     MEMCACHED_SERVERS=""
478     MEMCACHED_PREFIX=""
479 fi
480
481 # Are we root? If not, the mod_rewrite check will fail and be confusing, so
482 # we look into this first.
483 if [[ $UID -ne 0 ]]
484 then
485     die "This script must be run with root privileges."
486 fi
487
488 # Check everything is ok with Apache, die otherwise
489 check_apache_config
490
491 opacdomain="$OPACPREFIX$name$OPACSUFFIX$DOMAIN"
492 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
493
494
495 if [ -f $PASSWDFILE ] && [ `cat $PASSWDFILE | grep "^$name:"` ]
496 then
497     passwdline=`cat $PASSWDFILE | grep "^$name:"`
498     mysqluser=`echo $passwdline | cut -d ":" -f 2`
499     mysqlpwd=`echo $passwdline | cut -d ":" -f 3`
500     mysqldb=`echo $passwdline | cut -d ":" -f 4`
501 fi
502
503 # The order of precedence for MySQL database name is:
504 # default < passwd file < command line
505 if [ "$mysqldb" = "" ]
506 then
507     mysqldb="koha_$name"
508 fi
509 if [ "$CLO_DATABASE" != "" ]
510 then
511     mysqldb="$CLO_DATABASE"
512 fi
513
514 if [ "$mysqluser" = "" ]
515 then
516     mysqluser="koha_$name"
517 fi
518 mysqlhost="$(getmysqlhost)"
519
520 if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
521 then
522     if [ "$mysqlpwd" = "" ]
523     then
524         mysqlpwd="$(pwgen -s 16 1)"
525     fi
526 else
527     mysqlpwd="$(getinstancemysqlpassword $name)"
528 fi
529
530
531 if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
532 then
533     # Create new user and group.
534     username="$name-koha"
535     if getent passwd "$username" > /dev/null
536     then
537         die "User $username already exists."
538     fi
539     if getent group "$username" > /dev/null
540     then
541         die "Group $username already exists."
542     fi
543     adduser --no-create-home --disabled-login \
544         --gecos "Koha instance $username" \
545         --home "/var/lib/koha/$name" \
546         --quiet "$username"
547
548     # Create the site-specific directories.
549     koha-create-dirs "$name"
550
551     # Generate Zebra database password.
552     zebrapwd="$(pwgen -s 16 1)"
553     # Future enhancement: make this configurable for when your db is on
554     # another server.
555     mysql_hostname="localhost"
556     # Set up MySQL database for this instance.
557     if [ "$op" = create ]
558     then
559         mysql --defaults-extra-file=/etc/mysql/koha-common.cnf <<eof
560 CREATE DATABASE \`$mysqldb\`;
561 CREATE USER \`$mysqluser\`@'$mysql_hostname' IDENTIFIED BY '$mysqlpwd';
562 CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
563 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
564 FLUSH PRIVILEGES;
565 eof
566     fi #`
567
568     if [ "$op" = use ]
569     then
570         mysql --defaults-extra-file=/etc/mysql/koha-common.cnf --force <<eof
571 CREATE USER \`$mysqluser\`@'$mysql_hostname' IDENTIFIED BY '$mysqlpwd';
572 CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
573 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
574 FLUSH PRIVILEGES;
575 eof
576     fi #`
577
578     # Generate and install Apache site-available file and log dir.
579     generate_config_file apache-site.conf.in \
580         "/etc/apache2/sites-available/$name.conf"
581     mkdir "/var/log/koha/$name"
582     chown "$username:$username" "/var/log/koha/$name"
583
584
585     # Generate and install main Koha config file.
586     generate_config_file koha-conf-site.xml.in \
587         "/etc/koha/sites/$name/koha-conf.xml"
588
589     # Generate and install Zebra config files.
590     generate_config_file zebra-biblios-site.cfg.in \
591         "/etc/koha/sites/$name/zebra-biblios.cfg"
592     generate_config_file zebra-biblios-dom-site.cfg.in \
593         "/etc/koha/sites/$name/zebra-biblios-dom.cfg"
594     generate_config_file zebra-authorities-site.cfg.in \
595         "/etc/koha/sites/$name/zebra-authorities.cfg"
596     generate_config_file zebra-authorities-dom-site.cfg.in \
597         "/etc/koha/sites/$name/zebra-authorities-dom.cfg"
598     generate_config_file zebra.passwd.in \
599         "/etc/koha/sites/$name/zebra.passwd"
600
601
602     # Create a GPG-encrypted file for requesting a DB to be set up.
603     if [ "$op" = request ]
604     then
605         touch "$name-db-request.txt"
606         chmod 0600 "$name-db-request.txt"
607         cat > "$name-db-request.txt" << eof
608 Please create a MySQL database and user on $mysqlhost as follows:
609
610 database name: $mysqldb
611 database user: $mysqluser
612      password: $mysqlpwd
613
614 Thank you.
615 eof
616
617         echo "See $name-db-request.txt for database creation request."
618         echo "Please forward it to the right person, and then run"
619         echo "$0 --populate-db $name"
620         echo "Thanks."
621     fi
622 fi
623
624
625 if [ "$op" = create ] || [ "$op" = populate ]
626 then
627     # Re-fetch the passwords from the config we've generated, allows it
628     # to be different from what we set, in case the user had to change
629     # something.
630     mysqluser=$(getinstancemysqluser $name)
631     mysqldb=$(getinstancemysqldatabase $name)
632     # Use the default database content if that exists.
633     if [ -e "$DEFAULTSQL" ]
634     then
635         # Populate the database with default content.
636         zcat "$DEFAULTSQL" |
637         sed "s/__KOHASITE__/$name/g" |
638         mysql --host="$mysqlhost" --user="$mysqluser" --password="$mysqlpwd" "$mysqldb"
639
640
641         # Change the default user's password.
642         staffpass="$(pwgen 12 1)"
643         staffdigest=$(echo -n "$staffpass" |
644                       perl -e '
645                             use Digest::MD5 qw(md5_base64); 
646                             while (<>) { print md5_base64($_), "\n"; }')
647         mysql --host="$mysqlhost" --user="$mysqluser" \
648 --password="$mysqlpwd" <<eof
649 USE \`$mysqldb\`;
650 UPDATE borrowers 
651 SET password = '$staffdigest' 
652 WHERE borrowernumber = $ADMINUSER;
653 eof
654         #`
655         echo "staff user password is '$staffpass' but keep that secret"
656
657         # Upgrade the database schema, just in case the dump was from an 
658         # old version.
659         koha-upgrade-schema "$name"
660     else
661         echo "Koha instance is empty, no staff user created."
662     fi
663 fi
664
665
666 if [ "$op" = create ] || [ "$op" = populate ] || [ "$op" = use ]
667 then
668     # Reconfigure Apache.
669     if ! {
670         a2ensite "$name" > /dev/null 2>&1 ||
671             a2ensite "${name}.conf" > /dev/null 2>&1
672     }; then
673         echo "Warning: problem enabling $name in Apache" >&2
674     fi
675     service apache2 restart
676
677     # Start Zebra.
678     koha-start-zebra "$name"
679
680     if [ "$USE_INDEXER_DAEMON" = "yes" ]; then
681         # Start Indexer daemon
682         koha-indexer --start "$name"
683     fi
684 fi
685
686
687 if [ "$op" = request ]
688 then
689     koha-disable "$name"
690 fi
691
692 echo <<eoh
693
694 Email for this instance is disabled. When you're ready to enable it, use:
695 koha-email-enable $name
696 eoh