#!/usr/bin/perl -w # please develop with -w # # chances are you'll need to install CPAN IO::Tee # perl -MCPAN -e'install "IO::Tee"' # # written @ Mon Jul 06 13:40:53 EDT 2009 patches@koha.org # use strict; # please develop with the strict pragma use Config; use IO::Tee; use diagnostics; use DBI; use File::Copy; use File::Path; ################################################################### # Global Assignments # (You may edit these values) ################################################################### my $LOGDIR="/var/log/koha"; my $ETCDIR="/etc/koha"; my $DROPMODE=""; my $DELCOMPLETE=""; my $MODE="SAFE"; my $KOHAROOT=""; my $CONFIGFILE=""; my $TEE_DUAL_OUTPUT; my @kohausers =qw(koha kohaadmin kohauser); my @kohagroups =qw(koha kohaadmin kohauser); ################################################################### # Support Multiple Machine Architectures ################################################################### my $deluser; my $delgroup; for ($Config{myarchname}) { if (/i686-linux/) { $deluser= "/usr/sbin/userdel"; $delgroup= "/usr/sbin/groupdel"; }elsif (/i86pc-solaris/){ $deluser= "/usr/sbin/userdel"; $delgroup= "/usr/sbin/groupdel"; }elsif (/sun4-solaris/){ $deluser= "/usr/sbin/userdel"; $delgroup= "/usr/sbin/groupdel"; }else { $deluser= "/usr/sbin/userdel"; $delgroup= "/usr/sbin/groupdel"; } } ################################################################### # Program Main # (Do not edit) ################################################################### #[1] Open an uninstall logfile & open_logfile; #[2] Ask for deletion info. But, only do this as an input flag # or if you cannot locate any del info if ($ARGV[0]){ if ($ARGV[0] =~ /-user/){ & user_input; }else{ if ($ARGV[0] =~ /-h\b|-help\b/i){ & printhelp; exit 0; }else{ if ($ARGV[0] =~ /-dropdb\b/i){ & drop_the_koha_db; exit 0; } } } }else{ & printhelpshort; } # Search for ENV variables & locate_env_variables; ################################################################### # The Unix Help Documentation ################################################################### sub printhelp{ print $TEE_DUAL_OUTPUT <From the location of the koha Perl library at: $ENV{PERL5LIB} I make an educated guess that there is a koha installation at: $KOHAROOT At the moment $KOHAROOT is KOHAROOT ENVFOUND } sub noguarantee{ print $TEE_DUAL_OUTPUT <new(">> /tmp/koha_uninstall_history.log", \*STDOUT); } sub is_this_authentic_koha_root{ my $BADUNINSTALLINFO=""; my $manyerrors="0"; my @KOHAFILES=qw( bin doc intranet lib man misc opac ); foreach (@KOHAFILES) { if ($manyerrors < 3){ if ( !-d "$KOHAROOT/$_"){ ++$manyerrors; $BADUNINSTALLINFO="True"; & warnaboutroot; } # Bailout of uninstall. You've been given bad uninstall info. } else { if ($manyerrors < 3){; if ( -f "$KOHAROOT/$_"){ print $TEE_DUAL_OUTPUT "Validating $KOHAROOT [Found] $_\n"; } } } } if ( "$BADUNINSTALLINFO" !~ /True/){ & okibelieve; & prep_genuine_root_del; } } sub locate_env_variables{ # The first thing we need to do is test for ENV variables if ($ENV{KOHA_CONF}){ print $TEE_DUAL_OUTPUT "\n [Found] ENV Variable $ENV{KOHA_CONF} \n"; & try_etc_koha_conf_xml; } if (!$ENV{PERL5LIB}){ if (!$ENV{KOHA_CONF}){ print $TEE_DUAL_OUTPUT "\nWARNING PERL5LIB & KOHA_CONF ENV VARS missing!\n"; & try_etc_koha_conf_xml; & user_input; } }else{ if (!$ENV{KOHA_CONF}){ print $TEE_DUAL_OUTPUT "\nWARNING KOHA_CONF ENV VAR missing!\n"; }else{ print $TEE_DUAL_OUTPUT "\n [Found] ENV Variable $ENV{KOHA_CONF} \n"; & try_etc_koha_conf_xml; } # Env variables are present. $KOHAROOT=$ENV{PERL5LIB}; if ( $ENV{PERL5LIB} =~ /lib\// ){ $KOHAROOT =~ s/lib\///; }else{ $KOHAROOT =~ s/lib//; } $KOHAROOT =~ s/koha\//koha/; & printenvfound; & is_this_authentic_koha_root; } } sub try_etc_koha_conf_xml{ if ( -f "/etc/koha-conf.xml" ){ $CONFIGFILE="/etc/koha-conf.xml"; & read_config_file; & del_conf; }else{ if ( -f "/etc/koha/koha-conf.xml" ){ $CONFIGFILE="/etc/koha/koha-conf.xml"; & read_config_file; & del_conf; }else{ if ( -f "$ENV{KOHA_CONF}" ){ $CONFIGFILE="$ENV{KOHA_CONF}"; & read_config_file; & del_conf; } } sub read_config_file{ my $etc_conf_log; print $TEE_DUAL_OUTPUT "\n\n [Found] Config at loc $CONFIGFILE \n"; open(CONFIG_FILE, "<$CONFIGFILE"); while (){ if ($_ =~ /install_log/){ s/\//; # config is XML trim this off s/\<\/install_log\>//; s/\s+//g; chomp; $etc_conf_log = $_; if ( -f "$etc_conf_log" ){ $KOHAROOT=$etc_conf_log; $KOHAROOT =~ s/koha-install-log//; $KOHAROOT =~ s/misc\///; print $TEE_DUAL_OUTPUT <); chop $kohainstallation; if ( -d $kohainstallation ){ $KOHAROOT = "$kohainstallation"; print " [Found] $KOHAROOT Do you want to delete it? Yes or No?\n"; my $continue2=(); chop $continue2; if ($continue2 =~ /^Y\b|^yes|^y\b|^Yes/i) { # Uninstall user-specified koharoot & is_this_authentic_koha_root; } }else{ print <); chop $dropit; if ($dropit =~ /^Y\b|^yes|^y\b|^Yes/i) { # You have permission to drop the database print "Are you sure that you want to drop the koha database? Yes or No?\n"; my $sure=(); chop $sure; if ($sure =~ /^Y\b|^yes|^y\b|^Yes/i) { #How sure does he need to be? (Remove it.) & run_drop_db_code; } } print "Do you want to delete koha's users and groups? Yes or No?\n"; my $people=(); chop $people; if ($people =~ /^Y\b|^yes|^y\b|^Yes/i) { # del users & groups & del_users_and_groups; } sub run_drop_db_code{ my $DBPASSWD="katikoan"; my $DBUSER="root"; if ( $DROPMODE !~ /SILENT/){ print "Please enter [mysql]database username of mysql admin user. \n"; $DBUSER=(); chop $DBUSER; print "Please enter [mysql]database password for the mysql admin user. \n"; $DBPASSWD=(); chop $DBPASSWD; } my $dbh=DBI->connect('DBI:mysql:mysql:localhost:port=3306:socket=/tmp/mysqld-new.sock', $DBUSER, $DBPASSWD) or warn "Cannot connect to database : $DBI::errstr\n"; my $sth = $dbh->prepare('drop database koha') or warn "Couldn't prepare statement: " . $dbh->errstr; $sth->execute() # Execute the query or warn "Couldn't execute statement: " . $sth->errstr; $sth->finish; $dbh->disconnect; } sub del_users_and_groups{ foreach (@kohausers){ `$deluser $_`; } foreach (@kohagroups){ `$delgroup $_`; } } } sub deletelogs{ if (-d "$LOGDIR") { my @KOHALOGS=qw( koha-access_log koha-intranet-rewrite.log koha-opac-error_log README koha-error_log koha-opac-access_log koha-opac-rewrite.log); foreach (@KOHALOGS) { print $TEE_DUAL_OUTPUT "[DELETING] $LOGDIR/$_\n"; unlink "$LOGDIR/$_"; } rmdir ("$LOGDIR") || warn (" [ERROR IN DELETING] $LOGDIR $?"); } }