#!/usr/bin/perl

# ispCP ω (OMEGA) a Virtual Hosting Control Panel
# Copyright (C) 2001-2006 by moleSoftware GmbH - http://www.molesoftware.com
# Copyright (C) 2006-2010 by isp Control Panel - http://ispcp.net
#
# Version: $Id: ispcp-uninstall 3465 2010-10-15 20:07:59Z scitech $
#
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
# License for the specific language governing rights and limitations
# under the License.
#
# The Original Code is "VHCS - Virtual Hosting Control System".
#
# The Initial Developer of the Original Code is moleSoftware GmbH.
# Portions created by Initial Developer are Copyright (C) 2001-2006
# by moleSoftware GmbH. All Rights Reserved.
# Portions created by the ispCP Team are Copyright (C) 2006-2010 by
# isp Control Panel. All Rights Reserved.
#
# The ispCP ω Home Page is:
#
#    http://isp-control.net
#

use FindBin;
use lib "$FindBin::Bin/..";
use Term::ANSIColor qw(:constants colored);
require 'ispcp_common_code.pl';

use strict;
use warnings;

sub welcome_note {

	my $rdata = undef;

	push_el(\@main::el, 'welcome_note()', 'Starting...');

	my $welcome = colored(['bold underline'],
		"\n\tWelcome to ispCP '$main::cfg{'Version'}' Un-Install Dialog.\n\n"
	);

	my $welcome_message =
		"\tThis program will uninstall ispCP OMEGA system from your server.\n";

	my $welcome_warning = colored(['bold red'],
		"\t/!\\ WARNING: All domain users and their accounts will be removed.  /!\\\n"
	);

	print STDOUT $welcome, $welcome_message, $welcome_warning;

	do {
		print STDOUT "\n\tDo you really want to continue [Y/n]: ";
		chomp($rdata = readline \*STDIN);

	} while($rdata !~ /^(|y|n)$/i);

	exit 0 if($rdata =~ /^n$/i);

	push_el(\@main::el, 'welcome_note()', 'Ending...');

	0;
}

sub ask_remove_user_data {

	my ($rs, $rdata) = (undef, undef);

	push_el(\@main::el, 'ask_remove_user_data()', 'Starting...');

	do {
		print STDOUT "\tRemove user data? [y/N]: ";
		chomp($rdata = readline \*STDIN);
	} while($rdata !~ /^(|y|n)$/i);

	if ($rdata =~ /^y$/i) {
		$main::rm{'rm_u_data'} = 'yes';
	} else {
		$main::rm{'rm_u_data'} = 'no';
	}

	push_el(\@main::el, 'ask_remove_user_data()', 'Ending...');

	0;
}

sub user_dialog {

	my $rs = undef;

	push_el(\@main::el, 'user_dialog()', 'Starting...');

	welcome_note();
	ask_remove_user_data();

	push_el(\@main::el, 'user_dialog()', 'Ending...');

	0;
}

# Startup uninstall process
sub uninstall_startup {
	push_el(\@main::el, 'uninstall_start_up()', 'Starting...');

	my $rs = undef;

	umask(027);

	# config check;
	$rs = get_conf();
	return $rs if ($rs != 0);

	if (-e "/tmp/ispcp-uninstall-services.log"){
		$rs = del_file("/tmp/ispcp-uninstall-services.log");
		return $rs if ($rs != 0);
	}

	push_el(\@main::el, 'uninstall_start_up()', 'Ending...');

	0;
}

# Shutdown uninstall process
sub uninstall_shutdown {

	push_el(\@main::el, 'uninstall_shut_down()', 'Starting...');

	my $shutdown_message = colored(
		['bold underline'],
		"\n\n\tCongratulations, you removed ispCP OMEGA" .
		"'$main::cfg{'Version'}' successfully!\n\n"
	);

	print STDOUT $shutdown_message;

	push_el(\@main::el, 'uninstall_shut_down()', 'Ending...');

	0;
}

sub remove_sql_users{
	my ($rs, $rdata) = (undef, undef);

	push_el(\@main::el, 'remove_sql_users()', 'Starting...');

	my $sql = "select distinct(sqlu_name) from sql_user";

	($rs, $rdata) = doSQL($sql);
	if ($rs != 0) {
		print "cannot obtain sql user list!\n\n";
		send_error_mail('remove_sql_db()', "cannot obtain sql user list!");
	}

	my $sql_count = @$rdata;

	if ($sql_count > 0) {

		foreach (@$rdata) {

			my $row = $_;
			my $sqlu_name = @$row[0];

			$sql = "DROP USER '$sqlu_name'\@'localhost'";
			($rs, $rdata) = doSQL($sql);
			if ($rs != 0) {
				print "cannot remove sql user '$sqlu_name'\@localhost!\n\n";
				send_error_mail('remove_sql_db()', "cannot remove sql user '$sqlu_name'\@localhost!");
			}


			$sql = "DROP USER '$sqlu_name'\@'%'";
			($rs, $rdata) = doSQL($sql);
			if ($rs != 0) {
				print "cannot remove sql user '$sqlu_name'\@%!\n\n";
				send_error_mail('remove_sql_db()', "cannot remove sql user '$sqlu_name'\@%!");
			}

		}

	}

	push_el(\@main::el, 'remove_sql_users()', 'Ending...');

	return 0;
}

sub remove_sql_db{
	my ($rs, $rdata) = (undef, undef);

	push_el(\@main::el, 'remove_sql_db()', 'Starting...');

	my $sql = "select distinct(sqld_name) from sql_database";

	($rs, $rdata) = doSQL($sql);

	my $sql_count = @$rdata;

	if ($sql_count > 0) {

		foreach (@$rdata) {

			my $row = $_;
			my $sqld_name = @$row[0];

			$sql = "DROP DATABASE IF EXISTS $sqld_name;";
			($rs, $rdata) = doSQL($sql);
			if ($rs != 0) {
				print "cannot remove database $sqld_name!!\n\n";
				send_error_mail('remove_sql_db()', "cannot remove database $sqld_name!");
			}
		}

	}

	push_el(\@main::el, 'remove_sql_db()', 'Ending...');

	return 0;
}

sub uninstall_system_users {

	my ($rs, $rdata, $rows, $sql) = (undef, undef, undef, undef);

	push_el(\@main::el, 'uninstall_system_users()', 'Starting...');

	my ($fuid, $fgid) = ($main::cfg{'MTA_MAILBOX_UID_NAME'}, $main::cfg{'MTA_MAILBOX_GID_NAME'});
	my ($muid, $mgid) = ($main::cfg{'APACHE_SUEXEC_MIN_UID'}, $main::cfg{'APACHE_SUEXEC_MIN_GID'});
	my ($upref) = ($main::cfg{'APACHE_SUEXEC_USER_PREF'});
	my ($uid, $gid) = (undef, undef);

	my @udata = ();
	my @gdata = ();

	# MTA Mailbox User
	@udata = getpwnam($fuid);
	@gdata = getgrnam($fgid);

	if (scalar(@udata) != 0) { # we have not this one user data;
		my $cmd = "$main::cfg{'CMD_USERDEL'} $fuid";

		$rs = sys_command($cmd);
		if ($rs != 0) {
			print "cannot remove  user '$fuid'!\n\n";
			send_error_mail('uninstall_system_users()', "cannot remove  user '$fuid'!");
		}
	}

	# PHP Master
	@udata = getpwnam("$upref$muid");

	if (scalar(@udata) != 0) { # we have not this one user data;
		my $cmd = "$main::cfg{'CMD_USERDEL'} $upref$muid";

		$rs = sys_command($cmd);
		if ($rs != 0) {
			print "cannot remove  user '$fuid'!\n\n";
			send_error_mail('uninstall_system_users()', "cannot remove  user '$fuid'!");
		}
	}

	@gdata = getgrnam("$upref$mgid");

	if (scalar(@gdata) != 0) { # we have not this one group data;
		my $cmd = "$main::cfg{'CMD_GROUPDEL'} $upref$mgid";

		$rs = sys_command($cmd);
		if ($rs != 0) {;
			print "cannot remove  group '$upref$mgid'!\n\n";
			send_error_mail('uninstall_system_users()', "cannot remove  group '$upref$mgid'!");
		}
	}

	# Virtual User
	$sql = "select domain_uid,domain_gid from domain";

	($rs, $rows) = doSQL($sql);
	if ($rs != 0){
		print "cannot obtain user list!\n\n";
		send_error_mail('uninstall_system_users()', "cannot obtain user list!");
		return 0;
	}

	if (scalar(@$rows) != 0) {
		foreach (@$rows) {
			my $query_user = $_;
			@udata = getpwnam("$upref@$query_user[0]");

			if (scalar(@udata) != 0) { # we have not this one user data;
				my $cmd = "$main::cfg{'CMD_USERDEL'} $upref@$query_user[0]";

				$rs = sys_command($cmd);
				if ($rs != 0) {
					print "cannot remove  user '{$upref@$query_user[0]}'!\n\n";
					send_error_mail('uninstall_system_users()', "cannot remove  user '{$upref@$query_user[0]}'!");
				}
			}

			@gdata = getgrnam("$upref@$query_user[1]");

			if (scalar(@gdata) != 0) { # we have not this one group data;
				my $cmd = "$main::cfg{'CMD_GROUPDEL'} $upref@$query_user[0]";

				$rs = sys_command($cmd);
				if ($rs != 0) {
					print "cannot remove  group '{upref@$query_user[0]}'!\nYou must remove it manually\n";
					send_error_mail('uninstall_system_users()', "cannot remove  group '{$upref@$query_user[0]}'!");
				}
			}
		}
	}

	push_el(\@main::el, 'uninstall_system_users()', 'Ending...');
	return 0;

}

sub uninstall_system_dirs {

	my ($rs, $rdata) = (undef, undef);

	push_el(\@main::el, 'uninstall_system_dirs()', 'Starting...');

	if ($main::rm{'rm_u_data'} eq 'yes') {
		$rs = del_dir($main::cfg{'APACHE_WWW_DIR'});
		if ($rs != 0){
			print "cannot delete folder ".$main::cfg{'APACHE_WWW_DIR'}."!\n\n";
			send_error_mail('uninstall_system_dirs()', "cannot delete folder ".$main::cfg{'APACHE_WWW_DIR'}."!");
		}
	}

	$rs = del_dir($main::cfg{'APACHE_USERS_LOG_DIR'});
	if ($rs != 0){
		print "cannot delete folder ".$main::cfg{'APACHE_USERS_LOG_DIR'}."!\n\n";
		send_error_mail('uninstall_system_dirs()', "cannot delete folder ".$main::cfg{'APACHE_USERS_LOG_DIR'}."!");
	}

	$rs = del_dir($main::cfg{'APACHE_BACKUP_LOG_DIR'});
	if ($rs != 0){
		print "cannot delete folder ".$main::cfg{'APACHE_BACKUP_LOG_DIR'}."!\n\n";
		send_error_mail('uninstall_system_dirs()', "cannot delete folder ".$main::cfg{'APACHE_BACKUP_LOG_DIR'}."!");
	}

	$rs = del_dir($main::cfg{'MTA_VIRTUAL_CONF_DIR'});
	if ($rs != 0){
		print "cannot delete folder ".$main::cfg{'MTA_VIRTUAL_CONF_DIR'}."!\n\n";
		send_error_mail('uninstall_system_dirs()', "cannot delete folder ".$main::cfg{'MTA_VIRTUAL_CONF_DIR'}."!");
	}

	if ($main::rm{'rm_u_data'} eq 'yes') {
		$rs = del_dir($main::cfg{'MTA_VIRTUAL_MAIL_DIR'});
		if ($rs != 0){
			print "cannot delete folder ".$main::cfg{'MTA_VIRTUAL_MAIL_DIR'}."!\n\n";
			send_error_mail('uninstall_system_dirs()', "cannot delete folder ".$main::cfg{'MTA_VIRTUAL_MAIL_DIR'}."!");
		}
	}

	$rs = del_dir($main::cfg{'LOG_DIR'});
	if ($rs != 0){
		print "cannot delete folder ".$main::cfg{'LOG_DIR'}."!\n\n";
		send_error_mail('uninstall_system_dirs()', "cannot delete folder ".$main::cfg{'LOG_DIR'}."!");
	}

	$rs = del_dir($main::cfg{'PHP_STARTER_DIR'});
	if ($rs != 0){
		print "cannot delete folder ".$main::cfg{'PHP_STARTER_DIR'}."!\n\n";
		send_error_mail('uninstall_system_dirs()', "cannot delete folder ".$main::cfg{'PHP_STARTER_DIR'}."!");
	}

	push_el(\@main::el, 'uninstall_system_dirs()', 'Ending...');

	return 0;
}

sub uninstall_sql {

	my ($rs, $rdata) = (undef, undef);

	push_el(\@main::el, 'uninstall_sql()', 'Starting...');

	$rs = remove_sql_users();
	$rs = remove_sql_db();

	#
	# check for existing database;
	#

	my $sql = "show tables;";

	($rs, $rdata) = doSQL($sql);

	if ($rs == 0) { # Yes, we have one ! Let's drop it;

		my $store_db_name = $main::db_name;


		# Let's reset data;


		$main::db = undef;

		$main::db_name = '';

		@main::db_connect = (
							 "DBI:mysql:$main::db_name:$main::db_host",
							 $main::db_user,
							 $main::db_pwd
							);

		$sql = "drop database `$store_db_name`;";

		($rs, $rdata) = doSQL($sql);
		if ($rs != 0){
			print "cannot delete database $store_db_name!\n\n";
			send_error_mail('uninstall_sql()', "cannot delete database $store_db_name!");
		}


		# Let's reset data;


		$main::db = undef;

		$main::db_name = $store_db_name;

		@main::db_connect = (
							 "DBI:mysql:$main::db_name:$main::db_host",
							 $main::db_user,
							 $main::db_pwd
							);

	}

	push_el(\@main::el, 'uninstall_sql()', 'Ending...');

	return 0;

}

sub uninstall_crontab {

	my $rs = undef;

	push_el(\@main::el, 'uninstall_crontab()', 'Starting...');

	if ( -e "/etc/cron.d/ispcp") {
		$rs = del_file("/etc/cron.d/ispcp");
		if ($rs != 0){;
			print "cannot delete file /etc/cron.d/ispcp!\n\n";
			send_error_mail('uninstall_crontab()', "cannot delete file /etc/cron.d/ispcp!");
		}
	} else {
		print "File /etc/cron.d/ispcp do not exists!";
	}

	push_el(\@main::el, 'uninstall_crontab()', 'Ending...');

	return 0;

}

sub uninstall_resolver {

	push_el(\@main::el, 'uninstall_resolver()', 'Starting...');

	my $resolv_file = "/etc/resolv.conf";

	my ($rs, $cmd) = (undef, undef);

	if ( -e "$resolv_file.bkp" ) {

		$cmd = "$main::cfg{'CMD_MV'} $resolv_file.bkp $resolv_file";

		$rs = sys_command_rs($cmd);
		if ($rs != 0){
			print "cannot move file $resolv_file.bkp into $resolv_file!\n\n";
			send_error_mail('uninstall_resolver()', "cannot move file $resolv_file.bkp into $resolv_file!");
		}
	} else {
		print "File $resolv_file.bkp do not exits. File is not restored!\n\n";
	}

	push_el(\@main::el, 'uninstall_resolver()', 'Ending...');

	return 0;

}

sub uninstall_logrotate {

	push_el(\@main::el, 'uninstall_logrotate()', 'Starting...');

	my ($rs, $cmd) = (undef, undef);

	my $logrotate_d = "/etc/logrotate.d";

	if ( -e "$logrotate_d/ispcp" ) {

		$rs = del_file("$logrotate_d/ispcp");
		if ($rs != 0){
			print "cannot delete $logrotate_d/ispcp!\n\n";
			send_error_mail('uninstall_resolver()', "cannot delete $logrotate_d/ispcp!");
		}
	} else {
		print "File $logrotate_d/ispcp do not exits!\n\n";
	}

	push_el(\@main::el, 'uninstall_logrotate()', 'Ending...');

	return 0;

}

sub uninstall_named {

	my ($rs, $rdata) = (undef, undef);

	push_el(\@main::el, 'uninstall_named()', 'Starting...');

	my $cfg_dir = "$main::cfg{'CONF_DIR'}/bind";

	my $bk_dir = "$cfg_dir/backup";

	my $wrk_dir = "$cfg_dir/working";

	my ($cfg_tpl, $cfg, $cmd) = (undef, undef, undef);

	sys_command_rs("$main::cfg{'CMD_NAMED'} stop >> /tmp/ispcp-uninstall-services.log");

	if (-e "$bk_dir/named.conf.system") {

		$cmd = "$main::cfg{'CMD_CP'} -p $bk_dir/named.conf.system $main::cfg{'BIND_CONF_FILE'}";

		$rs = sys_command($cmd);
		if ($rs != 0){
			print "File $bk_dir/named.conf.system cannot be restored\n\n";
			send_error_mail('uninstall_named()', "File $bk_dir/named.conf.system cannot be restored");
		}

	} else {
		print "Backup files $bk_dir/named.conf.system cannot be founded\n";
		send_error_mail('uninstall_ftpd()', "Backup files $bk_dir/named.conf.system cannot be founded!");
	}

	sys_command_rs("$main::cfg{'CMD_NAMED'} start >> /tmp/ispcp-uninstall-services.log");

	push_el(\@main::el, 'uninstall_named()', 'Ending...');

	return 0;

}

sub uninstall_httpd {

	my ($rs, $rdata) = (undef, undef);

	push_el(\@main::el, 'uninstall_httpd()', 'Starting...');

	my $cfg_dir = "$main::cfg{'CONF_DIR'}/apache";

	my $bk_dir = "$cfg_dir/backup";

	my $wrk_dir = "$cfg_dir/working";

	my ($cfg_tpl, $cfg, $cmd) = (undef, undef, undef);

	sys_command_rs("$main::cfg{'CMD_HTTPD'} stop >> /tmp/ispcp-uninstall-services.log");

	sys_command_rs("a2dissite ispcp.conf >> /tmp/ispcp-uninstall-services.log");

	$rs = del_file("$main::cfg{'APACHE_SITES_DIR'}/ispcp.conf");
	if ($rs != 0){
		print "cannot delete $main::cfg{'APACHE_SITES_DIR'}/ispcp.conf!\n\n";
		send_error_mail('uninstall_httpd()', "cannot delete $main::cfg{'APACHE_SITES_DIR'}/ispcp.conf!");
	}

	sys_command_rs("a2dissite 00_master.conf >> /tmp/ispcp-uninstall-services.log");

	$rs = del_file("$main::cfg{'APACHE_SITES_DIR'}/00_master.conf");
	if ($rs != 0){
		print "cannot delete $main::cfg{'APACHE_SITES_DIR'}/00_master.conf!\n\n";
		send_error_mail('uninstall_httpd()', "cannot delete $main::cfg{'APACHE_SITES_DIR'}/00_master.conf!");
	}

	sys_command_rs("a2dismod fastcgi_ispcp >> /tmp/ispcp-uninstall-services.log");

	$rs = del_file("$main::cfg{'APACHE_MODS_DIR'}/fastcgi_ispcp.conf");
	if ($rs != 0){
		print "cannot delete $main::cfg{'APACHE_MODS_DIR'}/fastcgi_ispcp.conf!\n\n";
		send_error_mail('uninstall_httpd()', "cannot delete $main::cfg{'APACHE_MODS_DIR'}/fastcgi_ispcp.conf!");
	}

	sys_command_rs("a2dismod fcgid_ispcp >> /tmp/ispcp-uninstall-services.log");

	$rs = del_file("$main::cfg{'APACHE_MODS_DIR'}/fcgid_ispcp.conf");
	if ($rs != 0){
		print "cannot delete $main::cfg{'APACHE_MODS_DIR'}/fcgid_ispcp.conf!\n\n";
		send_error_mail('uninstall_httpd()', "cannot delete $main::cfg{'APACHE_MODS_DIR'}/fcgid_ispcp.conf!");
	}

	if ( -e "$main::cfg{'APACHE_MODS_DIR'}/fastcgi_ispcp.load") {
		$rs = del_file("$main::cfg{'APACHE_MODS_DIR'}/fastcgi_ispcp.load");
		if ($rs != 0){
			print "cannot delete $main::cfg{'APACHE_MODS_DIR'}/fastcgi_ispcp.load!\n\n";
			send_error_mail('uninstall_httpd()', "cannot delete $main::cfg{'APACHE_MODS_DIR'}/fastcgi_ispcp.load!");
		}
	} else {
		print "File $main::cfg{'APACHE_MODS_DIR'}/fastcgi_ispcp.load do not exits!\n\n";
	}

	if ( -e "$main::cfg{'APACHE_MODS_DIR'}/fcgid_ispcp.load") {
		$rs = del_file("$main::cfg{'APACHE_MODS_DIR'}/fcgid_ispcp.load");
		if ($rs != 0){
			print "cannot delete $main::cfg{'APACHE_MODS_DIR'}/fcgid_ispcp.load!\n\n";
			send_error_mail('uninstall_httpd()', "cannot delete $main::cfg{'APACHE_MODS_DIR'}/fcgid_ispcp.load!");
		}
	} else {
		print "File $main::cfg{'APACHE_MODS_DIR'}/fcgid_ispcp.load do not exits!\n\n";
	}

	sys_command_rs("a2dissite 01_awstats.conf >> /tmp/ispcp-uninstall-services.log");

	if ( -e "$main::cfg{'APACHE_SITES_DIR'}/01_awstats.conf") {
		$rs = del_file("$main::cfg{'APACHE_SITES_DIR'}/01_awstats.conf");
		if ($rs != 0){
			print "cannot delete $main::cfg{'APACHE_SITES_DIR'}/01_awstats.conf!\n\n";
			send_error_mail('uninstall_httpd()', "cannot delete $main::cfg{'APACHE_MODS_DIR'}/01_awstats.conf!");
		}
	} elsif($main::cfg{'APACHE_SITES_DIR'} eq 'yes' && $main::cfg{'AWSTATS_MODE'} == 0) {
		print "File $main::cfg{'APACHE_SITES_DIR'}/01_awstats.conf do not exits!\n\n";
	}

	$rs = del_dir($main::cfg{'APACHE_CUSTOM_SITES_CONFIG_DIR'});
	if ($rs != 0){
		print "Folder $main::cfg{'APACHE_CUSTOM_SITES_CONFIG_DIR'} cannot be deleted\n\n";
		send_error_mail('uninstall_httpd()', "Folder $main::cfg{'APACHE_CUSTOM_SITES_CONFIG_DIR'} cannot be deleted");
	}

	sleep(5);

	sys_command_rs("$main::cfg{'CMD_HTTPD'} start >> /tmp/ispcp-uninstall-services.log");

	push_el(\@main::el, 'uninstall_httpd()', 'Ending...');

	return 0;

}

sub uninstall_mta {

	my ($rs, $rdata) = (undef, undef);

	push_el(\@main::el, 'uninstall_mta()', 'Starting...');

	my $cfg_dir = "$main::cfg{'CONF_DIR'}/postfix";

	my $bk_dir = "$cfg_dir/backup";

	my $wrk_dir = "$cfg_dir/working";

	my $vrl_dir = "$cfg_dir/ispcp";

	my ($cfg_tpl, $cfg, $cmd) = (undef, undef, undef);

	sys_command_rs("$main::cfg{'CMD_MTA'} stop >> /tmp/ispcp-uninstall-services.log");

	if (-e "$bk_dir/main.cf.system") {

		$cmd = "$main::cfg{'CMD_CP'} -p $bk_dir/main.cf.system $main::cfg{'POSTFIX_CONF_FILE'}";

		$rs = sys_command($cmd);
		if ($rs != 0){
			print "cannot move $bk_dir/main.cf.system into $main::cfg{'POSTFIX_CONF_FILE'}!\n\n";
			send_error_mail('uninstall_mta()', "cannot move $bk_dir/main.cf.system into $main::cfg{'POSTFIX_CONF_FILE'}!");
		}

		$cmd = "$main::cfg{'CMD_CP'} -p $bk_dir/master.cf.system $main::cfg{'POSTFIX_MASTER_CONF_FILE'}";

		$rs = sys_command($cmd);
		if ($rs != 0){
			print "cannot move $bk_dir/master.cf.system into $main::cfg{'POSTFIX_MASTER_CONF_FILE'}!\n\n";
			send_error_mail('uninstall_mta()', "cannot move $bk_dir/master.cf.system into $main::cfg{'POSTFIX_MASTER_CONF_FILE'}!");
		}

	} else {
		print "Backup files for MTA cannot be founded\n";
		send_error_mail('uninstall_mta()', "Backup files for MTA cannot be founded!");
	}

	if ( -e "/var/spool/postfix/private/ispcp-arpl" ) {
		$rs = del_file("/var/spool/postfix/private/ispcp-arpl");
		if ($rs != 0){
			print "cannot delete /var/spool/postfix/private/ispcp-arpl!\n\n";
			send_error_mail('uninstall_mta()', "cannot delete /var/spool/postfix/private/ispcp-arpl!");
		}
	}

	if ( -e "/usr/sbin/maillogconvert.pl" ) {
		$rs = del_file("/usr/sbin/maillogconvert.pl");
		if ($rs != 0){
			print "cannot delete /usr/sbin/maillogconvert.pl!\n\n";
			send_error_mail('uninstall_mta()', "cannot delete /usr/sbin/maillogconvert.pl!");
		}
	}

	$rs = sys_command("$main::cfg{'CMD_NEWALIASES'} >> /tmp/ispcp-uninstall-services.log");
	if ($rs != 0){
		print "Command $main::cfg{'CMD_NEWALIASES'} failed! Check MTA config file\n\n";
		send_error_mail('uninstall_mta()', "Command $main::cfg{'CMD_NEWALIASES'} failed! Check MTA config file!");
	}

	sys_command_rs("$main::cfg{'CMD_MTA'} start >> /tmp/ispcp-uninstall-services.log");

	push_el(\@main::el, 'uninstall_mta()', 'Ending...');

	return 0;

}

sub uninstall_po {

	my ($rs, $rdata) = (undef, undef);

	push_el(\@main::el, 'uninstall_po()', 'Starting...');

	my $cfg_dir = "$main::cfg{'CONF_DIR'}/courier";

	my $bk_dir = "$cfg_dir/backup";

	my $wrk_dir = "$cfg_dir/working";

	my ($cfg_tpl, $cfg, $cmd) = (undef, undef, undef);

	sys_command_rs("$main::cfg{'CMD_AUTHD'} stop >> /tmp/ispcp-uninstall-services.log");

	sys_command_rs("$main::cfg{'CMD_IMAP'} stop >> /tmp/ispcp-uninstall-services.log");

	sys_command_rs("$main::cfg{'CMD_POP'} stop >> /tmp/ispcp-uninstall-services.log");

	$cmd = "$main::cfg{'CMD_CP'} -p $bk_dir/authdaemonrc.system $main::cfg{'AUTHLIB_CONF_DIR'}/authdaemonrc";
	$rs = sys_command($cmd);
	if ($rs != 0){
		print "File $bk_dir/authdaemonrc.system do not exist! cannot be restored\n\n";
		send_error_mail('uninstall_po()', "File $bk_dir/authdaemonrc.system do not exist! cannot be restored");
	}

	if (-e "$bk_dir/authmodulelist.system") {
		$cmd = "$main::cfg{'CMD_CP'} -p $bk_dir/authmodulelist.system $main::cfg{'AUTHLIB_CONF_DIR'}/authmodulelist";
		$rs = sys_command($cmd);
		if ($rs != 0){
			print "File $bk_dir/authmodulelist.system do not exist! cannot be restored\n\n";
			send_error_mail('uninstall_po()', "File $bk_dir/authmodulelist.system do not exist! cannot be restored");
		}
	}

	if (-e "$bk_dir/userdb.system") {
		$cmd = "$main::cfg{'CMD_CP'} -p $bk_dir/userdb.system $main::cfg{'AUTHLIB_CONF_DIR'}/userdb";
		$rs = sys_command($cmd);
	}

	if ($rs != 0){
		print "File $bk_dir/userdb.system do not exist! cannot be restored\n\n";
		send_error_mail('uninstall_po()', "File $bk_dir/userdb.system do not exist! cannot be restored");
	}

	$rs = sys_command($main::cfg{'CMD_MAKEUSERDB'});
	if ($rs != 0){
		print "Command $main::cfg{'CMD_MAKEUSERDB'} failed! Check config file\n\n";
		send_error_mail('uninstall_mta()', "Command $main::cfg{'CMD_MAKEUSERDB'} failed! Check config file!");
	}

	sys_command_rs("$main::cfg{'CMD_AUTHD'} start >> /tmp/ispcp-uninstall-services.log");

	sys_command_rs("$main::cfg{'CMD_IMAP'} start >> /tmp/ispcp-uninstall-services.log");

	sys_command_rs("$main::cfg{'CMD_POP'} start >> /tmp/ispcp-uninstall-services.log");

	push_el(\@main::el, 'uninstall_po()', 'Ending...');

	return 0;

}

sub uninstall_ftpd {

	my ($rs, $rdata) = (undef, undef);

	push_el(\@main::el, 'uninstall_ftpd()', 'Starting...');

	my $cfg_dir = "$main::cfg{'CONF_DIR'}/proftpd";

	my $bk_dir = "$cfg_dir/backup";

	my ($cfg_tpl, $cfg, $cmd) = (undef, undef, undef);

	sys_command_rs("$main::cfg{'CMD_FTPD'} stop >> /tmp/ispcp-uninstall-services.log");

	if (-e "$bk_dir/proftpd.conf.system") {

		$cmd = "$main::cfg{'CMD_CP'} -p $bk_dir/proftpd.conf.system $main::cfg{'FTPD_CONF_FILE'}";
		$rs = sys_command($cmd);
		if ($rs != 0){
			print "File $bk_dir/proftpd.conf.system cannot be copied\n\n";
			send_error_mail('uninstall_ftpd()', "File $bk_dir/proftpd.conf.system cannot be copied");
		}

		$rs = del_dir($main::cfg{'FTPD_CONF_DIR'});
		if ($rs != 0){
			print "Folder $main::cfg{'FTPD_CONF_DIR'} cannot be deleted\n\n";
			send_error_mail('uninstall_ftpd()', "Folder $main::cfg{'FTPD_CONF_DIR'} cannot be deleted");
		}

	} else {
		print "Backup files $bk_dir/proftpd.conf.system cannot be founded\n";
		send_error_mail('uninstall_ftpd()', "Backup files $bk_dir/proftpd.conf.system cannot be founded!");
	}

	sys_command_rs("$main::cfg{'CMD_FTPD'} start >> /tmp/ispcp-uninstall-services.log");

	push_el(\@main::el, 'uninstall_ftpd()', 'Ending...');

	return 0;

}

sub uninstall_ispcpd {

	my ($rs, $rdata) = (undef, undef);

	push_el(\@main::el, 'uninstall_ispcpd()', 'Starting...');

	sys_command_rs("$main::cfg{'CMD_ISPCPD'} stop >> /tmp/ispcp-uninstall-services.log");

	sys_command_rs("$main::cfg{'CMD_ISPCPN'} stop >> /tmp/ispcp-uninstall-services.log");

	if ( -x "/usr/lib/lsb/install_initd" ) { #LSB 3.1 Core section 20.4 compatibility

		sys_command_rs("/usr/lib/lsb/install_initd $main::cfg{'CMD_ISPCPD'} >> /tmp/ispcp-uninstall-services.log");
		sys_command_rs("/usr/lib/lsb/install_initd $main::cfg{'CMD_ISPCPN'} >> /tmp/ispcp-uninstall-services.log");

	}

	$rs = del_file("$main::cfg{'CMD_ISPCPD'}");
	if ($rs != 0){
		print "File $main::cfg{'CMD_ISPCPD'} cannot be deleted\n\n";
		send_error_mail('uninstall_ispcpd()', "File $main::cfg{'CMD_ISPCPD'} cannot be deleted");
	}

	$rs = del_file("$main::cfg{'CMD_ISPCPN'}");
	if ($rs != 0){
		print "File $main::cfg{'CMD_ISPCPN'} cannot be deleted\n\n";
		send_error_mail('uninstall_ispcpd()', "File $main::cfg{'CMD_ISPCPN'} cannot be deleted");
	}

	if ( ! -x "/usr/lib/lsb/install_initd" && -x "/usr/sbin/update-rc.d" ) {

		sys_command_rs("/usr/sbin/update-rc.d ispcp_daemon remove >> /tmp/ispcp-uninstall-services.log");
		sys_command_rs("/usr/sbin/update-rc.d ispcp_network remove >> /tmp/ispcp-uninstall-services.log");

	}

	push_el(\@main::el, 'uninstall_ispcpd()', 'Ending...');

	return 0;

}

sub uninstall_ispcp {

	my ($rs, $rdata) = (undef, undef);

	push_el(\@main::el, 'uninstall_ispcp()', 'Starting...');

	$rs = del_dir($main::cfg{'CONF_DIR'});
	if ($rs != 0){
		print "Folder $main::cfg{'CONF_DIR'} cannot be deleted\n\n";
		send_error_mail('uninstall_ispcp()', "Folder $main::cfg{'CONF_DIR'} cannot be deleted");
	}

	$rs = del_dir($main::cfg{'SCOREBOARDS_DIR'});
	if ($rs != 0){
		print "Folder $main::cfg{'SCOREBOARDS_DIR'} cannot be deleted\n\n";
		send_error_mail('uninstall_ispcp()', "Folder $main::cfg{'SCOREBOARDS_DIR'} cannot be deleted");
	}

	$rs = del_dir($main::cfg{'ROOT_DIR'});
	if ($rs != 0){
		print "Folder $main::cfg{'ROOT_DIR'} cannot be deleted\n\n";
		send_error_mail('uninstall_ispcp()', "Folder $main::cfg{'ROOT_DIR'} cannot be deleted");
	}


	push_el(\@main::el, 'uninstall_ispcp()', 'Ending...');

	return 0;

}

sub uninstall_host_system {

	my ($rs, $rdata) = (undef, undef);

	push_el(\@main::el, 'uninstall_host_system()', 'Starting...');

	$rs = uninstall_httpd();
	return $rs if ($rs != 0);

	$rs = uninstall_system_users();
	return $rs if ($rs != 0);

	$rs = uninstall_system_dirs();
	return $rs if ($rs != 0);

	$rs = uninstall_sql();
	return $rs if ($rs != 0);

	$rs = uninstall_crontab();
	return $rs if ($rs != 0);

	$rs = uninstall_logrotate();
	return $rs if ($rs != 0);

	$rs = uninstall_mta();
	return $rs if ($rs != 0);

	$rs = uninstall_po();
	return $rs if ($rs != 0);

	$rs = uninstall_ftpd();
	return $rs if ($rs != 0);

	$rs = uninstall_named();
	return $rs if ($rs != 0);

	$rs = uninstall_ispcpd();
	return $rs if ($rs != 0);

	$rs = uninstall_ispcp();
	return $rs if ($rs != 0);

	push_el(\@main::el, 'uninstall_host_system()', 'Ending...');

	return 0;

}

################################################################################
##                                  MAIN                                      ##
################################################################################

# Clear screen
system('clear');

my $rs = undef;

$rs = uninstall_startup();
if ($rs != 0) {

	my $el_data = pop_el(\@main::el);

	my ($sub_name, $msg) = split(/$main::el_sep/, $el_data);

	print STDERR "$msg\n";

	exit 1;
}

$rs = user_dialog();
if ($rs != 0) {

	my $el_data = pop_el(\@main::el);

	my ($sub_name, $msg) = split(/$main::el_sep/, $el_data);

	print STDERR "$msg\n";

	exit 1;
}

$rs = uninstall_host_system();
if ($rs != 0) {

	my $el_data = pop_el(\@main::el);

	my ($sub_name, $msg) = split(/$main::el_sep/, $el_data);

	print STDERR "$msg\n";

	exit 1;
}

$rs = uninstall_shutdown();
if ($rs != 0) {

	my $el_data = pop_el(\@main::el);

	my ($sub_name, $msg) = split(/$main::el_sep/, $el_data);

	print STDERR "$msg\n";

	exit 1;
}

=pod
 Documentation of the uninstaller's program flow:

 uninstall_startup()
	get_conf() .> ispcp-setup-methods.pl
	del_file() -> ispcp-setup-methods.pl
 user_dialog()
	welcome_note()
	ask_remove_user_data()
 uninstall_host_system()
	uninstall_system_users()
	uninstall_system_dirs()
	uninstall_sql()
	uninstall_crontab()
	uninstall_logrotate()
	uninstall_httpd()
	uninstall_mta()
	uninstall_po()
	uninstall_ftpd()
	uninstall_named()
	uninstall_ispcpd()
	uninstall_ispcp()
 uninstall_shutdown()
 =cut
