#! /usr/bin/perl
#
# Program to generate DHCP information from DNS A and TXT records
# The TXT records have the ethernet address.
#
# $Log: dhcpgen,v $
# Revision 1.4  1999/04/02 05:23:56  david
# New Hosts file format.
#
# Revision 1.3  1997/04/09 05:13:51  david
# Fixed to only output without a comment when both name and ip address are defined.
#
# Revision 1.2  1997/04/09 05:10:31  david
# Fixed typo.  Needed ';'.
#
# Revision 1.1  1997/04/06 17:49:55  david
# Initial revision
#
#

if ( ($#ARGV + 1) != 2 )
   {
   die "Usage: $0 dns_server domain\n";
   }

open( DNS, "dig $ARGV[0] $ARGV[1] axfr|" ) || die "Can't start dig program.";
open( IN, "</etc/dhcpd.conf.header" ) || die "Can't open /etc/dhcpd.conf.header";

while ( <DNS> )
   {
   if ( substr( $_, 0, 1 ) =~ /\s/ )
      {
      ($ttl, $in, $op, $data) = split;
      }
    else # No space
      {
      ($name, $ttl, $in, $op, $data) = split;
      }
   
   if ( $op eq "TXT" )
      {
#      print STDERR "DEBUG: Name = $name, Op = $op, Data = $data\n";
      $_ = $name; s/.jonesnet.org.//; $name = $_;
      $dname{$name} = $name;
      $_ = $data; s/"//g; $data = $_;
      $ether{$name} = $data;
      }

   if ( $op eq "A" )
      {
#      print STDERR "DEBUG: Name = $name, Op = $op, Data = $data\n";
      $_ = $name; s/.jonesnet.org.//; $name = $_;
      $dname{$name} = $name;
      $ipaddr{$name} = $data;
      }
   }

close( DNS );

$host = `hostname`; chop $host;

print "server-identifier $host;\n";
print "\n";

while ( <IN> )
   {
   print;
   }

close( IN );

print "\n";

foreach $n (sort keys %dname)
   {
   print "# " unless $ether{$n} and $ipaddr{$n};
   print "host  $n \{ hardware ethernet $ether{$n}; fixed-address $ipaddr{$n}; \}\n";
   }

exit 0;

