#!/usr/bin/perl

# VHCS(tm) - Virtual Hosting Control System
# Copyright (c) 2001-2004 by moleSoftware GmbH
# http://www.molesoftware.com
#
#
# License:
#    This program is free software; you can redistribute it and/or
#    modify it under the terms of the MPL Mozilla Public License
#    as published by the Free Software Foundation; either version 1.1    
#    of the License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
#    MPL Mozilla Public License for more details.
#    
#    You may have received a copy of the MPL Mozilla Public License
#    along with this program.
#    
#    An on-line copy of the MPL Mozilla Public License can be found
#    http://www.mozilla.org/MPL/MPL-1.1.html
#
#
# The VHCS Home Page is at:
#
#    http://www.vhcs.net



use FindBin;
use lib "$FindBin::Bin/..";
require 'vhcs2_common_code.pl';

use strict;

use warnings;

%main::ua = ();

sub welcome_note {
    
    my ($rs, $rdata) = (undef, undef);
    
    push_el(\@main::el, 'welcome_note()', 'Starting...');
    
    my $welcome_message = <<MSG;
    
    Welcome in VHCS2 '$main::cfg{'VersionH'}' Uninstall Program.
    
    This program will uninstall VHCS system from your server. 
    All domain users and their accounts will be removed.
    
    Please press 'Enter' to continue.
MSG
    
    print STDOUT $welcome_message;
    
    $rdata = readline(\*STDIN); chop($rdata);
    
    push_el(\@main::el, 'welcome_note()', 'Ending...');
    
    return 0;
    
}

sub user_dialog {
    
    my ($rs, $rdata) = (undef, undef);
    
    push_el(\@main::el, 'user_dialog()', 'Starting...');
    
    $rs = welcome_note();
    
    return $rs if ($rs != 0);
    
    return 0;
    
}

sub uninstall_start_up {
    
    my ($rs, $rdata) = (undef, undef);
    
    push_el(\@main::el, 'uninstall_start_up()', 'Starting...');
    
    # config check;
    
    $rs = get_conf();
    
    return $rs if ($rs != 0);
    
    push_el(\@main::el, 'uninstall_start_up()', 'Ending...');
    
    return 0;
    
}

sub uninstall_shut_down {
    
    my ($rs, $rdata) = (undef, undef);
    
    push_el(\@main::el, 'uninstall_shut_down()', 'Starting...');
    
    $rs = del_file("/tmp/vhcs2-uninstall-services.log");
    
    return $rs if ($rs != 0);
    
    my $shut_down_message = <<MSG;
    
    Congratulations !

    VHCS2 '$main::cfg{'VersionH'}' uninstall completed successfully !

    Thank you for using our product !

MSG

    print STDOUT $shut_down_message;
    
    push_el(\@main::el, 'uninstall_shut_down()', 'Ending...');
    
    return 0;
    
}

sub check_host_sql {
    
    push_el(\@main::el, 'check_host_sql()', 'Starting...');
    
    my ($rs, $rdata, $sql) = (undef, undef, undef);
    
    $sql = "show databases;";
    
    my $store_db_name = $main::db_name;
    
    $main::db_name = "";
    
    @main::db_connect = (
                         "DBI:mysql:$main::db_name:$main::db_host",
                         $main::db_user,
                         $main::db_pwd
                        );
    
    ($rs, $rdata) = doSQL($sql);
    
    return $rs if ($rs != 0);
    
    $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, 'check_host_sql()', 'Ending...');
    
    return 0;
}

sub check_host_system {
    
    my ($rs, $rdata) = (undef, undef);
    
    push_el(\@main::el, 'check_host_system()', 'Starting...');
    
    $rs = check_host_sql();
    
    return $rs if ($rs != 0);
    
    push_el(\@main::el, 'check_host_system()', 'Ending...');
    
    return 0;
    
}

sub uninstall_system_users {
    
    my ($rs, $rdata) = (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 ($uid, $gid) = (undef, undef);
    
    
    my @udata = ();
    
    my @gdata = ();
    
    
    
    @udata = getpwnam($fuid);
    
    if (scalar(@udata) != 0) { # we have not this one user data;
        
        my $cmd = "$main::cfg{'CMD_USERDEL'} $fuid";
        
        $rs = sys_command($cmd);
        
        return $rs if ($rs != 0);
        
    }
    
    @gdata = getgrnam($fuid);
    
    if (scalar(@gdata) != 0) { # we have not this one group data;
        
        my $cmd = "$main::cfg{'CMD_GROUPDEL'} $fuid";
        
        $rs = sys_command($cmd);
        
        return $rs if ($rs != 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...');
    
    $rs = del_dir($main::cfg{'APACHE_WWW_DIR'});
    
    return $rs if ($rs != 0);
    
    $rs = del_dir($main::cfg{'APACHE_USERS_LOG_DIR'});
    
    return $rs if ($rs != 0);
    
    $rs = del_dir($main::cfg{'APACHE_BACKUP_LOG_DIR'});
    
    return $rs if ($rs != 0);
    
    $rs = del_dir($main::cfg{'MTA_VIRTUAL_CONF_DIR'});
    
    return $rs if ($rs != 0);
    
    $rs = del_dir($main::cfg{'MTA_VIRTUAL_MAIL_DIR'});
    
    return $rs if ($rs != 0);
    
    $rs = del_dir($main::cfg{'LOG_DIR'});
    
    return $rs if ($rs != 0);
    
    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...');
    
    #
    # 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);
        
        return $rs if ($rs != 0);
        
       
        # 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, $rdata) = (undef, undef);
    
    push_el(\@main::el, 'uninstall_crontab()', 'Starting...');
    
    my $cfg_dir = "$main::cfg{'CONF_DIR'}/crontab";
    
    my $bk_dir = "$cfg_dir/backup";
    
    my $wrk_dir = "$cfg_dir/working";
    
    my ($cfg_tpl, $cfg, $cmd) = (undef, undef, undef);
    
    
    $cmd = "$main::cfg{'CMD_CRONTAB'} -u root -r &> /tmp/vhcs2-uninstall-crontab.log";
        
    $rs = sys_command_rs($cmd);
    
    $rs = del_file("/tmp/vhcs2-uninstall-crontab.log");
        
    return $rs if ($rs != 0);
    
    if (-e "$bk_dir/crontab.conf.system") { # We are running uninstall for the first time.
        
        ($rs, $rdata) = get_file("$bk_dir/crontab.conf.system");
        
        return $rs if ($rs != 0);
        
        if ($rdata !~ /no crontab/) {

            $cmd = "$main::cfg{'CMD_CRONTAB'} -u root $bk_dir/crontab.conf.system";

            $rs = sys_command_rs($cmd);

        }

        $rs = del_file("$bk_dir/crontab.conf.system");
        
        return $rs if ($rs != 0);
        
    }
    
    push_el(\@main::el, 'uninstall_crontab()', '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/vhcs2-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);
        
        return $rs if ($rs != 0);
        
        $rs = del_file("$bk_dir/named.conf.system");
        
        return $rs if ($rs != 0);
        
        $rs = del_file("$bk_dir/named.conf.vhcs2");
        
        return $rs if ($rs != 0);
        
        
    }
    
    sys_command_rs("$main::cfg{'CMD_NAMED'} start &> /tmp/vhcs2-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/vhcs2-uninstall-services.log");

    $rs = del_file($main::cfg{'APACHE_CONF_FILE'});
        
    return $rs if ($rs != 0);
    
    sleep(5);
    
    sys_command_rs("$main::cfg{'CMD_HTTPD'} start &> /tmp/vhcs2-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/vhcs2";
    
    my ($cfg_tpl, $cfg, $cmd) = (undef, undef, undef);
    
    sys_command_rs("$main::cfg{'CMD_MTA'} stop &> /tmp/vhcs2-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);
        
        return $rs if ($rs != 0);
        
        $cmd = "$main::cfg{'CMD_CP'} -p $bk_dir/master.cf.system $main::cfg{'POSTFIX_MASTER_CONF_FILE'}";
        
        $rs = sys_command($cmd);
        
        return $rs if ($rs != 0);
        
        $rs = del_file("$bk_dir/main.cf.system");
        
        return $rs if ($rs != 0);
        
        $rs = del_file("$bk_dir/master.cf.system");
        
        return $rs if ($rs != 0);
        
        $rs = del_file("$bk_dir/main.cf.vhcs2");
        
        return $rs if ($rs != 0);
        
        $rs = del_file("$bk_dir/master.cf.vhcs2");
        
        return $rs if ($rs != 0);
        
    }
    
    $rs = sys_command("$main::cfg{'CMD_NEWALIASES'} &> /tmp/vhcs2-uninstall-services.log");
        
    return $rs if ($rs != 0);
    
    sys_command_rs("$main::cfg{'CMD_MTA'} start &> /tmp/vhcs2-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/vhcs2-uninstall-services.log");
    
    sys_command_rs("$main::cfg{'CMD_IMAP'} stop &> /tmp/vhcs2-uninstall-services.log");
    
    sys_command_rs("$main::cfg{'CMD_POP'} stop &> /tmp/vhcs2-uninstall-services.log");
    
    if (-e "$bk_dir/imapd.system") {
        
       
        # Let's backup system configs;
       
        
        $cmd = "$main::cfg{'CMD_CP'} -p $bk_dir/imapd.system $main::cfg{'COURIER_CONF_DIR'}/imapd";
        
        $rs = sys_command($cmd);
        
        return $rs if ($rs != 0);
        
        $cmd = "$main::cfg{'CMD_CP'} -p $bk_dir/pop3d.system $main::cfg{'COURIER_CONF_DIR'}/pop3d";
        
        $rs = sys_command($cmd);
        
        return $rs if ($rs != 0);
       
	if (exists $main::cfg{'AUTHLIB_CONF_DIR'} && $main::cfg{'AUTHLIB_CONF_DIR'}) {
 
        $cmd = "$main::cfg{'CMD_CP'} -p $bk_dir/authdaemonrc.system $main::cfg{'AUTHLIB_CONF_DIR'}/authdaemonrc";
        
        $rs = sys_command($cmd);
        
        return $rs if ($rs != 0);
        
        $cmd = "$main::cfg{'CMD_CP'} -p $bk_dir/authmodulelist.system $main::cfg{'AUTHLIB_CONF_DIR'}/authmodulelist";
        
        $rs = sys_command($cmd);
        
        return $rs if ($rs != 0);
        
	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);
	    
	}
        
	} else {

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

        $rs = sys_command($cmd);

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

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

        $rs = sys_command($cmd);

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

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

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

            $rs = sys_command($cmd);

        }

	}

        return $rs if ($rs != 0);
        
        $rs = del_file("$bk_dir/imapd.system");
        
        return $rs if ($rs != 0);
        
        $rs = del_file("$bk_dir/pop3d.system");
        
        return $rs if ($rs != 0);
        
        $rs = del_file("$bk_dir/authdaemonrc.system");
        
        return $rs if ($rs != 0);
        
        $rs = del_file("$bk_dir/authmodulelist.system");
        
        return $rs if ($rs != 0);
        
	if (-e "$bk_dir/userdb.system") {
	
    	    $rs = del_file("$bk_dir/userdb.system");
        
    	    return $rs if ($rs != 0);
	    
	}
        
        $rs = del_file("$bk_dir/imapd.vhcs2");
        
        return $rs if ($rs != 0);
        
        $rs = del_file("$bk_dir/pop3d.vhcs2");
        
        return $rs if ($rs != 0);
        
        $rs = del_file("$bk_dir/authdaemonrc.vhcs2");
        
        return $rs if ($rs != 0);
        
        $rs = del_file("$bk_dir/authmodulelist.vhcs2");
        
        return $rs if ($rs != 0);
        
    }
    
    $rs = sys_command($main::cfg{'CMD_MAKEUSERDB'});
        
    return $rs if ($rs != 0);
    
    sys_command_rs("$main::cfg{'CMD_AUTHD'} start &> /tmp/vhcs2-uninstall-services.log");
    
    sys_command_rs("$main::cfg{'CMD_IMAP'} start &> /tmp/vhcs2-uninstall-services.log");
    
    sys_command_rs("$main::cfg{'CMD_POP'} start &> /tmp/vhcs2-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/vhcs2-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);
        
        return $rs if ($rs != 0);
        
        $rs = del_file("$bk_dir/proftpd.conf.system");
        
        return $rs if ($rs != 0);
        
        $rs = del_file("$bk_dir/proftpd.conf.vhcs2");
        
        return $rs if ($rs != 0);

    }
    
    sys_command_rs("$main::cfg{'CMD_FTPD'} start &> /tmp/vhcs2-uninstall-services.log");
    
    push_el(\@main::el, 'uninstall_ftpd()', 'Ending...');
    
    return 0;
    
}

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

    push_el(\@main::el, 'uninstall_vhcs2d()', 'Starting...');
    
    sys_command_rs("$main::cfg{'CMD_VHCS2D'} stop &> /tmp/vhcs2-uninstall-services.log");
    
    push_el(\@main::el, 'uninstall_vhcs2d()', 'Ending...');
    
    return 0;
    
}

sub uninstall_host_system {
    
    my ($rs, $rdata) = (undef, undef);
    
    push_el(\@main::el, 'uninstall_host_system()', 'Starting...');
    
    $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_httpd();
    
    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_vhcs2d();
    
    return $rs if ($rs != 0);
    
    push_el(\@main::el, 'uninstall_host_system()', 'Ending...');
    
    return 0;
    
}

my $rs = undef;

$rs = uninstall_start_up();

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 = check_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_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_shut_down();

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;

}

