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