#!/usr/bin/perl

# ispCP ω (OMEGA) a Virtual Hosting Control Panel
# Copyright (C) 2006-2010 by isp Control Panel - http://ispcp.net
#
# Version: $Id: ispcp-net-interfaces-mngr 3662 2010-11-23 00:27:15Z benedikt $
#
# 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 "ispCP ω (OMEGA) a Virtual Hosting Control Panel".
#
# The Initial Developer of the Original Code is ispCP Team.
# Portions created by Initial Developer 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/..";
require 'ispcp_common_code.pl';

use strict;
use warnings;

my $rs = undef;
our $proceed = $ARGV[0];

sub net_interfaces_mngr_start_up {

	my $ref = undef;

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

	umask(027);

	if (-e $main::ispcp_net_interfaces_mngr_el) {
		$rs = del_file($main::ispcp_net_interfaces_mngr_el);
		return $rs if ($rs != 0);
	}

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

	my $sql = "
		SELECT
			`value`
		FROM
			`config`
		WHERE
			`name` = 'DATABASE_REVISION'
		;
	";

	($rs, $ref) = doSQL($sql);
	return $rs if ($rs != 0);

	my $version = @$ref[0];

	if (@$version[0] < 18){
		$proceed = '';
	}

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

	0;
}

sub virtual_netcard_add(){

	my $ref = undef;

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

	my $sql = "
		SELECT
			`ip_id`, `ip_number`, `ip_card`
		FROM
			`server_ips`
		WHERE
			`ip_number` != '$main::cfg{'BASE_SERVER_IP'}'
		;
	";

	($rs, $ref) = doSQL($sql);
	return $rs if ($rs != 0);

	my $server_ips = $ref;

	foreach (@$server_ips) {

		my $ip_id = @$_[0];
		my $ip_number = @$_[1];
		my $ip_card = @$_[2];

		if (defined($ip_card)){
			push_el(
				\@main::el, 'virtual_netcard_add()',
				"$main::cfg{'CMD_IFCONFIG'} ${ip_card}:${ip_id} ${ip_number} " .
				"netmask 255.255.255.255 up"
			);

            $rs = sys_command(
                "$main::cfg{'CMD_IFCONFIG'} ${ip_card}:${ip_id} ${ip_number} " .
                "netmask 255.255.255.255 up"
			);

			if ($rs != 0){
				send_error_mail(
					'virtual_netcard_add()',
					"Error while trying to add IP ${ip_number} to network " .
					"card '${ip_card}'!"
				)
			}
		}
	}

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

	0;
}

sub virtual_netcard_remove(){

	my $ref = undef;

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

	my $sql = "
		SELECT
			`ip_id`, `ip_number`, `ip_card`
		FROM
			`server_ips`
		WHERE
			`ip_number` != '$main::cfg{'BASE_SERVER_IP'}'
		AND
			`ip_status` != 'toadd'
		;
	";

	($rs, $ref) = doSQL($sql);
	return $rs if ($rs != 0);

	my $server_ips = $ref;

	foreach (@$server_ips) {

		my $ip_id = @$_[0];
		my $ip_number = @$_[1];
		my $ip_card = @$_[2];

		push_el(
			\@main::el, 'virtual_netcard_remove()',
			"$main::cfg{'CMD_IFCONFIG'} ${ip_card}:${ip_id} down"
		);

		$rs = sys_command(
			"$main::cfg{'CMD_IFCONFIG'} ${ip_card}:${ip_id} down"
		);

		send_error_mail(
			'virtual_netcard_remove()',
			"Error while trying to remove IP ${ip_number} to network " .
			"card '${ip_card}'!") if ($rs != 0);
	}

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

	0;
}

sub net_interfaces_mngr_engine {

	my $ref = undef;

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

	if ($proceed eq 'start') {
		$rs = virtual_netcard_add();
		return $rs if ($rs != 0);
	}
	if ($proceed eq 'stop') {
		$rs = virtual_netcard_remove();
		return $rs if ($rs != 0);
	}

	my $sql = "
		DELETE FROM
			`server_ips`
		WHERE
			`ip_status` = 'delete'
		;
	";

	($rs, $ref) = doSQL($sql);
	return $rs if ($rs != 0);

	$sql = "
		UPDATE
			`server_ips`
		SET
			`ip_status` = 'ok'
		;
	";

	($rs, $ref) = doSQL($sql);
	return $rs if ($rs != 0);

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

	0;
}

sub net_interfaces_mngr_shut_down {

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

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

	0;
}

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

	dump_el(\@main::el, $main::ispcp_net_interfaces_mngr_el);
	net_interfaces_mngr_shut_down();
	exit 1;
}


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

	dump_el(\@main::el, $main::ispcp_net_interfaces_mngr_el);
	net_interfaces_mngr_shut_down();
	exit 1;
}

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

exit 0;
