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