Bug 14533: Make --use-db display a message if no DB defined
[koha.git] / debian / scripts / koha-sitemap
1 #!/bin/bash
2 #
3 # Copyright 2016 Theke Solutions
4 #
5 # This file is part of Koha.
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 set -e
21
22 . /lib/lsb/init-functions
23
24 # Read configuration variable file if it is present
25 [ -r /etc/default/koha-common ] && . /etc/default/koha-common
26
27 # include helper functions
28 if [ -f "/usr/share/koha/bin/koha-functions.sh" ]; then
29     . "/usr/share/koha/bin/koha-functions.sh"
30 else
31     echo "Error: /usr/share/koha/bin/koha-functions.sh not present." 1>&2
32     exit 1
33 fi
34
35 usage()
36 {
37     local scriptname=$(basename $0)
38
39     cat <<EOF
40 $scriptname
41
42 This script lets you manage sitemaps for your Koha instances.
43
44 Usage:
45 $scriptname --enable|--disable instancename1 [instancename2]
46 $scriptname --generate instancename1 [instancename2]
47 $scriptname -h|--help
48
49     --enable              Enable sitemap generation for the specified instances
50     --disable             Disable sitemap generation for the specified instances
51     --generate            (Re)generate stiemap for the specified instances
52     --quiet|-q            Make the script quiet about non existent instance names
53                           (useful for calling from another scripts).
54     --help|-h             Display this help message
55
56 EOF
57 }
58
59 enable_sitemap()
60 {
61     local instance=$1
62     local libdir="/var/lib/koha"
63
64     if is_sitemap_enabled $instance; then
65         [ "$quiet" = "no" ] && \
66             warn "Sitemap already enabled for ${instance}"
67     else
68
69         sudo -u "$instance-koha" \
70             touch $libdir/$instance/sitemap.enabled
71
72         [ "$quiet" = "no" ] && \
73             warn "Sitemap enabled for ${instance}"
74     fi
75 }
76
77 disable_sitemap()
78 {
79     local instance=$1
80     local libdir="/var/lib/koha"
81
82     if is_sitemap_enabled $instance; then
83         sudo -u "$instance-koha" \
84             rm -f $libdir/$instance/sitemap.enabled
85         [ "$quiet" = "no" ] && \
86             warn "Sitemap disabled for ${instance}"
87     else
88         [ "$quiet" = "no" ] && \
89             warn "Sitemap already disabled for ${instance}"
90     fi
91 }
92
93 generate_sitemap()
94 {
95     local instance=$1
96     local sitemapdir="/var/lib/koha/$instance/sitemap"
97
98     if ! is_sitemap_enabled $instance; then
99         [ "$quiet" = "no" ] && \
100             warn "Sitemap not enabled for ${instance}."
101     else
102         if [ ! -d "$sitemapdir" ]; then
103             # Need to create directory
104             [ "$quiet" = "no" ] && \
105                 warn "Sitemap directory for ${instance} doesn't exist. Creating."
106             sudo -u "$instance-koha" \
107                 mkdir -p "$sitemapdir"
108         fi
109
110         if sudo -u "$instance-koha" -H \
111             env PERL5LIB=$PERL5LIB \
112             KOHA_CONF="/etc/koha/sites/$instance/koha-conf.xml" \
113             $KOHA_BINDIR/cronjobs/sitemap.pl \
114                 --dir $sitemapdir ; then
115             return 0
116         else
117             return 1
118         fi
119     fi
120 }
121
122 check_env_and_warn()
123 {
124     local apache_version_ok="no"
125
126     if /usr/sbin/apache2ctl -v | grep -q -v "Server version: Apache/2.4"; then
127         [ "$quiet" = "no" ] && \
128             cat 1>&2 <<EOM
129 WARNING: the shipped Apache configuration requires version 2.4.x and you don't have that.
130          Sitemap files will be generated, but refer to the docs to make them publicly available.
131 EOM
132     fi
133 }
134
135 set_action()
136 {
137     if [ "$op" = "" ]; then
138         op=$1
139     else
140         die "Error: only one action can be specified."
141     fi
142 }
143
144 op=""
145 quiet="no"
146
147 # Read command line parameters
148 while [ $# -gt 0 ]; do
149
150     case "$1" in
151         -h|--help)
152             usage ; exit 0 ;;
153         -q|--quiet)
154             quiet="yes"
155             shift ;;
156         --enable)
157             set_action "enable"
158             shift ;;
159         --disable)
160             set_action "disable"
161             shift ;;
162         --generate)
163             set_action "generate"
164             shift ;;
165         -*)
166             die "Error: invalid option switch ($1)" ;;
167         *)
168             # We expect the remaining stuff are the instance names
169             break ;;
170     esac
171
172 done
173
174 # Optionally use alternative paths for a dev install
175 adjust_paths_dev_install $1
176 if [ "$DEV_INSTALL" = "" ]; then
177     KOHA_BINDIR=$KOHA_HOME/bin
178 else
179     KOHA_BINDIR=$KOHA_HOME/misc
180 fi
181
182 if [ $# -gt 0 ]; then
183     # We have at least one instance name
184     for name in "$@"; do
185
186         if is_instance $name; then
187
188             case $op in
189                 "generate")
190                     generate_sitemap $name
191                     ;;
192                 "enable")
193                     enable_sitemap $name
194                     ;;
195                 "disable")
196                     disable_sitemap $name
197                     ;;
198                 *)
199                     usage
200                     ;;
201             esac
202
203         else
204             if [ "$quiet" = "no" ]; then
205                 warn "Error: Invalid instance name $name"
206             fi
207         fi
208
209     done
210 else
211     if [ "$quiet" = "no" ]; then
212         warn "Error: you must provide at least one instance name"
213     fi
214 fi
215
216 exit 0