feat: show collection balance in billing widget (#7454)
[openemr.git] / contrib / util / ubuntu_package_scripts / production / prerm
blob466596899a496c32569b44c5df86e47ffc3b418c
1 #!/bin/bash -e
3 #This program is free software; you can redistribute it and/or modify
4 #it under the terms of the GNU General Public License as published by
5 #the Free Software Foundation; either version 2 of the License, or
6 #(at your option) any later version.
8 # Copyright 2011-2014
9 # authors: Amalu Obinna <amaluobinna@aol.com>
10 # Brady Miller <brady.g.miller@gmail.com>
12 # Un-install script steps:
13 # 1) Warn and confirm the user wants to remove the package (mandatory)
14 # 2) Collect the MySQL root user password (mandatory)
15 # 3) Remove openemr MySQL database(s)
16 # 4) Remove openemr MySQL user(s)
17 # 5) Remove OpenEMR etc directory
18 # 6) Remove rest of OpenEMR web directory
19 # 7) Remove tmp directory
20 # 8) Remove OpenEMR apache configuration
23 # summary of how this script can be called:
24 # * <prerm> `remove'
25 # * <old-prerm> `upgrade' <new-version>
26 # * <new-prerm> `failed-upgrade' <old-version>
27 # * <conflictor's-prerm> `remove' `in-favour' <package> <new-version>
28 # * <deconfigured's-prerm> `deconfigure' `in-favour'
29 # <package-being-installed> <version> `removing'
30 # <conflicting-package> <version>
31 # for details, see http://www.debian.org/doc/debian-policy/ or
32 # the debian-policy package
34 # Source debconf library.
35 . /usr/share/debconf/confmodule
37 case "$1" in
38 remove)
40 #constants and paths
41 LOGDIR=/var/log/openemr
42 LOG=$LOGDIR/install
43 CONFIGDIR=/etc/openemr
44 CONFIG=$CONFIGDIR/openemr.conf
45 TMPDIR=/tmp/openemr-tmp
46 WEB=/var/www
47 OPENEMR=$WEB/openemr
48 SITEDIR=$OPENEMR/sites
50 #Standardized echo function to send to only log file
51 # requires one parameter (string)
52 log_only () {
53 echo "`date`: $1" >> $LOG
56 #Standardized exit function to be used when unable to uninstall the
57 #openemr package
58 # requires one parameter (string with reason for exiting)
59 unable_exit () {
60 echo "`date`: $1" >> $LOG
61 echo "`date`: EXITING.........." >> $LOG
62 exit 1
65 #function to check mysql for selected databases
66 # 1st param is password, 2nd param database, 3rd param is host (optional), 4th param is user (optional)
67 check_mysql () {
68 if [ -n "$3" ]; then
69 HOST=$3
70 else
71 HOST=localhost
73 if [ -n "$4" ]; then
74 USE=$4
75 else
76 USE=root
79 if [ "`mysql -u "$USE" -h "$HOST" --password="$1" -e 'show databases' 2>/dev/null | awk '{ print $1}' | grep "^$2$"`" == "$2" ]; then
80 return 0
81 else
82 return 1
86 #function to collect variables from config files
87 # 1st param is variable name, 2nd param is filename
88 collect_var () {
89 echo `grep -i "^[[:space:]]*$1[[:space:]=]" $2 | cut -d \= -f 2 | cut -d \; -f 1 | sed "s/[ '\"]//gi"`
92 #function to prompt for input
93 # 1st param is name, 2nd param is priority, 3rd param is where result gets sent back in
94 # return the input
95 prompt_input () {
96 db_set "$1" ""
97 db_fset "$1" seen false
98 db_input "$2" "$1" || true
99 db_go || true
100 db_get "$1"
101 local input_value="$RET"
102 db_set "$1" ""
103 db_fset "$1" seen false
104 local __result=$3
105 eval $__result="'$input_value'"
108 #Confirm that user really wants to uninstall
109 prompt_input openemr/confirm_remove critical ret_result
110 if [ "$ret_result" == "no" ]; then
111 unable_exit "You have chosen to not uninstall the OpenEMR package."
114 #collect the mysql root password
115 MPASS=""
116 if check_mysql "$MPASS" "mysql"; then
117 log_only "Passed the mysql check loop"
118 else
119 #the blank initial mysql password didn't work, so prompt for password
120 # (will give 3 chances to provide correct password)
121 COUNTDOWN=1
122 while true; do
123 prompt_input openemr/mysql_p_remove_${COUNTDOWN} critical ret_result
124 MPASS="$ret_result"
125 if check_mysql "$MPASS" "mysql"; then
126 #the mysql root password works, so can exit loop
127 log_only "Passed the mysql check loop"
128 break
129 else
130 #the mysql root password did not work
131 if [ "$COUNTDOWN" -ge "3" ]; then
132 prompt_input openemr/unable_remove critical ret_result
133 unable_exit "OpenEMR removal attempt failed (can try again when you know the mysql root password)"
136 let "COUNTDOWN += 1"
137 done
140 #collect scripting information from config file
141 PROCESS=$(collect_var process $CONFIG)
142 PLAN=$(collect_var plan $CONFIG)
144 for dir in $(find $SITEDIR/* -maxdepth 0 -type d ); do
145 #collect sitename
146 SITENAME=$(basename "$dir")
148 # Collect database information from sqlconf.php file
149 SQLLOCATION=$(collect_var \$host $SITEDIR/$SITENAME/sqlconf.php)
150 SQLUSER=$(collect_var \$login $SITEDIR/$SITENAME/sqlconf.php)
151 SQLPASSWORD=$(collect_var \$pass $SITEDIR/$SITENAME/sqlconf.php)
152 SQLDATABASE=$(collect_var \$dbase $SITEDIR/$SITENAME/sqlconf.php)
154 #remove openemr mysql database (ensure the database exists)
155 if check_mysql "$MPASS" "$SQLDATABASE"; then
156 mysqladmin -f -u root -h "$SQLLOCATION" --password="$MPASS" drop "$SQLDATABASE" >> $LOG 2>&1
157 log_only "Removed OpenEMR MySQL database: $SQLDATABASE"
160 #remove openemr mysql user
161 mysql -f -u root -h "$SQLLOCATION" --password="$MPASS" -e "DELETE FROM mysql.user WHERE User = '$SQLUSER';FLUSH PRIVILEGES;" >> $LOG 2>&1
162 log_only "Removed OpenEMR MySQL user: $SQLUSER"
163 done
165 #remove etc directory
166 rm -rf $CONFIGDIR
167 log_only "Removed OpenEMR etc directory"
169 #remove rest of web directory
170 rm -rf $OPENEMR
171 log_only "Finished removing OpenEMR web directory"
173 #remove tmp directory
174 rm -fr $TMPDIR
175 log_only "Removed OpenEMR tmp directory"
177 #removes the configuration section for OpenEMR in Apache config file (deprecated, but keeping for older packages)
178 if [ -f /etc/apache2/httpd.conf ]; then
179 sed -i '/#This is the start of the Apache configuration for OpenEMR./,/#This is the end of the Apache configuration for OpenEMR./d' /etc/apache2/httpd.conf
180 log_only "Removed OpenEMR Apache configuration in /etc/apache2/httpd.conf"
183 #remove OpenEMR apache set up as active config
184 log_only "Turn off apache conf for OpenEMR"
185 a2dissite openemr.conf
187 #stop db
188 db_stop
190 exit 0
193 upgrade)
195 #echo "No need for upgrade instructions in prerm script to version $2"
196 exit 0
199 deconfigure)
201 echo "prerm asked to do deconfigure"
202 exit 0
205 failed-upgrade)
207 echo "prerm asked to do failed-upgrade from version $2"
208 exit 0
212 echo "prerm called with unknown argument \`$1'" >&2
213 exit 1
215 esac