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