#!/bin/sh /etc/rc.common

START=95
STOP=10
SERVICE_DAEMONIZE=1

NAME=xlnetacc

start() {
	local retry=1
	while ps -w | grep ${NAME}.sh | grep -v grep >/dev/null 2>&1; do
		[ $retry -ge 10 ] && return 1 || let retry++
		sleep 1
	done
	rm -f /var/lock/xlnetacc.lock

	config_load "$NAME"
	config_get_bool enabled "general" "enabled" 0
	config_get_bool down_acc "general" "down_acc" 0
	config_get_bool up_acc "general" "up_acc" 0
	config_get network "general" "network"
	config_get username "general" "account"
	config_get password "general" "password"
	( [ $enabled -eq 0 ] || [ $down_acc -eq 0 -a $up_acc -eq 0 ] || [ -z "$username" -o -z "$password" -o -z "$network" ] ) && return 2

	logger -p "daemon.notice" -t "$NAME" "XLNetAcc is starting ..."
	service_start /usr/bin/${NAME}.sh --start
}

stop() {
	ps -w | grep ${NAME}.sh | grep -v grep >/dev/null 2>&1 || return 1
	local pid spid
	for pid in $(ps -w | grep ${NAME}.sh | grep -v grep | awk '{print $1}'); do
		echo "Stop XLNetAcc process PID: $pid"
		kill $pid >/dev/null 2>&1
		for spid in $(pgrep -P $pid "sleep"); do
			echo "Stop XLNetAcc process SPID: $spid"
			kill $spid >/dev/null 2>&1
		done
	done
	rm -f /var/lock/xlnetacc.lock
	logger -p "daemon.notice" -t "$NAME" "XLNetAcc has stoped."
	return 0
}

restart() {
	stop && sleep 1
	start
}
