Bug 10572: Add phone to message_transport_types table for new installs
[koha.git] / debian / scripts / koha-rebuild-zebra
1 #!/bin/sh
2 #
3 # koha-rebuild-zebra - Rebuild the Zebra database for Koha instances.
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
20 set -e
21
22 die()
23 {
24     echo "$@" 1>&2
25     exit 1
26 }
27
28 warn()
29 {
30     echo "$@" 1>&2
31 }
32
33 is_instance()
34 {
35     local instancename=$1
36
37     if find /etc/koha/sites -mindepth 1 -maxdepth 1 \
38                          -type d -printf '%f\n'\
39           | grep -q -x $instancename ; then
40         return 0
41     else
42         return 1
43     fi
44 }
45
46 toggle_biblios_only()
47 {
48     biblios_only="yes"
49     biblios="yes"
50     if [ "$authorities_only" != "yes" ]; then
51         authorities="no"
52     fi
53 }
54
55 toggle_authorities_only()
56 {
57     authorities_only="yes"
58     authorities="yes"
59     if [ "$biblios_only" != "yes" ]; then
60         biblios="no"
61     fi
62 }
63
64 run_rebuild_zebra()
65 {
66     local instancename=$1; shift
67
68     # TODO: This comment is here to remind us that we should make
69     # rebuild_zebra.pl return error codes on failure
70     if sudo -u "$instancename-koha" -H \
71         env PERL5LIB=/usr/share/koha/lib \
72         KOHA_CONF="/etc/koha/sites/$instancename/koha-conf.xml" \
73         /usr/share/koha/bin/migration_tools/rebuild_zebra.pl $@ ; then
74         return 0
75     else
76         return 1
77     fi
78 }
79
80 usage()
81 {
82     local scriptname=$0
83     cat <<EOF
84 Rebuild the Zebra indexes for Koha instances. The default behaviour
85 is to do an incremental rebuild.
86
87 Usage: $scriptname [options] instancename1 instancename2...
88 Options:
89     --usmarc|-u       Runs the process as USMARC rather than
90                       the default of MARCXML.
91     --authorities|-a  Only run process for authorities.
92     --biblios|-b      Only run process for biblios.
93     --full|-f         Does a reindex of the whole collection.
94     --quiet|-q        Sometimes be a bit quieter for scripts/cronjobs.
95     --verbose|-v      Be verbose.
96     --help|-h         Print this help.
97
98
99 Note: Any other options are passed directly to rebuild_zebra.pl.
100 EOF
101 }
102
103 # Default parameters
104 opt_idx="-z"
105 opt_xml="-x"
106 opt_verbose=""
107 opts_other=""
108 biblios_only="no"
109 authorities_only="no"
110 biblios="yes"
111 authorities="yes"
112 # The '-q' option is intended to prevent the cronjob causing this to output
113 # help information if there are no instances defined.
114 quiet="no"
115
116 # Read parameters
117 while [ -n "$*" ]; do
118     case "$1" in
119         -h|--help)
120             usage ; exit 0
121             ;;
122         -b|--biblios)
123             toggle_biblios_only
124             ;;
125         -a|--authorities)
126             toggle_authorities_only
127             ;;
128         -u|--usmarc)
129             opt_xml=""
130             ;;
131         -f|--full)
132             opt_idx="-r"
133             ;;
134         -v|--verbose)
135             opt_verbose="-v"
136             ;;
137         -q|--quiet)
138             quiet="yes"
139             ;;
140         -*)
141             opts_other="$opts_other $1";
142             ;;
143         *)
144             break
145             ;;
146     esac
147
148     shift
149 done
150
151 # Parse command line.
152 if [ $# -lt 1 ]; then
153     if [ "$quiet" = "no" ]; then
154         usage
155         die "Missing instance name."
156     else
157         exit
158     fi
159 fi
160
161 # Loop over instance names
162 for name in "$@"
163 do
164     if is_instance $name; then
165         if [ "$biblios" = "yes" ]; then
166             if ! run_rebuild_zebra $name \
167                 -b $opt_verbose $opt_idx $opt_xml $opts_other; then
168                 warn "Something went wrong rebuilding biblio indexes for $name"
169             fi
170         fi
171         if [ "$authorities" = "yes" ]; then
172             if ! run_rebuild_zebra $name \
173                 -a $opt_verbose $opt_idx $opts_other ; then
174                 warn "Something went wrong rebuilding authority indexes for $name"
175             fi
176         fi
177     else
178         warn "Unknown instance $name."
179     fi
180 done
181
182 exit 0