#!/usr/bin/perl
# ipdb/cgi-bin/newnode.cgi
# Add new wifi tower or fibre demarc switch ("node") to database
###
# SVN revision info
# $Date: 2010-06-30 21:48:03 +0000 (Wed, 30 Jun 2010) $
# SVN revision $Rev: 417 $
# Last update by $Author: kdeugau $
###
# Copyright (C) 2010 - Kris Deugau

use strict;
use warnings;
#use CGI::Carp qw(fatalsToBrowser);
use DBI;
use CommonWeb qw(:ALL);
#use POSIX qw(ceil);
use NetAddr::IP;
use Sys::Syslog;

# don't remove!  required for GNU/FHS-ish install from tarball
##uselib##

use MyIPDB;

openlog "IPDB","pid","$IPDB::syslog_facility";

# Collect the username from HTTP auth.  If undefined, we're in a test environment.
my $authuser;
if (!defined($ENV{'REMOTE_USER'})) {
  $authuser = '__temptest';
} else {
  $authuser = $ENV{'REMOTE_USER'};
}

my %webvar = parse_post();
cleanInput(\%webvar);

my ($dbh,$errstr) = connectDB_My;
my $sth;

print "Content-type: text/html\n\n";

$sth = $dbh->prepare("insert into nodes (node_type,node_name,node_ip)".
	" values ('$webvar{type}','$webvar{nodename}','$webvar{nodeip}')");
$sth->execute;

if ($sth->err) {
  print "Error adding node to database: ".$sth->errstr;
  mailNotify($dbh, 'f:nno', "IPDB node add failure",
	"$authuser could not add node '$webvar{nodename}','$webvar{type}' to database: ".$sth->errstr);
  syslog "err", "$authuser could not add node '$webvar{nodename}','$webvar{type}' to database: ".$sth->errstr;
} else {
  print "Node added.  Closing this window should refresh the page.";
  syslog "notice", "$authuser added node '$webvar{nodename}'";
}

finish($dbh);
