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