#!/bin/bash
# This script will start a DHCP server and TFTP server in order to provide
# fully functional environment for PXE booting.
#

cd $(dirname $(readlink -f $0))

# find out our own IP address. If more interfaces are available, use the first one
IP=$(ifconfig "$(ls -1 /sys/class/net | grep eth | head -n 1)" | grep "inet addr" | cut -d : -f 2 | cut -d " " -f 1)

# if no IP is assigned to this computer, setup private address randomly
if [ "$IP" = "" ]; then
   killall dhcpcd 2>/dev/null
   IP="10."$(($RANDOM/130+1))"."$(($RANDOM/130+1))".1"
   ifconfig $(ls -1 /sys/class/net | head -n 1) $IP netmask 255.0.0.0
fi

# calculate C class range
RANGE=$(echo $IP | cut -d "." -f 1-3)

# make sure dnsmasq can be started
killall dnsmasq 2>/dev/null
mkdir -p /var/state/dnsmasq

# start the DHCP server and TFTP server
./dnsmasq --enable-tftp --tftp-root=/boot \
--dhcp-boot=/pxelinux.cfg/pxelinux.0,"$IP",$IP \
--dhcp-range=$RANGE.2,$RANGE.250,infinite --log-dhcp
