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