Bug 30084: Remove explicit dependency of liblocale-codes-perl
[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 # 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 args=$(getopt -l keep-mysql,purge-all -o kp -n $0 -- "$@")
31 eval set -- $args
32 while [ ! -z "$1" ]
33 do
34     case "$1" in
35          -k|--keep-mysql) keepmysql=1;;
36          -p|--purge-all) purgeall=1;;
37              # purgeall removes all instance files in var/lib/koha and var/spool/koha
38          --) shift; break;;
39           *) break;;
40     esac
41     shift
42 done
43
44 NAMES="$@"
45
46 SITECONFDIR="/etc/koha/sites"
47 # There has to be a better way of excluding '.' from find. But this works.
48 INSTANCES=$(get_instances)
49
50 if [ -z $NAMES ] ; then
51     echo "Please specify a Koha instance name. Your choices are:"
52     echo "$INSTANCES"
53     exit 1
54 fi
55
56 for name in $NAMES
57 do
58     # Does the directory (ie instance) name exist?
59     if [ ! -d $SITECONFDIR/$name ] ; then
60      echo Koha configuration directory for instance \"$name\" does not exist, please specify a valid Koha instance
61   exit 1
62     fi
63
64     echo "Removing Koha instance $name"
65     mysql_hostname="localhost"
66     if [ "$keepmysql" != "1" ]
67     then
68     mysql --defaults-extra-file=/etc/mysql/koha-common.cnf <<eof
69 DROP USER IF EXISTS \`koha_$name\`@\`%\`;
70 DROP USER IF EXISTS \`koha_$name\`@\`$mysql_hostname\`;
71 DROP DATABASE IF EXISTS \`koha_$name\`;
72 FLUSH PRIVILEGES;
73 eof
74     fi #`
75
76     # Stop the Zebra server if needed
77     if is_zebra_running $name; then
78         koha-zebra --stop $name || /bin/true
79     fi
80     # Stop the indexer daemon if needed
81     if is_indexer_running $name; then
82         koha-indexer --stop $name || /bin/true
83     fi
84     # Stop the worker daemon if needed
85     if is_worker_running $name; then
86         koha-worker --stop $name || /bin/true
87     fi
88     # Stop the Plack server if needed
89     if is_plack_running $name; then
90         koha-plack --stop $name || /bin/true
91     fi
92     # Stop the SIP server if needed
93     if is_sip_running $name; then
94         koha-sip --stop $name || /bin/true
95     fi
96
97     instancefile=$(get_apache_config_for $name)
98     le_opacdomain=$(letsencrypt_get_opacdomain_for $name)
99
100     [ -f "$instancefile" ]  && \
101         rm "$instancefile"
102     [ -f "/etc/koha/sites/$name/koha-conf.xml" ] && \
103         rm "/etc/koha/sites/$name/koha-conf.xml"
104     [ -f "/etc/koha/sites/$name/log4perl.conf" ] && \
105         rm "/etc/koha/sites/$name/log4perl.conf"
106     [ -f "/etc/koha/sites/$name/zebra-biblios.cfg" ] && \
107         rm "/etc/koha/sites/$name/zebra-biblios.cfg"
108     [ -f "/etc/koha/sites/$name/zebra-biblios-dom.cfg" ] && \
109         rm "/etc/koha/sites/$name/zebra-biblios-dom.cfg"
110     [ -f "/etc/koha/sites/$name/zebra-authorities.cfg" ] && \
111         rm "/etc/koha/sites/$name/zebra-authorities.cfg"
112     [ -f "/etc/koha/sites/$name/zebra-authorities-dom.cfg" ] && \
113         rm "/etc/koha/sites/$name/zebra-authorities-dom.cfg"
114     [ -f "/etc/koha/sites/$name/zebra.passwd" ] && \
115         rm "/etc/koha/sites/$name/zebra.passwd"
116
117     tempdir=$(get_tmpdir)
118     [ -d "$tempdir/koha_${name}_upload" ] && \
119         # Temporary uploads can be discarded, apart from purgeall
120         rm -r "$tempdir/koha_${name}_upload"
121
122     [ -f "/var/lib/koha/$name/letsencrypt.enabled" ] && \
123         rm -r "/var/lib/koha/$name/letsencrypt.enabled"
124     [ -f "/etc/letsencrypt/renewal/$le_opacdomain.conf" ] && \
125         rm -r "/etc/letsencrypt/renewal/$le_opacdomain.conf"
126     # 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.
127     [ -d "/etc/koha/sites/$name" ] && \
128         rmdir --ignore-fail-on-non-empty "/etc/koha/sites/$name"
129     [ -d "/var/lock/koha/$name" ] && \
130         rm -r "/var/lock/koha/$name"
131     [ -d "/var/log/koha/$name" ] && \
132         rm -r "/var/log/koha/$name"
133     [ -d "/var/cache/koha/$name" ] && \
134         rm -r "/var/cache/koha/$name"
135     [ -d "/var/run/koha/$name" ] && \
136         rm -r "/var/run/koha/$name"
137     [ "$purgeall" = "1" ] && [ -d "/var/lib/koha/$name" ] && \
138         rm -r "/var/lib/koha/$name"
139     [ "$purgeall" = "1" ] && [ -d "/var/spool/koha/$name" ] && \
140         rm -r "/var/spool/koha/$name"
141     getent passwd "$name-koha" > /dev/null && deluser --quiet "$name-koha"
142     # in case the site has already been disabled, we don't want to break the loop now.
143     a2dissite "$name" > /dev/null 2>&1 || a2dissite "${name}.conf" > /dev/null 2>&1 || /bin/true
144 done
145
146 service apache2 restart