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