Index: trunk/cgi-bin/allocate.pl
===================================================================
--- trunk/cgi-bin/allocate.pl	(revision 894)
+++ trunk/cgi-bin/allocate.pl	(revision 895)
@@ -45,43 +45,32 @@
 # Hokay, now we can start to handle the allocation.
 
-my ($cidr, $type, $custid, $city, $desc, $alloc_from);
+my ($cidr, $type, $vrf, $custid, $city, $desc) = @ARGV;
+my ($fbid, $fbparent);
 # Check ARGV.  We need some information to determine what to allocate.
-if (!$ARGV[1]) {
+if (!defined($vrf)) {
   # Usage message
-  print "Usage:  allocate.pl [IP/subnet] [Type] [CustID] [City] [\"Description\"]\n".
+  print "Usage:  allocate.pl <IP/subnet> <Type> <VRF> [CustID] [City] [\"Description\"]\n".
+	"	IP/subnet, Type, and VRF are required\n".
 	"	Further information can be entered via the web interface\n";
   exit;
 } else {
-  $cidr = new NetAddr::IP "$ARGV[0]";
-  $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr >>='$cidr'");
-  $sth->execute;
-  my @data = $sth->fetchrow_array;
-# User deserves errors if user can't be bothered to find the free block first.
-  die "Can't allocate from outside a free block!!\n"
-	if !$data[0];
-  $alloc_from = new NetAddr::IP $data[0];
-  $sth->finish;
-  $type = $ARGV[1];
-  if (!$ARGV[4]) {
+
+  $cidr = new NetAddr::IP $cidr;
+  ($fbid,$fbparent) = $ip_dbh->selectrow_array(
+	"SELECT id,parent_id FROM freeblocks WHERE cidr >>= ? AND vrf = ?", undef, "$cidr", $vrf)
+	or die "Couldn't find a free block to match '$cidr' in $vrf\n";
+  if (!$desc) {
     # Default desc
     $desc = "DEFAULT: $disp_alloctypes{$type}";
-  } else {
-    $desc = $ARGV[4];
   }
-  if (!$ARGV[3]) {
-    # Default city
-    $sth = $ip_dbh->prepare("select city from routed where cidr >>='$cidr'");
-    $sth->execute;
-    my @data = $sth->fetchrow_array;
-    $city = $data[0];
-    $sth->finish;
-  } else {
-    $city = $ARGV[3];
+  if (!$city) {
+    # Default city taken from parent allocation
+    ($city) = $ip_dbh->selectrow_array("SELECT city FROM allocations WHERE id = ?", undef, $fbparent);
   }
-  if (!$ARGV[2]) {
-    # Default custid - make it REAL obvious.
-    $custid = "FIXME";
-  } else {
-    $custid = $ARGV[2];
+  if (!$custid) {
+    # See if the type has a default custID....
+    $custid = $ip_dbh->selectrow_array("SELECT def_custid FROM alloctypes WHERE type = ?", undef, $type);
+    # ... and if not, make it REAL obvious this needs to be fixed.
+    $custid = "FIXME" if !$custid;
   }
 }
@@ -89,6 +78,17 @@
 print "Allocating $cidr as $type to $custid in $city: '$desc'\n";
 
-my ($code,$msg) = allocateBlock($ip_dbh, $cidr, $alloc_from, $custid, $type, $city,
-	$desc, '', '');
+my %insert_args = (
+        cidr            => "$cidr",
+        fbid            => $fbid,
+        parent          => $fbparent,
+        custid          => $custid,
+        type            => $type,
+        city            => $city,
+        desc            => $desc,
+        vrf             => $vrf,
+        user            => $authuser,
+);
+
+my ($code,$msg) = allocateBlock($ip_dbh, %insert_args);
 
 if ($code eq 'OK') {
