#!/bin/bash

# ======================================================================
# dhcpserver.sh
# ------------
#
# $Author: schulzema1 $
# $DATE$
# $Revision: 2.0.0.0 $
# $ID$
#
# This script is executed during boot
#
STATUS=stopped

CONF_FILE=/etc/dhcpd.conf
TMP_CONF=/tmp/dhcpd.conf
BIN=/sbin/dhcpd

NETDEV=eth0
PIDFILE=/var/run/dhcpd.pid
LEASE_FILE=/var/run/dhcpd.lease

# defines for previuous dhcp deamon: udhcpd
CONF_FILE_OLD=/etc/udhcpd.conf
TMP_CONF_OLD=/tmp/udhcpd.conf
BIN_OLD=/usr/sbin/udhcpd

NETDEV_OLD=
PIDFILE_OLD=/var/run/udhcpd.pid
LEASE_FILE_OLD=/var/run/udhcpd.lease

function die(){
	echo -e "[ERROR]\t$1"
	STATUS=crashed
	exit 1
}

function check_and_read_config(){
	# 1. check that config file is readable
	[ -s $CONF_FILE ] || die "$CONF_FILE does not exist or is empty." 
	# 1.1 read configuration
	ls $PIDFILE
	[ $? -eq 0 ] || touch $PIDFILE || die "$PIDFILE does not exist and could not be created"

	ls $LEASE_FILE
	[ $? -eq 0 ] || touch $LEASE_FILE || die "$LEASE_FILE does not exist and could not be created"

	# 2. check that the device is available
	# -q returns 0 if the pattern is found
	grep -q $NETDEV /proc/net/dev
	[ $? -eq 0 ] || die "$NETDEV not found."
	# 3. check that device is up
	[ -n "`ifconfig $NETDEV`" ] || die "$NETDEV is not up, check ifconfig."
	# 4. check that ISC dhcpd executable is available
	[ -x $BIN ] || die "$BIN does not exist or isnt executable."
}


function start_dhcpd(){
	check_and_read_config
	# check if dhcpd is already running
	# -s means "not empty" so we test for an empty file or no file 
	[ ! -s $PIDFILE ] || die "dhcpd is already running."
	# read the iprange from the ifconfig output
	IPRANGE=`ifconfig $NETDEV | grep "inet addr:" | sed 's/.*inet addr:\([[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+\.\)\([[:digit:]]\+\).*/\1/'`
	# check that $IPRANGE has a proper layout like "192.168.0."
	[ -n "`echo $IPRANGE | grep -e '[[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+\.'`" ] || die "Couldnt determine the right subnet. Got this: \"$IPRANGE\"."
		
	echo -n "Starting DHCP Server..."
	$BIN -f eth0 -pf $PIDFILE -lf $LEASE_FILE $CONF_FILE &
	echo $! > $PIDFILE
	if [ $? -ne 0 ]; then
	   die "\t\t\t[ failed ]"
	   STATUS=crashed
	else
	    echo -e "\t\t\t [ Done ]"
	    STATUS=running
	fi
}

function stop_dhcpd(){
	check_and_read_config
	# need pid file
	[ -s $PIDFILE ] || die "$PIDFILE is empty or not readable."
	echo -n "Stoping DHCP Server..."
	PID=`cat $PIDFILE`

	if [[ $PIDFILE ]]; then
		kill $PID
		[ $? -eq 0 ] || die "Couldnt kill running udhcpd"
		rm $PIDFILE
		echo -e "\t\t\t [ Done ]"
		STATUS=stopped
	else
		die "Could not find the right PID"
	fi
	
}

# config functions for old udhcpd

function check_and_read_config_old(){
	# 1. check that config file is readable
	[ -s $CONF_FILE_OLD ] || die "$CONF_FILE_OLD does not exist or is empty." 
	# 1.1 read configuration
	NETDEV=`grep -e '^interface' $CONF_FILE_OLD | sed 's/^interface[[:blank:]]\+\([^[:blank:]]\+\).*/\1/'`
	[ $? -eq 0 ] || die "couldnt read network interface from config file"
	# 2. check that the device is available
	grep -q $NETDEV /proc/net/dev
	[ $? -eq 0 ] || die "$NETDEV not found."
	# 3. check that device is up
	[ -n "`ifconfig $NETDEV`" ] || die "$NETDEV is not up, check ifconfig."
	# 4. check that udhcp executable is available
	[ -x $BIN_OLD ] || die "$BIN_OLD does not exist or isnt executable."
}


function start_dhcpd_old(){
	check_and_read_config_old
	# check if udhcpd is already running
	# -s means "not empty" so we test for an empty file or no file 
	[ ! -s $PIDFILE_OLD ] || die "udhcpd is already running."
	# read the iprange from the ifconfig output
	IPRANGE=`ifconfig $NETDEV | grep "inet addr:" | sed 's/.*inet addr:\([[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+\.\)\([[:digit:]]\+\).*/\1/'`
	# check that $IPRANGE has a proper layout like "192.168.0."
	[ -n "`echo $IPRANGE | grep -e '[[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+\.'`" ] || die "Couldnt determine the right subnet. Got this: \"$IPRANGE\"."
	# Update the config file to use right ip range
	sed $CONF_FILE_OLD -e 's/\(start\|end\)\([^[:digit:]]*\)\([[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+\.\)\([[:digit:]]\+\)/\1\2'$IPRANGE'\4/' > $TMP_CONF_OLD
		
	# Cause /var/run is on a tmpfs we need to recreate this file every time, to avoid ugly errors.
	touch $LEASE_FILE_OLD
		
	echo -n "Starting DHCP Server..."
	$BIN_OLD $TMP_CONF_OLD &
	echo $! > $PIDFILE_OLD
	if [ $? -ne 0 ]; then
	   die "\t\t\t[ failed ]"
	   STATUS=crashed
	else
	    echo -e "\t\t\t [ Done ]"
	    STATUS=running
	fi
}

function stop_dhcpd_old(){
	check_and_read_config_old
	# need pid file
	[ -s $PIDFILE_OLD ] || die "$PIDFILE_OLD is empty or not readable."
	echo -n "Stoping DHCP Server..."
	PID=`pidof udhcpd`
	# find the next valid pid
	PID_CMD_STRING=
	TIMEOUT=0
	while ([[ ! $PID_CMD_STRING =~ "$PID $BIN_OLD $TMP_CONF_OLD" ]] && [ $TIMEOUT -lt 10 ]); do
		PID_CMD_STRING=`ps -eo pid,cmd | grep '^[[:blank:]]\+'$PID`
		if [[ ! $PID_CMD_STRING =~ "$PID $BIN_OLD $TMP_CONF_OLD" ]]; then
			let PID++
		fi
		let TIMEOUT++
	done
	if [[ $PID_CMD_STRING =~ "$PID $BIN_OLD $TMP_CONF_OLD" ]]; then
		kill $PID
		[ $? -eq 0 ] || die "Couldnt kill running udhcpd"
		rm $PIDFILE_OLD
		echo -e "\t\t\t [ Done ]"
		STATUS=stopped
	else
		die "Could not find the right PID"
	fi	
}

action=$1

case "$action" in
	start*)
	    # dont start if dhcp is already configured in bootloader 
		# dont start if the static ip is in typical tms ip range
		CMD_IP=`grep -oe "ip=[[:alnum:]]\+ " /proc/cmdline`
		GOT_IP=`ifconfig eth0 | grep "inet addr:" | sed 's/.*inet addr:\([[:digit:]]\+\.[[:digit:]]\+\.\)\([[:digit:]]\+\.[[:digit:]]\+\).*/\1/'`
		if ( [ "$GOT_IP" != "136.230." ] && [ "$CMD_IP" != "ip=dhcp" ] ); then
			# if exists old dhcpd then use it
			if [ -f $BIN_OLD ]; then
			  echo start UDHCP DEAMON
			  start_dhcpd_old
			else
			  echo DHCP DEAMON
			  start_dhcpd
			fi
		fi;
		;;
	stop)
			# if exists old dhcpd then use it
			if [ -f $BIN_OLD ]; then
			  echo stopping UDHCP DEAMON
			  stop_dhcpd_old
			else
			  echo stopping DHCP DEAMON
			  stop_dhcpd
			fi
		;;	
	status)
		if [ -f $BIN_OLD ]; then
			#check_and_read_config_old
			if [ -f $PIDFILE_OLD ]; then
				echo UDHCP is running
			else
				echo UDHCP is stopped
			fi
		else
			#check_and_read_config
			if [ -f $PIDFILE ]; then
				echo DHCPD running
			else
				echo DHCPD stopped
			fi
		fi
		;;
	*)
		echo "Unknown action=$action !!"
		;;
esac
