#!/bin/sh

SPK_CONF="/etc/speaker.conf"
SPHONE_CONF="/tmp/accounts"

case "$1" in

	start )
		# config file abnormal
		[ ! -f "${SPK_CONF}" ] && printf "Configration file not exist.\n" && exit 0

        # prepare sip account conf
        /etc/scripts/sipconf
		[ ! -f "${SPHONE_CONF}" ] && printf "Configration file not exist.\n" && exit 0

        # start baresip
        /etc/scripts/baresip -4 -f /tmp > /dev/null 2>&1 &

        # start baresip service
        /etc/scripts/bareservice > /dev/null 2>&1 &

		;;

	stop )
        redis-cli lpush control-channel '{"cmd":"quit","data":""}'
		killall baresip > /dev/null 2>&1
        killall bareservice > /dev/null 2>&1
        #clear redis database
        redis-cli FLUSHALL
		;;

	restart | reload )

		/etc/scripts/sipphone.sh stop; sleep 1 ; /etc/scripts/sipphone.sh start
esac
