Bug 17717: (QA follow-up) Fix typo chdir
[koha.git] / debian / scripts / koha-foreach
1 #!/bin/sh
2 # koha-foreach -- run a command for each Koha instance
3 # Copyright 2010  Catalyst IT, Ltd
4
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 set -e
19
20 # Read configuration variable file if it is present
21 [ -r /etc/default/koha-common ] && . /etc/default/koha-common
22
23 # include helper functions
24 if [ -f "/usr/share/koha/bin/koha-functions.sh" ]; then
25     . "/usr/share/koha/bin/koha-functions.sh"
26 else
27     echo "Error: /usr/share/koha/bin/koha-functions.sh not present." 1>&2
28     exit 1
29 fi
30
31 chdir="no"
32 starting_dir=$(pwd)
33
34 listopts=""
35 while [ ! -z "$1" ]
36 do
37     case "$1" in
38         --chdir) chdir="yes";;
39         --email) listopts="$listopts --email";;
40       --noemail) listopts="$listopts --noemail";;
41       --enabled) listopts="$listopts --enabled";;
42      --disabled) listopts="$listopts --disabled";;
43           --sip) listopts="$listopts --sip";;
44         --nosip) listopts="$listopts --nosip";;
45         --plack) listopts="$listopts --plack";;
46       --noplack) listopts="$listopts --noplack";;
47   --letsencrypt) listopts="$listopts --letsencrypt" ;;
48 --noletsencrypt) listopts="$listopts --noletsencrypt" ;;
49               *) break;;
50     esac
51     shift
52 done
53
54 for name in $(koha-list $listopts)
55 do
56     # Replace the __instancename__ placeholder for the instance name (Bug 8566)
57     cmd=`echo "$@" | sed -e s/__instancename__/${name}/g`
58
59     if [ "${cmd}" != "" ]; then
60
61         # Change to the instance's home dir if required
62         [ "$chdir" != "no" ] && eval cd ~$name"-koha"
63
64         koha-shell ${name} -c "${cmd}"
65
66         # Go back to the original dir if required
67         [ "$chdir" != "no" ] && cd "$starting_dir"
68     fi
69 done