#!/bin/bash
#
#	/etc/rc.d/init.d/iwds
# chkconfig: 2345 40 88
#
# processname: iwd
### BEGIN INIT INFO
# Provides:       iwds
# Required-Start: $network
# Required-Stop:  $network
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Description:    Wireless service based on iwd
### END INIT INFO

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

DAEMON=/usr/libexec/iwd/iwd
START_ARGS="--background --make-pidfile"
PIDFILE="/var/run/iwd.pid"
LOCKFILE="/var/lock/subsys/iwd"

IWD_ARGS=""
test -x $DAEMON || exit 0

[ -r "/etc/default/iwd" ] && . "/etc/default/iwd"

start() {
	gprintf "Starting iwd:"
	mkdir -p /tmp/iwd/hotspot
	start-stop-daemon -b -m -S -q -p "$PIDFILE" -x "$DAEMON" \
		-- $IWD_ARGS
	status=$?
	if [ "$status" -eq 0 ]; then
		echo && touch $LOCKFILE
	else
		gprintf "FAIL\n"
	fi
	return "$status"
}

stop() {
	gprintf "Stopping iwd:"
	killproc $DAEMON
	status=$?
	if [ "$status" -eq 0 ]; then
		echo && rm -f $LOCKFILE
	else
		gprintf "FAIL\n"
	fi
	return "$status"
}
restart() {
	stop
	start
}
fdr_status() {
	status $DAEMON
}

case "$1" in
	start|stop|restart)
		"$1";;
    status)
		fdr_status;;
	*)
		gprintf "Usage: %s {start|stop|restart|status}\n" "$0"
		exit 1
esac
