#!/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;

sub serv_mngr_start_up {
    
    my ($rs, $rdata) = (undef, undef);
    
    push_el(\@main::el, 'serv_mngr_start_up()', 'Starting...');
    
    # checking for master process;
    
    $rs = check_master();
    
    return $rs if ($rs != 0);
    
    # Let's clear Execution Logs, if any.
    
    if (-e $main::vhcs2_serv_mngr_el) {
        
        $rs = del_file($main::vhcs2_serv_mngr_el);
        
        return $rs if ($rs != 0);
        
    }
    
    # config check;
    
    $rs = get_conf();
    
    return $rs if ($rs != 0);
    
    # license request check;
    
    #($rs, $rdata) = license_request();
    
    #return $rs if ($rs != 0);
    
    #
    # License conditions must be stand here;
    #
    
    # sql check;
    
    #
    # getting initial data also must be done here;
    #
    
    my $sql = "select * from domain;";
    
    ($rs, $rdata) = doSQL($sql);
    
    return $rs if ($rs != 0);
    
    $main::changed_dmn_cnt = $ARGV[0]."";
    
    $main::changed_sub_cnt = $ARGV[1]."";
    
    $main::changed_als_cnt = $ARGV[2]."";
    
    $main::changed_mail_cnt = $ARGV[3]."";
    
    push_el(\@main::el, 'serv_mngr_start_up()', 'Ending...');
    
    return 0;
    
}

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

sub restart_httpd {
    
    my ($cmd, $rs, $rdata) = (undef, undef, undef);
    
    push_el(\@main::el, 'restart_httpd()', 'Starting...');
    
    $cmd = $main::cfg{'CMD_HTTPD'};
    
    sys_command_rs("$cmd stop");
    
    sleep(5);
    
    sys_command_rs("$cmd start");
    
    sleep(5);
    
    push_el(\@main::el, 'restart_httpd()', 'Ending...');
    
}

sub check_httpd {
    
    my ($cmd, $rs, $rdata) = (undef, undef, undef);
    
    push_el(\@main::el, 'check_httpd()', 'Starting...');
    
    $cmd = "export COLUMNS=120;$main::cfg{'CMD_PS'} aux | $main::cfg{'CMD_AWK'} '\$0 ~/$main::cfg{'APACHE_NAME'}/ {print \$0}' | $main::cfg{'CMD_WC'} | $main::cfg{'CMD_AWK'} '{print \$1}' 1>/tmp/httpd-run-check.stdout";
    
    $rs = sys_command($cmd);
    
    return ($rs, 0) if ($rs != 0);
    
    ($rs, $rdata) = get_file("/tmp/httpd-run-check.stdout");
    
    return ($rs, 0) if ($rs != 0);
    
    push_el(\@main::el, 'check_httpd()', 'Ending...');
    
    chop($rdata);
    
    my $pids = $rdata;
    
    ($rs, $rdata) = del_file("/tmp/httpd-run-check.stdout");
    
    return ($rs, 0) if ($rs != 0);
    
    return (0, $pids);
    
}

sub serv_mngr_engine {
    
    my ($cmd, $rs, $rdata) = (undef, undef, undef);
    
    push_el(\@main::el, 'serv_mngr_engine()', 'Starting...');
    
    
    if ($main::changed_mail_cnt > 0 || $main::changed_dmn_cnt > 0 || $main::changed_sub_cnt > 0) {
    
        if ($main::cfg{'CMD_MTA'} ne 'no') {
            
            $cmd = $main::cfg{'CMD_MTA'};
            
            sys_command_rs("$cmd stop");
        
        }
        
        if ($main::cfg{'CMD_AUTHD'} ne 'no') {
        
            $cmd = $main::cfg{'CMD_AUTHD'};
            
            sys_command_rs("$cmd stop");
            
        }

		if ($main::cfg{'CMD_AMAVIS'} ne 'no') {

			$cmd = $main::cfg{'CMD_AMAVIS'};

			sys_command_rs("$cmd stop");

		}
        
        
        if ($main::cfg{'CMD_IMAP'} ne 'no') {
    
            $cmd = $main::cfg{'CMD_IMAP'};
            
            sys_command_rs("$cmd stop");
            
        }
        
        if ($main::cfg{'CMD_POP'} ne 'no') {
            
            $cmd = $main::cfg{'CMD_POP'};
            
            sys_command_rs("$cmd stop");
        
        }
	
    }
    
    if ($main::changed_dmn_cnt > 0 || $main::changed_sub_cnt > 0 || $main::changed_als_cnt > 0) {
        
        if ($main::cfg{'CMD_NAMED'} ne 'no') {
            
            $cmd = $main::cfg{'CMD_NAMED'};
            
            sys_command_rs("$cmd restart");
            
        }
        
    }
    
    if ($main::changed_mail_cnt > 0 || $main::changed_dmn_cnt > 0 || 
        $main::changed_sub_cnt > 0 || $main::changed_als_cnt > 0) {
    
        if ($main::cfg{'CMD_MTA'} ne 'no') {
            
            $cmd = $main::cfg{'CMD_MTA'};
            
            sys_command_rs("$cmd start");
            
        }
        
        
        if ($main::cfg{'CMD_AUTHD'} ne 'no') {
            
            $cmd = $main::cfg{'CMD_AUTHD'};
            
            sys_command_rs("$cmd start");
            
        }

		if ($main::cfg{'CMD_AMAVIS'} ne 'no') {

		$cmd = $main::cfg{'CMD_AMAVIS'};

		sys_command_rs("$cmd start");

		}        
        
        if ($main::cfg{'CMD_IMAP'} ne 'no') {
        
            $cmd = $main::cfg{'CMD_IMAP'};
            
            sys_command_rs("$cmd start");
            
        }
        
        if ($main::cfg{'CMD_POP'} ne 'no') {
            
            $cmd = $main::cfg{'CMD_POP'};
            
            sys_command_rs("$cmd start");
            
        }
		
    }
    
    if ($main::changed_dmn_cnt > 0 || $main::changed_sub_cnt > 0 || $main::changed_als_cnt > 0) {
        
        if ($main::cfg{'CMD_HTTPD'} ne 'no') {
            
            $cmd = $main::cfg{'CMD_HTTPD'};
            
            sys_command_rs("$cmd restart");
            
            sleep(3);
            
        }
        
        if ($main::cfg{'CMD_HTTPD'} ne 'no') {
            
            ($rs, $rdata) = check_httpd();
        
            return $rs if ($rs != 0);
    
            if ($rdata == 0) {
      
                my $i = 1;
      
                do {
          
                    restart_httpd();
          
                    ($rs, $rdata) = check_httpd();
    
                    return $rs if ($rs != 0);
                
                    $i++;
          
                } while ($rdata == 0 && $i <= $main::cfg{'APACHE_RESTART_TRY'});
            }
            
        }
        
    }

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

my $rs = undef;

$rs = serv_mngr_start_up();

if ($rs != 0) {
    
    dump_el(\@main::el, $main::vhcs2_serv_mngr_el);
    
    serv_mngr_shut_down();
    
    exit 1;
    
}

$rs = serv_mngr_engine();

if ($rs != 0) {
    
    dump_el(\@main::el, $main::vhcs2_serv_mngr_el);
    
    serv_mngr_shut_down();
    
    exit 1;
    
}

$rs = serv_mngr_shut_down();

if ($rs != 0) {
    
    dump_el(\@main::el, $main::vhcs2_serv_mngr_el);
    
    exit 1;
    
}

exit 0;
