#!/usr/bin/perl

use CGI;
use DBI;
##use strict;
unshift(@INC, '.');
use ipac_cfg;

my $q = new CGI;
my $path="https://" . $q->server_name();
my $cgi=$path . "/cgi-bin";
my $cust_id = "0";
my $net_id;
my $ip;
my $tar_name;
my $dbh;
my $sth;
my @row;

my $user = $ENV{REMOTE_USER};

if ($ENV{HTTPS} ne "on" || $user ne "admin") {
    print $q->header(-type=>"text/html;", -status=>'403 Forbidden'),
	    $q->start_html('Forbidden'),
	    $q->h1('You dont have permission to access this script'),
	    $q->end_html;
    exit;
}

$net_id = $q->param('net');
$ip = $q->param('ip');

if ($q->param('new_ip') ne "") {
	print "Location: $cgi/br_net?net=$net_id\n\n" ;
}

print  $q->header,
       $q->start_html(-title=>'Edit host/network definition');

print "<table><tr><td>".
	"<IMG BORDER=0 ALT=\"ipac-ng logo\"".
	"SRC=\"$path/poweredby.png\"></td><td>".
	"<center><font color=\"#ff0000\" size=6>Ipac-ng change host/network definiton</font></center>".
	"</td></tr></table><A HREF=\"http://sf.net/projects/ipac-ng\">".
    	"ipac-ng home page</A>&nbsp;&nbsp;";
print "<A HREF=\"$path/stat\">Go to main page</A>&nbsp;&nbsp;";
print $ipac_cfg::menu;

my $dbh = DBI->connect("dbi:Pg:dbname=$ipac_cfg::pg_dbname",       
                        $ipac_cfg::pg_login, $ipac_cfg::pg_pwd,    
	                             { RaiseError => 1, AutoCommit => 0 });

print "<table align=center CELLPADDING=2 CELLSPACING=1 BORDER=0>";
print "<tr>", $q->td(['host/network', '&nbsp']);print "</tr>";

print $q->start_form;
print "<tr>";
print $q->td([
	    $q->textfield('new_ip', $ip, 20, 60, -override=>1), "&nbsp;&nbsp;",
	    $q->submit()]),
	$q->hidden('net', $net_id),
	$q->hidden('ip', $ip);
print "</tr></table>";
print $q->endform();

$new_ip=$q->param('new_ip');

if ($new_ip eq "") {
	print $q->end_html;
	$dbh->disconnect or warn $dbh->errstr;
	exit;
}

$dbh->do(q{update nets set ip_net=?
	    where net_id=? and ip_net=?}, undef, $new_ip, $net_id, $ip)
				    or die $dbh->errstr;
$dbh->commit();
$dbh->disconnect  or warn $dbh->errstr;
reconfig();
print $q->end_html;
