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