#!/bin/sh /etc/rc.common
# 
# Copyright 2019-2020 Michael BD7MQB <bd7mqb@qq.com>
# This is free software, licensed under the GNU GENERAL PUBLIC LICENSE, Version 2.0
# 

START=99
APP=dmrid
PID_FILE=/var/run/dmrid.pid
USE_PROCD=1
EXTRA_COMMANDS="status query"                                                                                        
EXTRA_HELP="	status	Display status of ${APP}
	query	<CALLSIGN> Query user info by callsign" 

start_service() {
    ubus -t 60 wait_for network.interface network.wireless 2>/dev/null

    logger -s -t mmdvm "Starting $APP Service..."
    procd_open_instance
    procd_set_param respawn
    procd_set_param command /usr/sbin/$APP
    procd_set_param pidfile "${PID_FILE}"
    procd_close_instance
}
 
stop_service() {
    logger -s -t mmdvm "Stoping $APP Service..."
}

query() {
    ubus -t1 call dmrid get_by_callsign "{'callsign': '${1}'}"
}

status() {
    local PID=`pidof ${APP}`
    local STATUS
    if [ ! ${PID} ]; then
        STATUS="Stopped"
    else
        STATUS="Started - PID ${PID}"
    fi

    echo "${APP}:		[${STATUS}]"
}

service_triggers() {
      procd_add_reload_trigger "dmrid"
}
