#!/bin/sh
#
# chkconfig: 2345 11 89
# description: The daemon supports a so called Host AP mode, i.e., \
# 	       it takes care of IEEE 802.11 management functions in \
#	       the host computer and acts as an access point.
# config: /etc/hostap/hostapd.conf
# config: /etc/hostap/hostapd.accept
# config: /etc/hostap/hostapd.deny
# pidfile: /var/run/hostapd.pid

# author:       Alexei Takaseev <taf@altlinux.ru>
# version:	2003093000
# changed:	2003093000 initialize release

# Source function library
if [ -f /etc/init.d/functions ]; then
    . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ]; then
    . /etc/rc.d/init.d/functions
else
    exit 0
fi

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

# check if the config files are present
[ -f /etc/hostap/hostapd.conf ] || exit 0
[ -f /etc/hostap/hostapd.accept ] || exit 0
[ -f /etc/hostap/hostapd.deny ] || exit 0

# This is our service name
BASENAME=`basename $0`
if [ -L $0 ]; then
    BASENAME=`find $0 -name $BASENAME -printf %l`
    BASENAME=`basename $BASENAME`
fi
 
# Source service configuration.
if [ -f /etc/sysconfig/$BASENAME ]; then
    . /etc/sysconfig/$BASENAME
else
    echo "$BASENAME: configfile /etc/sysconfig/$BASENAME does NOT exist !"
    exit 1
fi

PIDFILE=/var/run/hostapd.pid
LOCKFILE=/var/lock/subsys/hostapd
RETVAL=0

start() {
    start_daemon --lockfile "$LOCKFILE" --expect-user root -- hostapd $HOSTAPD_OPTIONS
    RETVAL=$?
    return $RETVAL
}

stop() {
    stop_daemon  --lockfile "$LOCKFILE" --expect-user root hostapd
    RETVAL=$?
    return $RETVAL
}

restart() {
    stop
    start
}

reload() {
    msg_reloading hostapd
    stop_daemon --expect-user root -HUP hostapd
    RETVAL=$?
    return $RETVAL
}
 
condrestart() {
    [ -e /var/lock/subsys/$BASENAME ] && restart || :
}

rhstatus() {
    status hostapd
}

case "$1" in
    start)
	start
	;;
    stop)
	stop
	;;
    restart)
	restart
	;;
    reload)
	reload
	;;
    condrestart)
	condrestart
	;;
    status)
	rhstatus
	;;
    *)
	echo $"Usage: $BASENAME {start|stop|restart|reload|condrestart|status}"
	RETVAL=1
esac

exit $RETVAL
