#!/usr/bin/perl
## dhcpd.pl < file>
# example: dhcpd.pl mac_address.txt host-decl-names nbi_name
# where mac_address.txt contains mac address like this:
# 00:10:5a:5c:51:83
# 00:00:e2:54:24:8a
# example: ./dhcpd.pl nchc_c_rtl.txt node vmlinuz.etherboot
#
# Version 0.1, working version. 
# by c00hkl00@nchc.gov.tw, 2002/June

# Version 0.2, add some descriptions.
# by jhshiau@nchc.gov.tw, 2002/8/22

if($#ARGV!=3) {
  print "Usage: dhcpd.pl mac_address.txt host-decl-names nbi_name nisdomain\n";
  print "Ex: ./dhcpd.pl nchc_c_rtl.txt node vmlinuz.etherboot drbl\n";
  exit;
}

open(DHCP_OUT,">dhcpd.conf") || die "cannot open dhcpd.conf for writing";
open(HOST_OUT,">hosts") || die "cannot open hosts for writing";
open(MAC_IN, $ARGV[0]) || die "cannot open $1 for reading";
$Hostname=$ARGV[1];
$NBI=$ARGV[2];
$Nisdomain=$ARGV[3];

print DHCP_OUT <<EOF;
default-lease-time			21600;
max-lease-time				21600;
use-host-decl-names			on;

option subnet-mask			255.255.255.0;
subnet 192.168.0.0 netmask 255.255.255.0 {
	option broadcast-address	192.168.0.255;
	option routers				192.168.0.254;
	option domain-name-servers	192.168.0.254;

EOF

$host=1;
while(<MAC_IN>) {
  chop;
  $label="$host";
  if($host<10) { $label="00$host"; }
  elsif($host<100) { $label="0$host"; }
  else {} 
  print DHCP_OUT <<EOF;
	host $Hostname$label {
		hardware ethernet   $_;
		fixed-address		192.168.0.$host;
		filename			"$NBI";
    }

EOF
  print HOST_OUT <<EOF;
192.168.0.$host	node$label.$Nisdomain.org  node$label
EOF

  $host++;
}

print DHCP_OUT <<EOF;
}
EOF

print "Done!!! output files are dhcpd.conf and hosts !\n";
print "Now 
1. copy the dhcpd.conf and hosts to /etc , 
2. set the \"DHCPDARGS=ethx\" (change ethx to what you have, like eth1) in /etc/sysconfig/dhcpd to fit the network card attached to dhcpd server.
3. start or restart the dhcpd service like: service dhcpd start\n";


close(DHCP_OUT);
close(HOST_OUT);
close(MAC_IN);
