#! /system/bin/sh
 
TABLE=authentication
TYPE=$1

function getTable()
{       
	SU=`content query --uri content://stbconfig/summary --projection name:value | grep name=${NAME},`
	if [ "$SU" != "" ];then
	   TABLE=summary
	   echo "==================find in summary=========================="
	fi
	AU=`content query --uri content://stbconfig/authentication --projection name:value | grep name=${NAME},`
	if [ "$AU" != "" ];then
	   TABLE=authentication
	   echo "==================find in authentication==================="
	fi
	echo "=====================TABLE=$TABLE============================="
}

function showall()
{
     echo "============================================================="
     echo "==================show all databases========================="
	 echo "==================show all authentication===================="
	 content query --uri content://stbconfig/authentication
	 echo "==================show all stbconfig========================="
	 content query --uri content://stbconfig/stbconfig --projection name:value --sort "name ASC"
	 echo "===================show all summary=========================="
	 content query --uri content://stbconfig/summary --projection name:value --sort "name ASC"
     echo "============================================================="
}

if [ "$TYPE" == "" ];then
     echo "============================================================="
     echo "==================show all cmd start========================="
	 echo "db  (---------------show help--------------------------------)"
	 echo "db get (------------show all databases table-----------------)"
	 echo "db get name   (-----------------show name's value------------)"
	 echo "db set name newvalue  (---modify newvalue as name's value----)"
	 echo "db add name value (--insert name value into stbconfig table--)" 
	 echo "db init     (-----------restore db into init state-----------)" 
	 echo "==================show all cmd start========================="
     echo "============================================================="

fi
      
if [ "$TYPE" == "get" ];then
	NAME=$2
	if [ "$NAME" == "" ];then
	   showall
	else
	   getTable
       content query --uri content://stbconfig/$TABLE --where "name='$NAME'"
	fi
elif [ "$TYPE" == "set" ];then
	NAME=$2
	VALUE=$3
	getTable
	content update --uri content://stbconfig/$TABLE/$NAME  --bind value:s:$VALUE
	content query --uri content://stbconfig/$TABLE --where "name='$NAME'"
elif [ "$TYPE" == "del" ];then
	NAME=$2
	getTable
	content delete --uri content://stbconfig/$TABLE/$NAME
	#content query --uri content://stbconfig/$TABLE --where "name='$NAME'"
elif [ "$TYPE" == "add" ];then
	NAME=$2
	VALUE=$3
	getTable
	content insert --uri content://stbconfig/$TABLE --bind name:s:$NAME --bind value:s:$VALUE
	content query --uri content://stbconfig/$TABLE --where "name='$NAME'"
elif [ "$TYPE" == "init" ];then
	rm /params/backup.dat
	cp /system/opt/etc/backup.dat /params/
	rm -rf /data/data/com.fiberhome.fhstbconfig
	showall
fi
 

 
 
