#!/bin/bash
#
# Startup script handle the initialisation of PXE daemon
#
#
# description: A Preboot Execution Environment (PXE) Server.  This 
# server will allow you to network boot other PXE based machines.
#
# Script Authors: Erwan Velu <erwan@mandrakesoft.com>
#		  Antoine Ginies <aginies@mandrakesoft.com>
#
# Based on init script for pxe of Intel
#
### BEGIN INIT INFO
# Provides:          pxe
# Required-Start:    $syslog 
# Should-Start:      $time ypbind sendmail
# Required-Stop:     $syslog $remote_fs
# Should-Stop:       $time ypbind sendmail
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description:  Startup script for PXE
# Description:        Startup script handle the initialisation of PXE daemon
### END INIT INFO
#
# config: /etc/pxe.conf


PXE_CONF=/etc/pxe.conf
PXE_BIN=/usr/sbin/pxe
PXE_PID=/tmp/pxe.pid
LOCKFILE=/var/lock/subsys/pxe

if [ ! -f ${PXE_CONF} ]; then
    echo "${PXE_CONF} is not present !"
    exit 1

fi

if [ ! -x ${PXE_BIN} ]; then
    echo "${PXE_BIN} is not executable !\n"
    exit 1
fi


. /etc/rc.status

# Reset status of this service
rc_reset
case "$1" in
  start)
	echo -n 'Stopping PXE server: '

	# Looking for interface used for pxe service
	INTERFACE=$(grep "interface=" ${PXE_CONF} | cut -d "=" -f 2);

	# Check if pxe is already running
	if [ "$(pidof pxe)" = "" ]; then
		rm -rf $LOCKFILE
		rm -rf $PXE_PID
	    	route add -host 255.255.255.255 $INTERFACE 1>/dev/null 2>/dev/null
	    	route add -net 224.0.0.0 netmask 224.0.0.0 $INTERFACE 1>/dev/null 2>/dev/null
	    	startproc ${PXE_BIN} -c ${PXE_CONF}
	else	   
	    	echo  -n 'PXE server already started !'
	    	
	fi
        rc_status -v
	;;
  stop)
	#action "Stopping pxe daemon: " 
	route del 255.255.255.255 2>/dev/null
	route del -net 224.0.0.0/3 2>/dev/null
	echo -n 'Stopping PXE server'
	killproc ${PXE_BIN}
	rm -f ${PXE_PID}
        rc_status -v
	;;
  reload|restart)
	$0 stop
	$0 start
	;;
  status)
	checkproc ${PXE_BIN}
	rc_status -v
	;;
  *)
	echo "Usage: pxe {start|stop|restart|reload|status}\n"
	exit 1
esac

rc_exit
