#!/bin/sh
# chkconfig: 2345 21 79
### BEGIN INIT INFO
# Provides:          hostapd
# Required-Start:    modules $local_fs $network
# Required-Stop:     
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start hostapd at boot time
# Description:       Enable service provided by hostapd.
# X-Start-Before:    
# X-Stop-After:      
# X-Timesys-Start-Number:  21
# X-Timesys-Stop-Number:  79
### END INIT INFO

PIDFILE=/var/run/hostapd.pid
CONF=/etc/hostapd.conf

start_service() {
    if [ -f $CONF ]; then
        echo -n "Starting hostapd: "
        if hostapd -B -P $PIDFILE $CONF; then
            echo "done"
        else
            echo "failed"
            exit 1
        fi
    fi
}

stop_service() {
    if [ -e $PIDFILE ]; then
        echo -n "Starting hostapd: "
        PID=$(cat $PIDFILE)
        if [ -z "$PID" ]; then
            echo "stale pid file, cleaning up"
            rm -f $PIDFILE
            exit 1
        fi
        if kill $PID ; then
            echo "done"
        else
            echo "failed"
            exit 1
        fi
    else
        echo "No pid file... is hostapd running?"
    fi
}

case "$1" in
	start)
	start_service
	;;
	stop)
	stop_service
	;;
	restart)
	stop_service
	start_service
	;;
	*)
    echo "$0 [start|stop|restart]"
    exit 1
	;;
esac

