prepping ubuntu packages
[openemr.git] / contrib / util / ubuntu_package_scripts / development / prerm
blob365a0171cdb42684d768326df78c4116f4cc1599
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: Brady Miller <brady@sparmy.com>
10 # Amalu Obinna <amaluobinna@aol.com>
12 # Un-install script steps:
13 # 1) Collect the MySQL root user password (mandatory)
14 # 2) Remove OpenEMR etc directory
15 # 3) Remove OpenEMR web directory
16 # 4) Remove OpenEMR MySQL database
17 # 5) Remove OpenEMR MySQL user
20 # summary of how this script can be called:
21 # * <prerm> `remove'
22 # * <old-prerm> `upgrade' <new-version>
23 # * <new-prerm> `failed-upgrade' <old-version>
24 # * <conflictor's-prerm> `remove' `in-favour' <package> <new-version>
25 # * <deconfigured's-prerm> `deconfigure' `in-favour'
26 # <package-being-installed> <version> `removing'
27 # <conflicting-package> <version>
28 # for details, see http://www.debian.org/doc/debian-policy/ or
29 # the debian-policy package
31 # Source debconf library.
32 . /usr/share/debconf/confmodule
34 case "$1" in
35 remove)
37 #constants and paths
38 LOGDIR=/var/log/git-openemr
39 LOG=$LOGDIR/install
40 CONFIGDIR=/etc/git-openemr
41 CONFIG=$CONFIGDIR/git-openemr.conf
42 WEB=/var/www
43 OPENEMR=$WEB/git-openemr
44 SITEDIR=$OPENEMR/sites/default
46 #Standardized echo function to send to only log file
47 # requires one parameter (string)
48 log_only () {
49 echo "`date`: $1" >> $LOG
52 #Standardized exit function to be used when unable to uninstall the
53 #openemr package
54 # requires one parameter (string with reason for exiting)
55 unable_exit () {
56 echo "`date`: $1" >> $LOG
57 echo "`date`: EXITING.........." >> $LOG
58 exit 1
61 #function to check mysql for selected databases
62 # 1st param is password, 2nd param database, 3rd param is host (optional), 4th param is user (optional)
63 check_mysql () {
64 if [ -n "$3" ]; then
65 HOST=$3
66 else
67 HOST=localhost
69 if [ -n "$4" ]; then
70 USE=$4
71 else
72 USE=root
75 if [ "`mysql -u "$USE" -h "$HOST" --password="$1" -e 'show databases' 2>/dev/null | awk '{ print $1}' | grep "^$2$"`" == "$2" ]; then
76 return 0
77 else
78 return 1
82 #function to collect variables from config files
83 # 1st param is variable name, 2nd param is filename
84 collect_var () {
85 echo `grep -i "^[[:space:]]*$1[[:space:]=]" $2 | cut -d \= -f 2 | cut -d \; -f 1 | sed "s/[ '\"]//gi"`
88 #function to prompt for input
89 # 1st param is name, 2nd param is priority, 3rd param is where result gets sent back in
90 # return the input
91 prompt_input () {
92 db_set "$1" ""
93 db_fset "$1" seen false
94 db_input "$2" "$1" || true
95 db_go || true
96 db_get "$1"
97 local input_value="$RET"
98 db_set "$1" ""
99 db_fset "$1" seen false
100 local __result=$3
101 eval $__result="'$input_value'"
104 #collect the mysql root password
105 MPASS=""
106 if check_mysql "$MPASS" "mysql"; then
107 log_only "Passed the mysql check loop"
108 else
109 #the blank initial mysql password didn't work, so prompt for password
110 # (will give 3 chances to provide correct password)
111 COUNTDOWN=1
112 while true; do
113 prompt_input git-openemr/mysql_p_remove_${COUNTDOWN} critical ret_result
114 MPASS="$ret_result"
115 if check_mysql "$MPASS" "mysql"; then
116 #the mysql root password works, so can exit loop
117 log_only "Passed the mysql check loop"
118 break
119 else
120 #the mysql root password did not work
121 if [ "$COUNTDOWN" -ge "3" ]; then
122 prompt_input git-openemr/unable_remove critical ret_result
123 unable_exit "OpenEMR removal attempt failed (can try again when you know the mysql root password)"
126 let "COUNTDOWN += 1"
127 done
130 #collect scripting information from config file
131 PROCESS=$(collect_var process $CONFIG)
132 PLAN=$(collect_var plan $CONFIG)
134 # Collect database information from sqlconf.php file
135 SQLLOCATION=$(collect_var \$host $SITEDIR/sqlconf.php)
136 SQLUSER=$(collect_var \$login $SITEDIR/sqlconf.php)
137 SQLPASSWORD=$(collect_var \$pass $SITEDIR/sqlconf.php)
138 SQLDATABASE=$(collect_var \$dbase $SITEDIR/sqlconf.php)
140 #remove etc directory
141 rm -rf $CONFIGDIR
142 log_only "Removed OpenEMR etc directory"
144 #remove web directory
145 rm -rf $OPENEMR
146 log_only "Finished removing OpenEMR web directory"
148 #remove openemr mysql database (ensure the database exists)
149 if check_mysql "$MPASS" "$SQLDATABASE"; then
150 mysqladmin -f -u root -h "$SQLLOCATION" --password="$MPASS" drop "$SQLDATABASE" >> $LOG 2>&1
151 log_only "Removed OpenEMR MySQL database"
154 #remove openemr mysql user
155 mysql -f -u root -h "$SQLLOCATION" --password="$MPASS" -e "DELETE FROM mysql.user WHERE User = '$SQLUSER';FLUSH PRIVILEGES;" >> $LOG 2>&1
156 log_only "Removed OpenEMR MySQL user"
158 #remove OpenEMR apache set up as active config
159 log_only "Turn off apache conf for OpenEMR"
160 a2dissite git-openemr.conf
162 #stop db
163 db_stop
165 exit 0
168 upgrade)
170 #echo "No need for upgrade instructions in prerm script to version $2"
171 exit 0
174 deconfigure)
176 echo "prerm asked to do deconfigure"
177 exit 0
180 failed-upgrade)
182 echo "prerm asked to do failed-upgrade from version $2"
183 exit 0
187 echo "prerm called with unknown argument \`$1'" >&2
188 exit 1
190 esac