#!/bin/sh

# dnsmasq, a small dhcp server / dns cache
# http://www.thekelleys.org.uk/dnsmasq/
# http://etherboot.org/wiki/dnsmasq

[ -z "$1" ] && echo "usage $0 <iface> <network xx.yy.zz>" && exit 1

IF=$1
NET=$2
ME=$NET.254

echo 1 >/proc/sys/net/ipv4/ip_forward 


# need to masquerade output: this should be configured somewhere else
# iptables -t nat -A POSTROUTING -o $WORLDIF -j MASQUERADE

cmd () {
    echo "$*"
    "$@"
}

DHCP="--dhcp-range=$NET.100,$NET.199"
DNS="--domain=i"
# BIND="-z $ME"

# Run tftp from current directory
# This works with debian netboot.tar.gz
CONF=`mktemp dnsmasq.XXXXX`
cat <<EOF >$CONF
dhcp-boot=pxelinux.0
enable-tftp
tftp-root=$(readlink -f .)
EOF

cmd ifconfig $IF $ME netmask 255.255.255.0 up
cmd dnsmasq  -C $CONF -d -K -9 -i $IF $BIND $DHCP $DNS

rm $CONF
