#!/system/bin/sh

function getTiming()
{
  exec_start=$1
  exec_end=$2
  exec_start_s=`echo $exec_start | cut -d "." -f 1`  #get start time :s
  exec_start_ns=`echo $exec_start | cut -d "." -f 2` #get start time :ns
  exec_end_s=`echo $exec_end | cut -d "." -f 1`   #get end time :s
  exec_end_ns=`echo $exec_end | cut -d "." -f 2` #get end time :ns

  exec_start_ns=$(echo -e $exec_start_ns | sed -r 's/0*([0-9])/\1/')
  exec_end_ns=$(echo -e $exec_end_ns | sed -r 's/0*([0-9])/\1/')


  if [ $exec_end_ns -le $exec_start_ns ]; then
         delta_time_ms=$((($exec_start_ns - $exec_end_ns)/1000000))
        delta_time_s_old=$(($exec_end_s - $exec_start_s))
        delta_time_s=`expr $delta_time_s_old - 1`
  else
         delta_time_ms=$((($exec_end_ns - $exec_start_ns)/1000000))
        delta_time_s=$(($exec_end_s - $exec_start_s))
  fi

   echo "This getting cp status spent time: $delta_time_s.$delta_time_ms s"
}                                          


if [ $# -ne 2 ]; then
    echo "usage: $0 <set/get> <status>"
    exit 1
fi

file="/dev/cp_sleep_status"
error_string="error:"

if [ ! -f "$file" ]; then
 touch "$file"
 chmod 777 $file
 echo "2" > $file
fi


cmd=$1
cp_value=$2

if [ x"$cmd" = x"get" ];then
    cp_value=$(cat $file)
    if [ $cp_value = "1" ]; then
	
        echo "current cp status is 1:suspend/sleep"
        exit 1
		
    elif [ $cp_value = "2" ]; then
	
		start=$(date +%s.%N)
		cd /
		uname_result=$(/data/hbipc-utils run "uname -a")
		param=`echo $uname_result | cut -c 1-8`
		param1=`echo $uname_result | cut -c 1-26`
		end=$(date +%s.%N)
		getTiming $start $end
		if [ "$param" = "Linux J2" ];then
			echo "current cp status is 2:normal !"
			exit 2
		elif [[ $param1 == *$error_string* ]];then
			echo "current cp status is is 5:exception"
			echo "5" > $file
			exit 5
		fi
		
	elif [ $cp_value = "3" ]; then
	
		echo "current cp status is 3：Motion Detect"
        exit 3	
	
	elif [ $cp_value = "4" ]; then
	
		start=$(date +%s.%N)
		cd /
		uname_result=$(/data/hbipc-utils run "uname -a")
		param=`echo $uname_result | cut -c 1-8`
		param1=`echo $uname_result | cut -c 1-26`
		end=$(date +%s.%N)
		getTiming $start $end
		if [ "$param" = "Linux J2" ];then
			echo "current cp status is 2：normal"
			echo "2" > $file
			exit 2	
		else
		   echo "current cp status is 4：rebooting"
		   exit 4	
		fi
		
	
	elif [ $cp_value = "5" ]; then
		start=$(date +%s.%N)
	    cd /
		uname_result=$(/data/hbipc-utils run "uname -a")
		param=`echo $uname_result | cut -c 1-8`
		param1=`echo $uname_result | cut -c 1-26`
		end=$(date +%s.%N)
		getTiming $start $end
		if [ "$param" = "Linux J2" ];then
			echo "current cp status is 2：normal"
			echo "2" > $file
			exit 2
		else
		    echo "current cp status is 5：exception"
			exit 5
		fi

	else
		echo "current sleep_status is unkown state"
		exit -1
    fi

#below is setting state action
elif [ x"$cmd" = x"set" ];then
    cp_value=$2
	
    if [ "$cp_value" = "1" ]; then
        echo "trigger cp suspend action"
		cd /
		./data/hbipc-utils run_noret "hrut_sleep"
		echo "1" > $file
        exit 0
    elif [ "$cp_value" = "2" ]; then
        echo "trigger cp resume action"
		# Run cp resume cmd
		echo "2" > $file
        exit 0
	elif [ "$cp_value" = "3" ]; then
		echo "trigger cp motion detection action"
		# Run cp motion detect cmd
		echo "3" > $file
        exit 0
	elif [ "$cp_value" = "4" ]; then
		echo "trigger cp rebooting action"
		cd /
		./data/hbipc-utils run "reboot"
		echo "4" > $file
        exit 0
	else
		echo "hrsom_runstatus set command not support!"
		exit -1
    fi

else
    echo "unsuppport command"
    exit -1
fi


