#!/bin/sh
# Generate dhcp file
# Arguments:
# 1) Dhcp output file
# 2) Interface (Should be up, Assumes we are the first IP on this subnet)
# 3) PXE-Image file (relative to tftproot) [usually Grub or Etherboot] 
# 4) Grub config file path (relative to tftproot)
# 5) Etherboot-Image bootfile (Passed via Etherboot) [usually udpcast]

if [ "$#" -ne 5 ]
then
        echo "Use Arguments:
	1) Dhcp output file
	2) Interface (Should be up, Assumes we are the first IP on this subnet)
	3) PXE-Image file (relative to tftproot) [usually Grub or Etherboot]
	4) Grub config file path (relative to tftproot)
	5) Etherboot-Image bootfile (Passed via Etherboot) [usually udpcast]"
        exit 1
fi

###Interface hasnt been implemented yet but:
###ifconfig eth0 | grep "inet addr" | awk?????

OUTPUT_DHCP=$1
PXE_CLIENT_FNAME=$3
GRUB_CONFIG=$4
ETHERBOOT_CLIENT_FNAME=$5

#######
# Start dhcp.conf output
#######
#######Obey all rules of printf (watch out for \ and % etc..)

printf "not authoritative;
ddns-update-style none;
allow bootp;
allow booting;
option grubfile code 150 = text;
dynamic-bootp-lease-length 600;

class \"pxeclients\" {
	match if substring(option vendor-class-identifier, 0, 9) = \"PXEClient\";
	filename \"%s\";
}
class \"etherboot\" {
	match if substring (option vendor-class-identifier, 0, 9) = \"Etherboot\";
	filename \"%s\";
	option vendor-encapsulated-options 3c:09:45:74:68:65:72:62:6f:6f:74:ff;
}

class \"grub\" {
	match if substring (option vendor-class-identifier, 0, 9) = \"pxegrub\";
	option grubfile \"%s\";
	option vendor-encapsulated-options 70:78:65:67:72:75:62:ff;
}

subnet 192.168.7.0 netmask 255.255.255.0 {
	pool {
   		allow members of \"pxeclients\";
   		range dynamic-bootp 192.168.7.2 192.168.7.80;
  	}
	pool {
   		allow members of \"etherboot\";
   		range dynamic-bootp 192.168.7.81 192.168.7.150;
  	}
	pool {
   		allow members of \"grub\";
   		range dynamic-bootp 192.168.7.151 192.168.7.254;
  	}
}" $PXE_CLIENT_FNAME $ETHERBOOT_CLIENT_FNAME $GRUB_CONFIG > $OUTPUT_DHCP


#######
# End dhcp.conf output
#######
