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