#!/bin/sh
#
# init.d script for quectel daemon
#  ---------------------------------------------------------------------------
#   Copyright (c) 2012 Qualcomm Technologies, Inc.  All Rights Reserved.
#   Qualcomm Technologies Proprietary and Confidential.
#  ---------------------------------------------------------------------------
#

set -e

program_name="quectel_slic_daemon"
echo -n "$0 $1" > /dev/kmsg

if [ ! -f "/data/slic.conf" ]
then
    echo -n "/data/slic.conf not exis" > /dev/kmsg
    exit 0
fi

slic_conf=`cat /data/slic.conf`
echo -n "/data/slic.conf $slic_conf" > /dev/kmsg

enable=${slic_conf:0:1}
type=${slic_conf:2:1}
country=${slic_conf:4:3}
statu=`ps | grep -w $program_name | grep -v grep | wc -l`

echo -n "slic statu:$statu,enable:$enable,type:$type,country:$country" > /dev/kmsg

case "$1" in
       start)
                echo -n "Starting quectel_slic_daemon: " > /dev/kmsg
                if [ $statu -ge '1' ]
                then
                    echo "$program_name was already been startted" > /dev/kmsg
                    exit 1
                fi
                if [ $enable = '1' ]
                then
                    $program_name $type $country &
                    echo -n "start done" > /dev/kmsg
                fi
                echo "start $program_name $type $country end" > /dev/kmsg
                ;;
       stop)
                echo -n "Stopping quectel_slic_daemon: " > /dev/kmsg
                if [ $statu = 0 ] || [ $statu = '0' ]
                then
                    echo "$program_name was already been stopped" > /dev/kmsg
                    exit 1
                fi
                echo -n "stop $program_name" > /dev/kmsg
                killall $program_name &
                echo "stop $program_name $type $country end" > /dev/kmsg
                ;;
       restart)
                $0 stop
                sleep 5
                $0 start
                ;;
       *)
                echo "Usage: quectel_slic_daemon { start | stop | restart }" >&2
                exit 1
                ;;
esac

exit 0
