#!/bin/sh
###################################################################
# History :
#
# - create  : 
# - 2012/12/09 Lin ZHU     PR301207 Testability for OT (audio only): Add the embedded command tonedetector \n
# 
# 
#
###################################################################
argument=$@

if [ $# -eq 1 ]; then 
    expr $argument + 300 > /dev/null 2>&1
    isnumber=`echo $?`
#    echo "$isnumber"

    if [ "$isnumber" = "0" ]; then
#        echo "number found"

        if [ $argument -lt 5 ]; then
            argument=5
        elif [ "$argument" -gt 300 ]; then
            argument=300
        fi

       # echo "$argument"
        result=`/usr/bin/ICTApplication/ICTCliGateLite -call ictaudio:cmdTonedetector -args "string:on"`
        exist=`echo $result|grep KO`
        #check the result of tonedetector is OK/KO, OK do query&off, KO return
        if [ -z "$exist" ]; then
            echo "Start tonedetector"
            while [ $argument -gt 0 ]
            do
                #echo "$argument"
                argument=`expr $argument - 1`
                sleep 1
            
                result=`/usr/bin/ICTApplication/ICTCliGateLite -call ictaudio:cmdTonedetector -args "string:query"`
                #check the return parameter
                exist=`echo $result|grep KO`            
                if [ -z "$exist" ]; then
                    echo "TONE Detected"
                    break
                fi
            done
            #call command tonedetector off to turn it off
            /usr/bin/ICTApplication/ICTCliGateLite -call ictaudio:cmdTonedetector -args "string:off"
        else
            #print-out the result of command
            echo "tonedetector KO"
        fi
    else
        #echo "not number"
        /usr/bin/ICTApplication/ICTCliGateLite -call ictaudio:cmdTonedetector -args "string:${argument}"
    fi
else
    #echo "more than 1"
    /usr/bin/ICTApplication/ICTCliGateLite -call ictaudio:cmdTonedetector -args "string:${argument}"
fi

