#!/bin/sh

ATCP_COM1=/bin/atcp_com1.elf
ATCPAPP_COM1=/bin/atcpapp_com1.elf

test -f $ATCP_COM1 || exit 0

start() {
	if [ -f /var/run/atcp_com1.pid ]; then
		echo " --- ATCP_COM1 is running, start to restart"
		stop
		sleep 1
	fi	
	$ATCP_COM1
}

stop() {
	if [ -f /var/run/atcp_com1.pid ]; then
        $ATCPAPP_COM1 stop
        if [ $? == 0 ]; then
            echo " --- ATCP_COM1 is stopped."
		else
		    echo " --- ATCP_COM1 cannot be stopped."
		fi
            kill -9 `cat /var/run/atcp_com1.pid` > /dev/null 2>&1
	    	rm /var/run/atcp_com1.pid
		    #killall $ATCP_COM1 > /dev/null 2>&1
		
	else
		echo " --- ATCP_COM1 is not running"
	fi
}

case "$1" in
    start)
        echo -n " START ATCP_COM1"
        start
        ret=0
        echo "."
        ;;
    stop)
        echo -n " STOP ATCP_COM1"
        sleep 1
        stop
        ret=0
        echo "."
        ;;
    restart)
        echo -n "Restart ATCP_COM1"
        stop
        sleep 1
        start
        ret=0
        echo "."
        ;;
    reload)
        echo -n "Reload ATCP_COM1 Configuration"
        ret=0
        $ATCPAPP_COM1 reload
        echo "."
        ;;
    *)  
        echo "Usage: /etc/init.d/atcp_com1 {start|stop|restart|reload}"
        ret=0
        exit 2
        ;;
esac

if [ $ret = 0 ] ; then
    echo " OK"
    exit 0
else
    echo " FAIL"
    exit 1
fi


