#!/bin/sh

DESC="channelmanager"

do_start() {
    str1=$(cat /proc/cmdline)
    str2="calibration="
    str3="sprdboot.mode=autotest"

    result1=$(echo $str1 | grep "${str2}")
    result2=$(echo $str1 | grep "${str3}")

    if [ "$result1" != "" ];then
        echo "enter the calibration"
        exit 0
        elif [ "$result2" != "" ];then
        echo "enter the bbat"
        /usr/bin/channelmanager &
        exit 0
    else
        echo "enter the normal"
        cat /etc/inittab | grep "/usr/bin/channelmanager"

        if [ $? -eq 0 ]
        then
            echo "Starting /usr/bin/channelmanager from inittab"
        else
            echo "enter the channelmanager"
            /usr/bin/channelmanager &
        fi
        exit 0
    fi
}

do_stop() {
    start-stop-daemon --stop --name channelmanager --quiet
}

case "$1" in
  start)
	echo "Starting $DESC"
	do_start
	;;
  stop)
	echo "Stopping $DESC"
	do_stop
	;;
  restart|force-reload)
	echo "Restarting $DESC"
	do_stop
	sleep 1
	do_start
	;;
  *)
	echo "Usage: $0 {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
