Merge branch 'new/bug11185' into 3.14.x
[koha.git] / debian / scripts / koha-remove
1 #!/bin/sh
2 #
3 # koha-remove -- Remove 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 args=$(getopt -l keep-mysql -o k -n $0 -- "$@")
23 eval set -- $args
24 while [ ! -z "$1" ]
25 do
26     case "$1" in
27          -k|--keep-mysql) keepmysql=1; shift;;
28          --) shift; break;;
29           *) break;;
30     esac
31     shift
32 done
33
34 NAMES="$@"
35
36 SITECONFDIR="/etc/koha/sites"
37 # There has to be a better way of excluding '.' from find. But this works.
38 INSTANCES=`cd $SITECONFDIR && find . -type d -printf " %f" |sed s/\ .\ //`
39
40 if [ -z $NAMES ] ; then
41     echo "Please specify a Koha instance name. Your choices are:"
42     echo "$INSTANCES"
43     exit 1
44 fi
45
46 for name in $NAMES
47 do
48     # Does the directory (ie instance) name exist?
49     if [ ! -d $SITECONFDIR/$name ] ; then
50      echo Koha configuration directory for instance \"$name\" does not exist, please specify a valid Koha instance
51   exit 1
52     fi
53
54     echo "Removing Koha instance $name"
55     mysql_hostname="localhost"
56     if [ "$keepmysql" != "1" ]
57     then
58     # The grant creates the user in case it isn't, we don't want our loop to fail if it has already being deleted.
59     mysql --defaults-extra-file=/etc/mysql/koha-common.cnf <<eof
60 GRANT USAGE ON \`koha_$name\`.* TO \`koha_$name\`@\`%\`;
61 GRANT USAGE ON \`koha_$name\`.* TO \`koha_$name\`@\`$mysql_hostname\`;
62 DROP USER \`koha_$name\`@\`%\`;
63 DROP USER \`koha_$name\`@\`$mysql_hostname\`;
64 DROP DATABASE IF EXISTS \`koha_$name\`;
65 FLUSH PRIVILEGES;
66 eof
67     fi #`
68     
69     # If the daemon is not running already, we don't want to fail this loop. So bin the result code if this fails.
70     koha-stop-zebra $name || /bin/true
71     [ -f "/etc/apache2/sites-available/$name" ]  && \
72         rm "/etc/apache2/sites-available/$name"
73     [ -f "/etc/koha/sites/$name/koha-conf.xml" ] && \
74         rm "/etc/koha/sites/$name/koha-conf.xml"
75     [ -f "/etc/koha/sites/$name/zebra-biblios.cfg" ] && \
76         rm "/etc/koha/sites/$name/zebra-biblios.cfg"
77     [ -f "/etc/koha/sites/$name/zebra-biblios-dom.cfg" ] && \
78         rm "/etc/koha/sites/$name/zebra-biblios-dom.cfg"
79     [ -f "/etc/koha/sites/$name/zebra-authorities.cfg" ] && \
80         rm "/etc/koha/sites/$name/zebra-authorities.cfg"
81     [ -f "/etc/koha/sites/$name/zebra-authorities-dom.cfg" ] && \
82         rm "/etc/koha/sites/$name/zebra-authorities-dom.cfg"
83     [ -f "/etc/koha/sites/$name/zebra.passwd" ] && \
84         rm "/etc/koha/sites/$name/zebra.passwd"
85     # Maybe a user has left something in the config directory they want to keep? We won't delete it here, nor throw an error if the have.
86     [ -d "/etc/koha/sites/$name" ] && \
87         rmdir --ignore-fail-on-non-empty "/etc/koha/sites/$name"
88     [ -d "/var/lock/koha/$name" ] && \
89         rm -r "/var/lock/koha/$name"
90     [ -d "/var/log/koha/$name" ] && \
91         rm -r "/var/log/koha/$name"
92     [ -d "/var/run/koha/$name" ] && \
93         rm -r "/var/run/koha/$name"
94     getent passwd "$name-koha" > /dev/null && deluser --quiet "$name-koha"
95     # in case the site has already been disabled, we don't want to break the loop now.
96     a2dissite "$name" || /bin/true
97 done
98
99 service apache2 restart