#!/bin/sh
#
# Init file for IPW3945 regulatory daemon
#
# chkconfig: 345 9 91
# description: Intel Pro Wireless 3945 regulatory daemon
#
#

# Source function library.
. /etc/rc.d/init.d/functions

prog="ipw3945d"

start() {
  #Check to see if we have ipw3945 hardware
  /sbin/lspci | grep -q Intel.*3945
  if [ $? -eq 0 ] ; then	
	# Load module and start daemon
	echo -n $"Starting $prog: "
	/sbin/modprobe ipw3945 2>/dev/null
	for ((count=0; count<20; count++)); do
		if [ ! -e /sys/bus/pci/drivers/ipw3945/*/cmd ]; then
			sleep 0.5
		else
			break
		fi
	done
	[ $count -eq 20 ] && return 1
	daemon /sbin/ipw3945d --quiet 2>/dev/null
	RETVAL=$?
	for ((count=0; count<20; count++)); do
		if [ ! -e /sys/bus/pci/drivers/ipw3945/*/net:* ]; then
			sleep 0.5
		else
			break
		fi
	done
	[ $count -eq 20 ] && return 1
	echo
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/ipw3945d
	return $RETVAL
  else
	#There is not 3945 hardware, say so
	echo -n "There is not any 3945 hardware, not starting ipw3945d"
	return 0
  fi
}

stop() {
  #Check to see if we have ipw3945 hardware
  /sbin/lspci | grep -q Intel.*3945
  if [ $? -eq 0 ] ; then	
        # Stop service.
        echo -n $"Shutting down $prog: "
	killproc ipw3945d
	RETVAL=$?
	echo
	if [ $RETVAL -eq 0 ]; then
	    rm -f /var/lock/subsys/ipw3945d
	    /sbin/modprobe -r ipw3945 2>/dev/null
	fi
	return $RETVAL
  else
	#There is not 3945 hardware, just exit quietly
	return 0
  fi
}

[ -f /sbin/ipw3945d ] || exit 0

# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
	stop
        ;;
  status)
	status ipw3945d
	RETVAL=$?
	;;
  restart|reload)
	stop
	start
	RETVAL=$?
	;;
  condrestart)
	if [ -f /var/lock/subsys/ipw3945d ]; then
	    stop
	    start
	    RETVAL=$?
        fi
	;;
  *)
        echo $"Usage: $0 {start|stop|restart|reload|condrestart|status}"
        exit 1
esac

exit $RETVAL
