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