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