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

USE_PROCD=1
START=99
STOP=15
SERVICE_WRITE_PID=1
SERVICE_DAEMONIZE=1
LOGFILE=/var/log/kuainiao.log

start_service()
{
  procd_open_instance
  procd_set_param command /usr/bin/kuainiao
  procd_set_param respawn ${respawn_threshold:-5} ${respawn_timeout:-300} ${respawn_retry:-10}
  procd_close_instance
}

stop_service(){	
	clean_log
	pid_kuainiao=`ps | grep -v grep | grep -w "/usr/bin/kuainiao" |awk '{print $1}' 2>/dev/null`
	[ -n "$pid_kuainiao" ] && {
		for pid in $pid_kuainiao
		do
			echo "Stop service kuainiao PID: $pid"
			kill $pid 2>/dev/null
			for spid in `pgrep sleep`
			do
				ppid=`cat /proc/$spid/status | grep -w "PPid" | awk '{print $2}'`
				[ "$ppid" -eq "$pid" ] && kill $spid 2>/dev/null && echo "Stop service kuainiao SPID: $spid"
			done
		done
	}
	echo "<font color=red>暂无加速信息</font>" > /usr/share/kuainiao/kuainiao_down_state
	echo "<font color=red>暂无加速信息</font>" > /usr/share/kuainiao/kuainiao_up_state
}

clean_log() {
	[ -f "$LOGFILE" ] && {
		logsnum=$(cat $LOGFILE | grep -c .)
		if [ $logsnum -gt 300 ];then
			rm -f $LOGFILE >/dev/null 2>&1 &
			echo "$(date): 日志文件过长，清空处理！" >> $LOGFILE
		fi
	}
}
