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