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

START=99
STOP=90
USE_PROCD=1

dir="/tmp/wechatpush/"

start_service() {
    procd_open_instance
    enable_value=$(uci get wechatpush.config.enable 2>/dev/null || echo "0")
    [ "$enable_value" -ne "0" ] && procd_set_param command /usr/share/wechatpush/wechatpush && echo "wechatpush is starting now ..."
    procd_close_instance
}

reload_service() {
    stop
    sleep 1
    start
}

stop_service() {
    [ -f ${dir}child_pid ] && parent_pid=$(cat ${dir}child_pid)
    [ -n "$parent_pid" ] && {
        child_pids=$(pgrep -P $parent_pid)
        echo "Terminating child processes of wechatpush..."
        for child_pid in $child_pids; do
            kill $child_pid
        done
    }
    local pids=$(ps | grep "wechatpush" | grep -v grep | grep -v $$ | awk '{print $1}')
	[ -n "$pids" ] && echo "$pids" | xargs kill 2>/dev/null
    echo "Terminating wechatpush process..."
}

service_triggers() {
    procd_add_reload_trigger wechatpush
}
