Bug 11404: add support for Apache 2.4's config file convention
[koha.git] / debian / scripts / koha-enable
1 #!/bin/sh
2 #
3 # koha-enable -- enable a Koha instance.
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
23 die()
24 {
25     echo "$@" 1>&2
26     exit 1
27 }
28
29 warn()
30 {
31     echo "$@" 1>&2
32 }
33
34 is_enabled()
35 {
36     local instancename=$1
37     local instancefile="/etc/apache2/sites-available/$instancename.conf"
38
39     if ! is_instance $instancename; then
40         return 1
41     fi
42
43     if grep -q '^[[:space:]]*Include /etc/koha/apache-shared-disable.conf' \
44             "$instancefile" ; then
45         return 1
46     else
47         return 0
48     fi
49 }
50
51 is_instance()
52 {
53     local instancename=$1
54
55     if find /etc/koha/sites -mindepth 1 -maxdepth 1 \
56                          -type d -printf '%f\n'\
57           | grep -q -x $instancename ; then
58         return 0
59     else
60         return 1
61     fi
62 }
63
64 enable_instance()
65 {
66     local instancename=$1
67     local instancefile="/etc/apache2/sites-available/$instancename.conf"
68
69     if ! is_enabled $instancename; then
70         sed -i 's:^\(\s*Include /etc/koha/apache-shared-disable.conf\)$:#\1:' \
71             "$instancefile"
72         return 0
73     else
74         return 1
75     fi
76 }
77
78 usage()
79 {
80     local scriptname=$0
81     cat <<EOF
82 Enables Koha instances.
83
84 Usage: $scriptname instancename1 instancename2...
85
86 EOF
87 }
88
89 # Parse command line.
90 [ $# -ge 1 ] || ( usage ; die "Missing instance name..." )
91
92 restart_apache="no"
93
94 for name in "$@"
95 do
96     if is_instance $name ; then
97         if enable_instance $name; then
98             restart_apache="yes"
99         else
100             warn "Instance $name already enabled."
101         fi
102     else
103         warn "Unknown instance $name."
104     fi
105 done
106
107 if [ "$restart_apache" = "yes" ]; then
108     /etc/init.d/apache2 restart
109 fi
110
111 exit 0