Merge remote-tracking branch 'origin/new/bug_7729'
[koha.git] / install_misc / install_koha_on_fresh_debian
1 # $Id:$ vim: fdm=marker
2
3 # Licensed under the GPL
4 # Copyright 2008 Biblibre.com
5 # Koha library project  www.koha-community.org
6 #
7 # this script follow all the installtion procedure described in INSTALL.Debian
8 # with some additions to use lenny packages.
9 #
10 # This file is part of Koha.
11 #
12 # Koha is free software; you can redistribute it and/or modify it under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 2 of the License, or (at your option) any later
15 # version.
16 #
17 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License along
22 # with Koha; if not, write to the Free Software Foundation, Inc.,
23 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #
25 # TODO:
26 # - search and destroy all TODO in this file
27 # - detect country or locale to choose lang and marc by default for koha install
28 #   (fr, unimarc) 
29 # - detect country or locale to choose translator
30
31 # abort if anything wrong
32 set -e
33
34 : ${DB_TYPE:=mysql}
35 : ${DB_HOST:=localhost}
36 : ${DB_NAME:=koha}    
37 : ${DB_USER:=kohaadmin}
38 : ${DB_PASS:=katikoan}
39
40 : ${INSTALL_ZEBRA:=yes}
41 : ${INSTALL_SRU:=yes}
42 : ${INSTALL_PAZPAR2:=no}
43 : ${AUTH_INDEX_MODE:=grs1}
44 : ${BIB_INDEX_MODE:=grs1}
45
46 : ${INSTALL_MODE:=standard}
47
48 : ${ZEBRA_MARC_FORMAT:=marc21}
49 : ${ZEBRA_LANGUAGE:=en}
50 : ${ZEBRA_USER:=kohauser}
51 : ${ZEBRA_PASS:=zebrastripes}
52 : ${ZEBRA_SRU_HOST:=localhost}
53 : ${ZEBRA_SRU_BIBLIOS_PORT:=9998}
54 : ${ZEBRA_SRU_AUTHORITIES_PORT:=9999}
55
56 : ${KOHA_USER:=koha}
57 : ${KOHA_GROUP:=koha}
58
59
60 : ${MERGE_SERVER_HOST:=localhost}
61 : ${MERGE_SERVER_PORT:=11001}
62
63 : ${PAZPAR2_HOST:=localhost}
64 : ${PAZPAR2_PORT:=11002}
65
66 : ${KOHA_SRC_DIR:=/usr/local/src}
67 : ${KOHA_SRC_BASE:=koha}
68 KOHA_SRC_PATH="$KOHA_SRC_DIR/$KOHA_SRC_BASE"
69 : ${GET_KOHA_SRC_METHOD:=git}
70
71 # repo that is used by configure_lenny_sources
72 # FIXME: comment this before update
73 LENNY_FLAVORS="testing main non-free contrib" 
74
75 GET_KOHA_LOG=/dev/null
76
77 # functions declarations {{{
78
79 ask_yn () {
80         local ans
81         echo
82         echo
83         echo "$*" 
84         while echo '(y/n)' && read ans; do
85                 [ x"$ans" = xn ] && return 1
86                 [ x"$ans" = xy ] && return 0
87         done
88 }
89
90 # perlish warn
91 warn () { echo "$*" >&2
92 }
93
94 # $1 is errorlevel
95 # the rest is an error message
96 die () {
97     err=$1
98     shift
99     warn "$*"
100     exit $err
101 }
102
103 add_package () {
104 # add a list of names of packages to be installed 
105 # exemple: add_package screen zsh 
106 # or     : echo screen zsh | add_package 
107         local deb 
108         if [ "$1" ]; then
109             for deb in $@; do
110                     echo $deb >&3
111             done
112         else
113             cat >&3
114         fi
115 }
116
117 open_package_list () {
118     PACKAGELIST=$(mktemp)
119     # write in fd3 to add a name of required package
120     exec 3>$PACKAGELIST
121 }
122
123 install_package_list () {
124     # close the list
125     exec 3>&-
126     # install it
127     xargs -a $PACKAGELIST aptitude install
128     # remove it
129     rm $PACKAGELIST
130 }
131
132 configure_indexdata_sources () {
133     
134     # Add indexdata packages to the apt sources
135     echo ' # Sources for yaz and idzebra
136     deb http://ftp.indexdata.dk/debian etch main
137     ' > /etc/apt/sources.list.d/indexdata.sources.list
138     
139     # install indexdata key (to make indexdata sources usable)
140     wget -O- http://ftp.indexdata.dk/debian/indexdata.asc |
141             apt-key add - || die 1 "can't get indexdata sources"
142
143 }
144
145 configure_lenny_using_apt_spy () {
146     dpkg -l apt-spy ||
147         aptitude -y install apt-spy
148     apt-spy -d lenny
149 }
150
151 # TODO:
152 # configure_lenny_using_etch_mirror () { 
153 # sed /etc/apt/sources.list > \
154 # /etc/apt/sources.list.d/testing.sources.list
155 # need to handle $@ for this 
156 # }
157
158 add_default_lenny_repro () {
159     echo "deb $LENNY_REPO $LENNY_FLAVORS" > \
160         /etc/apt/sources.list.d/testing.sources.list
161 }
162
163 ask_for_lenny_repo () {
164         echo 'type the url of the repository to use for lenny packages. Be sure that is a valid debian repository.
165         for example http://ftp.fr.debian.org/debian/'
166         read LENNY_REPO
167         add_default_lenny_repro
168 }
169
170 configure_lenny () {
171     local howto
172     if [ "$LENNY_REPO" ]; then
173         add_default_lenny_repro
174     else
175         echo '$LENNY_REPO not set, what do you want to do now'
176         select howto in \
177                 'manually set LENNY_REPO
178                 ' \
179                 'get and use apt-spy to automatically find a repository
180                 (it can take some times)
181                 ' \
182                 'let sources.list as it (still configured)' \
183                 'leave the installation' 
184         do
185                 case "$howto" in
186
187                         *manually*)
188                                 ask_for_lenny_repo
189                                 break
190                         ;;
191
192                         *automatically*)
193                                 configure_lenny_using_apt_spy
194                                 break
195                         ;;
196
197                         *configured*)
198                                 break
199                         ;;
200
201                         *leave*)
202                                 exit
203                         ;;
204                 esac
205         done
206     fi
207 }
208
209 configure_aptitude_correctly () {
210 # configure aptitude to behave as old apt tools and
211 # use etch packages by default
212 cat << CONFAPT >  /etc/apt/apt.conf.d/Apt
213 APT {
214     Default-Release "stable";
215     Cache-Limit 20000000;
216 }
217
218 Aptitude {
219     Recommends-Important false;
220     Keep-Recommends false;
221 }
222 CONFAPT
223 }
224
225
226 set_libxml_parser () {
227     # TODO: update-perl-sax-parsers --priority since lib-xml-sax-perl 0.16
228         sed -i '
229                 # when XML::LibXML::SAX::Parser header detected
230                 # move the 2 lines in the hold space
231                 /[[]XML::LibXML::SAX::Parser[]]/ {
232                         N;H;d
233                 }
234
235                 # replace end of file by hold space
236                 $ { p;x }
237         ' /etc/perl/XML/SAX/ParserDetails.ini
238 }
239
240 verify_sax_parser () {
241     local r
242     r=`perl -MXML::SAX::ParserFactory -e 'print ref XML::SAX::ParserFactory->parser'`
243     test "$r" = XML::LibXML::SAX::Parser || set_libxml_parser
244 }
245
246 create_debian_koha_user() {
247 # TODO: use $KOHA_GROUP
248 echo ----------------------
249 echo add the system user and group for koha.
250 echo your choice: $1
251 echo ----------------------
252 adduser $1
253 }
254
255 apache_add_ports () {
256         local ports ports_conf needed
257         ports_conf="${1:-/etc/apache2/ports.conf}"
258         ports=`sed -n 's/Listen[\t ]\([0-9]\+\)/\1/p' "$ports_conf"` 
259         { for needed in 80 8080; do
260                 expr "$ports" : '.*\<\('$needed'\)\>.*' > /dev/null ||
261                         echo "# Added by koha installer
262 Listen $needed
263 "
264         done } >> "$ports_conf" 
265 }
266
267 get_koha_git_clone () {
268     # /!\ be carrefull to be in the good directory
269     # /usr/local/src/ recommended
270     local base="${1:-koha}"
271
272     dpkg -l git-core ||
273         aptitude -y  install git-core git-email
274     git clone git://git.koha-community.org/koha.git "$base"
275 }
276
277 get_koha_release () {
278     wget -O- http://download.koha-community.org/koha-3.00.00.tar.gz |
279         tar xzf - 
280 }
281
282 get_koha_beta () {
283     # /!\ be carrefull to be in the good directory
284     # /usr/local/src/ recommended
285
286     local method base
287     base="$1"
288     method="${2:-beta}"
289
290     get_koha_release "$base" && return 0
291     [ $method = tar ] && return 1 
292
293     local i basename; i=2
294     while [ $i != 11 ]; do
295         basename=koha-3.00.00-beta$i
296         wget -O- http://download.koha-community.org/$basename.tar.gz |
297             tar xzf - &&
298                 mv $basename "$base" &&
299                 return 0
300         let i+=1
301     done
302
303     return 1 
304 }
305
306 get_koha_sources () {
307     # /!\ be carrefull to be in the good directory
308     # /usr/local/src/ recommended
309     local method base
310     method="${2:-git}"
311     base="$1"
312
313     case "$method" in
314         git) get_koha_git_clone $base
315         ;;
316         tar)
317                 get_koha_release "$base" ||
318                         die 1 koha not found
319         ;;
320         beta) get_koha_beta "$base" "$method" ||
321                         die 1 koha not found
322         ;;
323         *) : # TODO: error message and exit 
324         ;;
325     esac
326 }
327
328 # end of function declarations }}}
329
330 if [ -e "$KOHA_SRC_PATH" ];then
331     if ask_yn "$KOHA_SRC_PATH still exists. use it ?"; then
332             ask_yn "download the sources again ?" ||
333                 PLEASE_DONT_GET=1
334     else 
335             exit
336     fi
337 else 
338         [ -d "$KOHA_SRC_DIR" ] ||
339                 die 1 "$KOHA_SRC_DIR is not a directory"
340 fi
341
342 # parsing arguments {{{
343 # TODO: document it
344 # TODO: this won't work with real bourne shell (use expr instead)
345 if [ "$1" ]; then
346     [ "$1" = *b* ] && GET_KOHA_SRC_METHOD=beta
347     [ "$1" = *t* ] && GET_KOHA_SRC_METHOD=tar
348     [ "$1" = *c* ] && DEBIAN_STILL_CONFIGURED=yes
349 fi
350
351 # TODO: remove it ? 
352 goto_koha_path () {
353     cd $KOHA_SRC_PATH && return 0
354     cd $KOHA_SRC_DIR
355     local candidate
356     local done
357     done=no
358     for candidate in koha?*; do
359         if [ $done = no ]; then
360             mv $candidate $KOHA_SRC_BASE
361         else
362             die 1 "more than one koha candidate into $KOHA_SRC_DIR, please clean up" 
363         fi
364     done 
365     cd $KOHA_SRC_PATH && die 1 "can't cd to $KOHA_SRC_PATH"
366 }
367
368 mysql_create_base () {
369         # TODO: what if non mysql backend ? so code ! 
370         # try to create or try to find the base
371         mysqladmin -uroot create "$1" || 
372                 echo show databases   |
373                         mysql         |
374                         grep -q "^$1$"
375 }
376
377 mysql_grant_access () { 
378         local base login password
379         base="$1" login="$2" password="$3"
380 cat << GRANTACCESS | mysql 
381 grant all on $base.* to '$login'@'localhost' identified by '$password';
382 flush privileges;
383 GRANTACCESS
384 }
385
386 verify_etch_sources () {
387         local current backup
388         current=/etc/apt/sources.list 
389         backup=/etc/apt/sources.list.bak 
390         if [ ! -e $current ] || [ ! -s $current ]; then
391                 [ -e $backup ] && mv $backup $current || die 1 "can't find etch"
392         fi
393 }
394
395 # }}}
396
397 cd "$KOHA_SRC_DIR"
398 [ "$PLEASE_DONT_GET" ] ||
399         get_koha_sources "$KOHA_SRC_BASE" "$GET_KOHA_SRC_METHOD" > $GET_KOHA_LOG 2>&1  & 
400
401 [ $DEBIAN_STILL_CONFIGURED ] || {
402     configure_indexdata_sources  # to install yaz and iezebra
403     configure_lenny              # because etch lacks some packages
404     configure_aptitude_correctly # to solve dependancies and conflicts
405     verify_etch_sources
406 }
407
408 # update the system
409 aptitude update
410 aptitude upgrade
411
412 open_package_list           # now we can add packages with add_package command
413 # this would be installed by dependances but it needs to be
414 # installed first to autoconfigure perl sax parser
415 add_package libxml-libxml-perl
416 # libyaz-dev required to build Net::Z3950::ZOOM from cpan
417 # can be removed when debian package will be usefull
418 add_package libyaz-dev
419 # lot of koha dependancies
420 cat << PACKAGES | add_package
421 idzebra-2.0
422 yaz
423 libmail-sendmail-perl
424 libhtml-scrubber-perl
425 libmarc-record-perl/testing
426 libmarc-charset-perl/testing
427 libmarc-crosswalk-dublincore-perl
428 libpdf-reuse-perl
429 libpdf-reuse-barcode-perl
430 libdata-ical-perl
431 libxml-rss-perl/testing
432 libpoe-perl/testing
433 libschedule-at-perl
434 apache2
435 daemon
436 libgcrypt11
437 libgcrypt11-dev
438 gcc
439 make
440 mysql-server
441 libcgi-session-perl
442 libdate-calc-perl
443 libdate-manip-perl
444 libdatetime-format-mail-perl
445 libdatetime-format-ical-perl
446 liblingua-stem-perl
447 libdatetime-format-strptime-perl
448 libdatetime-format-w3cdtf-perl
449 libdbi-perl
450 libmysqlclient15-dev
451 libnet-ldap-perl
452 liblocale-po-perl
453 libpdf-api2-perl
454 libpoe-perl/testing
455 libtext-csv-perl/testing
456 libtext-charwidth-perl
457 libtime-duration-perl
458 libtime-format-perl
459 libunix-syslog-perl
460 libxml-dom-perl
461 libxml-dumper-perl
462 libxml-simple-perl
463 libxml-regexp-perl
464 libxml-xslt-perl/testing
465 libxml-libxslt-perl
466 libxml2-utils
467 libxslt1-dev
468 libyaml-syck-perl
469 libhtml-template-pro-perl
470 libdbd-mysql-perl/testing
471 libgraphics-magick-perl
472 liblist-moreutils-perl
473 libtext-iconv-perl/testing
474 libalgorithm-checkdigits-perl
475 libmarc-xml-perl
476 PACKAGES
477
478 # no more "path not found" message during cpan configuration
479 add_package unzip lynx ncftp ftp
480
481 # just because it ease the life of the sysop
482 add_package screen zsh less lsof strace
483
484 # every selected packages will now be installed
485 install_package_list
486 # something wrong with libyaz-dev... KISS workaround is reinstall
487 aptitude install libyaz-dev
488
489 # those perl libs have no statisfying debian packages
490 # so they are installed via cpan.
491 cpan Net::Z3950::ZOOM Biblio::EndnoteStyle
492
493 getent passwd $KOHA_USER ||
494     create_debian_koha_user $KOHA_USER ||
495     die 1 "can't create $KOHA_USER"
496
497 mysql_create_base "$DB_NAME" ||
498         die 1 "can't create or find $DB_NAME"
499 mysql_grant_access "$DB_NAME" "$DB_USER" "$DB_PASS" ||
500         die 1 "can't grant access to $DB_USER"
501
502 echo "please wait while i'm downloading koha sources"
503 wait
504 cd "$KOHA_SRC_PATH"
505
506 verify_sax_parser 
507
508 perl Makefile.PL
509 make
510 make test
511 make install
512 apache_add_ports
513
514 makefile_value () {
515     sed -n ' s/^'"$1"' = //T;p;q'  Makefile
516 }
517
518 koha_conf=`makefile_value KOHA_DEST_KOHA_CONF_DIR`
519 ln -s "$koha_conf"/koha-httpd.conf /etc/apache2/sites-available/koha
520
521 a2enmod rewrite
522 a2ensite koha
523 invoke-rc.d apache2 reload
524
525 koha_script=`makefile_value KOHA_DEST_SCRIPT_DIR`
526 ln -s "$koha_script"/koha-zebraqueue-ctl.sh  /etc/init.d/koha-zebraqueue-daemon
527 update-rc.d koha-zebraqueue-daemon defaults
528
529 # TODO:
530 # - add translator
531 # if cd misc/translator; then
532 #     ./tmpl_process3 install -s po/fr-FR... -i ../../koha-tmpl/opac-tmpl/prog/en -o ../../koha-tmpl/opac-tmpl/prog/fr-FR
533 #     cd -
534 # fi