#!/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 $serv_id = "0";
my $tar_id = "0";
my $net_id;
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;
}

if (($q->param('ser_id') ne "") && ($q->param('tar_id') ne "")) {
	$serv_id = $q->param('ser_id');
	$tar_id = $q->param('tar_id');
	$net_id = $q->param('net_id');
} else {
	exit;
}

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

$dbh->do(q{delete from services where serv_id=? and net_id=? and tariff_id=?},
			undef, $serv_id, $net_id, $tar_id) or die $dbh->errstr;
$sth=$dbh->prepare("select serv_id from services where serv_id=?");
$sth->execute($serv_id) or die $sth->errstr;
@row=$sth->fetchrow_array;
if ($row[0] eq "") {
        $dbh->do(q{delete from services_names where serv_id=?}, undef, $serv_id);
}	
$sth->finish;		
$dbh->commit();
$dbh->disconnect  or warn $dbh->errstr;
reconfig();
print $q->header(-Refresh=>"1; URL=$cgi/br_services");
print $q->end_html;
