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