#!/system/bin/sh

info_version="/info/version"
info_sys_cfg="/info/sys_cfg"
info_network_interface="/info/network/interface"
TAG_NUM=$#
MODE="normal"
result="null"

init() {
if [ "`busybox grep ro.recovery.model /default.prop`" ] ; then
  MODE="recovery"
  mkdir -p /system_dir /vendor
  if ! busybox test -s "$info_version" ; then 
    touch $info_version
    check_mount_vendor
    check_mount_system
    project_version=`grep ro.project.version -r /vendor/build.prop   |cut -d'=' -f2`
    vendor_version=`grep ro.vendor.version -r /vendor/default.prop |cut -d'=' -f2`
    system_version=`grep ro.system.version -r /system_dir/default.prop  |cut -d'=' -f2`
    recovery_version=`grep ro.recovery.version -r /default.prop  |cut -d'=' -f2`
    if [ ! "$system_version" ] ; then
        system_version=$project_version
    fi
    if [ ! "$system_version" ] ; then
        recovery_version=$project_version
    fi
    kernel_version=$project_version
    boot_version=$project_version
    echo "ro.project.version=$project_version" > $info_version
    echo "ro.boot.version=$boot_version" >> $info_version
    echo "ro.system.version=$system_version" >> $info_version
    echo "ro.vendor.version=$vendor_version" >> $info_version
    echo "ro.kernel.version=$kernel_version" >> $info_version
    echo "ro.recovery.version=$recovery_version" >> $info_version
  fi
else
  if ! busybox test -s "$info_version" ; then 
    touch $info_version
    project_version=`grep ro.project.version -r /vendor/build.prop   |cut -d'=' -f2`
    vendor_version=`grep ro.vendor.version -r /vendor/default.prop |cut -d'=' -f2`
    system_version=`grep ro.system.version -r /system/default.prop  |cut -d'=' -f2`
    boot_version=`grep ro.boot.version -r /default.prop  |cut -d'=' -f2`
    if [ ! "$system_version" ] ; then
        system_version=$project_version
    fi
    if [ ! "$system_version" ] ; then
        recovery_version=$project_version
    fi
    kernel_version=$project_version
    recovery_version=$project_version
    echo "ro.project.version=$project_version" > $info_version
    echo "ro.boot.version=$boot_version" >> $info_version
    echo "ro.system.version=$system_version" >> $info_version
    echo "ro.vendor.version=$vendor_version" >> $info_version
    echo "ro.kernel.version=$kernel_version" >> $info_version
    echo "ro.recovery.version=$recovery_version" >> $info_version
    echo "ro.git.version=0-0" >> $info_version
  fi
fi
  mkdir -p `dirname $info_network_interface`
  touch $info_sys_cfg   $info_network_interface
  chmod 755 `dirname $info_network_interface`
  chmod 644 $info_sys_cfg  $info_version  $info_network_interface 
}

error_exit() {
  exit 127
}

check_mount_vendor() {
if [ "$MODE" == "normal"  ] ; then 
  if [ ! "`mount |grep vendor`" ] ; then
    mount /dev/block/mtd/by-name/vendor /vendor/
  fi
fi
}

check_mount_system() {
if [ "$MODE" == "normal"  ] ; then 
  if [ ! "`mount |grep system`" ] ; then
    mount /dev/block/mtd/by-name/system /system_dir/
  fi
fi
}


get_prop() {
if [ "$MODE" == "normal"  ] ; then
  getprop $1
else
#find: /info/sys_cfg -> /vendor/default.prop -> /vendor/build.prop  -> /system_dir/default.prop -> /system_dir/build.prop
  file="/info/sys_cfg"
  if [  -f "$file" ] ; then
    value=`grep $1 $file   |cut -d'=' -f2`
    if [ "$value" ] ; then
      echo $value
      return
    fi
  fi
  file="/vendor/default.prop"
  if [  -f "$file" ] ; then
    value=`grep $1 $file   |cut -d'=' -f2`
    if [ "$value" ] ; then
      echo $value
      return
    fi
  fi
  file="/vendor/build.prop"
  if [  -f "$file" ] ; then
    value=`grep $1 $file   |cut -d'=' -f2`
    if [ "$value" ] ; then
      echo $value
      return
    fi
  fi
  file="/system_dir/default.prop"
  if [  -f "$file" ] ; then
    value=`grep $1 $file   |cut -d'=' -f2`
    if [ "$value" ] ; then
      echo $value
      return
    fi
  fi
  file="/system_dir/build.prop"
  if [  -f "$file" ] ; then
    value=`grep $1 $file   |cut -d'=' -f2`
    if [ "$value" ] ; then
      echo $value
      return
    fi
  fi
fi
}


str_replace() {
  ok=`grep $1 $3`;
  if [ -z "$ok" ] ; then
    echo "$1=$2" >> $3;
  else
    t_v=`echo $2 |sed 's/\//\\\\\//g'`
    sed -i "s/^\($1\).*/\1=$t_v/" $3
  fi
}

check_get() {
  if [ "$result" == "null" -o "$result" == "_" ] ; then
    error_exit
  else
    echo -n $result
    exit 0
  fi
}
check_set() {
  if [ "$result" == "$1" ] ; then
    sync
    exit 0
  else
    error_exit
  fi
}

check_tag_num() {
  if [ $TAG_NUM -ne $1 ] ; then   
    error_exit
  fi
}

set_time_zone() {
  str_replace persist.sys.timezone $1  $info_sys_cfg
  if [ "$MODE" == "normal"  ] ; then
    setprop persist.sys.timezone $1
  fi
}

get_time_zone() {
  result=$(get_prop  persist.sys.timezone)
}

set_rf_freq() {
  str_replace rw.akuvox.rf.freq $1  $info_sys_cfg
  if [ "$MODE" == "normal"  ] ; then
    setprop rw.akuvox.rf.freq $1
    /system/bin/misc.sh
  fi
}

get_rf_freq() {
  result=$(get_prop  rw.akuvox.rf.freq)
}

set_country() {
  str_replace persist.sys.language `echo $1 | cut -d '_' -f1` $info_sys_cfg  &&str_replace persist.sys.country  `echo $1 | cut -d '_' -f2` $info_sys_cfg
  if [ "$MODE" == "normal"  ] ; then
    setprop persist.sys.language `echo $1 | cut -d '_' -f1` 
    setprop persist.sys.country  `echo $1 | cut -d '_' -f2`
  fi
  
  str_replace ro.product.locale.language `echo $1 | cut -d '_' -f1` /system/build.prop
  str_replace ro.product.locale.region  `echo $1 | cut -d '_' -f2` /system/build.prop
  str_replace ro.product.locale.language `echo $1 | cut -d '_' -f1` /system_dir/build.prop
  str_replace ro.product.locale.region  `echo $1 | cut -d '_' -f2` /system_dir/build.prop
}

get_country() {
  language=$(get_prop persist.sys.language)
  if [ ! "$language" ] ; then
    language=$(get_prop ro.product.locale.language)
  fi
  country=$(get_prop persist.sys.country)
  if [ ! "$country" ] ; then                                                                                                                           
    country=$(get_prop ro.product.locale.region)                                                                                                     
  fi
  result=`echo $language"_"$country`
}

set_rtc_time() {
  date  $1 > /dev/nul && hwclock -w
}

get_rtc_time() {
  result=`date +%m%d%H%M%Y.%S`
}

set_mac_addr() {
   rmac=`echo $2 |awk '{print substr($1,1,2) ":" substr($1,3,2) ":" substr($1,5,2) ":" substr($1,7,2) ":" substr($1,9,2) ":" substr($1,11,2)}'`
   #if [ "$1" == "eth0" ] ; then
       #ifconfig eth0 hw ether $rmac
   #fi
   str_replace $1 $rmac $info_network_interface 
}

get_mac_addr() {
  rmac=`cat $info_network_interface |grep $1`
  if [ "$rmac" ] ; then
    result=`echo $rmac |cut -d '=' -f2 | awk -F ':' '{print $1 $2 $3 $4 $5 $6}'`
  fi
}


set_serial_num() {
  check_tag_num 2
  str_replace serial $1 $info_network_interface
}

get_serial_num() {
  rserial=`cat $info_network_interface |grep serial`
  if [ "$rserial" ] ; then
    result=`echo $rserial | cut -d '=' -f2`
  fi
}

set_oem_info() {
  s=
}

get_oem_info() {
  result=$(get_prop ro.oem.name)
}

get_hw_version() {
  result=$(get_prop ro.hardware.model)-V$(get_prop ro.hardware.version)
}
get_hw_version1() {
  #get_prop ro.hw.version
  hw=`cat /sys/devices/platform/version-gpio/svp3300_version`
  case $hw in
    0)
      result="SVP3300W-V3.0"
      ;;
    1)
      result="SVP3300-V3.0"
      ;;
    2)
      result="SVP3300-V3.2"
      ;;
    3)
      result="SVP3300-V3.2"
      ;;
    4)
      result="SVP3300-V3.2"
      ;;
    5)
      result="SVP3300W-V3.12"
      ;;
    6)
      result="SVP3300W-V3.12"
      ;;
    7)
      result="SVP3300W-V3.1"
      ;;
    8)
      result="SVP3300-V3.1"
      ;;
    9)
      result="SVP3300F-V3.1"
      ;;
    10)
      result="SVP3300E-V3.1"
      ;;
    11)
      result="SVP3360-V1.0"
      ;;
    12)
      result="SVP3360-V1.0"
      ;;
    13)
      result="SVP3360-V1.0"
      ;;
    14)
      result="R47G-V3.1"
      ;;
    *)
      error_exit
      ;;
  esac
}

get_sw_version() {
  result=$(get_prop ro.oem.softver)
  #result=`cat /info/version |grep project |cut -d'=' -f2 `
}

get_hw_model() {
  #get_prop ro.hw.model
  #result="SVP3300-V3.0"
  result=$(get_prop ro.oem.model1)
}

get_device_logo() {
  file="/info/media/bootanimation.zip"
  if [  -f "$file" ] ; then
    result=$file
  fi
}

if [ $TAG_NUM -eq 0 ] ; then
  echo "eg : info_tool set_time_zone Asia/Shanghai"
  echo "     info_tool get_time_zone"
  echo "     info_tool set_rf_freq 869"
  echo "     info_tool get_rf_freq"
  echo "     info_tool set_country zh_CN"
  echo "     info_tool get_country"
  echo "     info_tool set_rtc_time 061820222013.23"
  echo "     info_tool get_rtc_time"
  echo "     info_tool set_mac_addr eth0 112233445566"
  echo "     info_tool get_mac_addr eth0"
  echo "     info_tool set_serial_num 123456789"
  echo "     info_tool get_serial_num"
  echo "     info_tool set_oem_info starnet"
  echo "     info_tool get_oem_info"
  echo "     info_tool get_hw_version"
  echo "     info_tool get_sw_version"
  echo "     info_tool get_hw_model"
  echo "     info_tool get_device_info"
  echo "     info_tool get_device_logo"
  error_exit
fi
init
function_name=$1
case $function_name in
# eg : infotool set_time_zone Asia/Shanghai
     set_time_zone)
		  check_tag_num 2
                  set_time_zone $2
                  get_time_zone
                  check_set $2
                  ;;
# eg: infotool get_time_zone
     get_time_zone)
		  check_tag_num 1
                  get_time_zone
                  check_get
                  ;;
# eg : infotool set_rf_freq 869
     set_rf_freq)
                  check_tag_num 2
                  set_rf_freq $2
                  get_rf_freq
                  check_set $2
                  ;;
# eg: infotool get_rf_freq
     get_rf_freq)
                  check_tag_num 1
                  get_rf_freq
                  check_get
                  ;;
# eg: infotool set_country zh_CN
     set_country)
                check_tag_num 2
                set_country  $2
                get_country
                check_set $2
                ;;
# eg: infotool get_country
     get_country)
		check_tag_num 1
                get_country
                check_get
                ;;
# eg: infotool set_rtc_time 061820222013.23
     set_rtc_time)
		check_tag_num 2
                 set_rtc_time $2
                 get_rtc_time
                 check_set $2
                 ;;
# eg: infotool get_rtc_time
     get_rtc_time)
		 check_tag_num 1
                 get_rtc_time
                 check_get
                 ;;
# eg: infotool set_mac_addr eth0 aa2233445566
     set_mac_addr)
		 check_tag_num 3
                 set_mac_addr $2 $3
                 get_mac_addr $2
                 check_set $3
                 ;;
# eg: infotool get_mac_addr eth0
     get_mac_addr)
		check_tag_num 2
                 get_mac_addr $2
                 check_get
                 ;;
# eg: infotool set_serial_num 123456789
     set_serial_num)
		  check_tag_num 2
                   set_serial_num $2
                   get_serial_num
                   check_set $2
                   ;;
# eg: infotool get_serial_num
     get_serial_num)
		  check_tag_num 1
                   get_serial_num
                   check_get
                   ;;
# eg: infotool set_oem_info starnet
     set_oem_info)
		check_tag_num 2
                 set_oem_info $2
                 get_oem_info
                 check_set $2
                 ;;
# eg: infotool get_oem_info
     get_oem_info)
		 check_tag_num 1
                 get_oem_info
                 check_get
                 ;;
# eg: infotool get_hw_version
     get_hw_version)
		  check_tag_num 1
                   get_hw_version
                   check_get
                   ;;
# eg: infotool get_sw_version
     get_sw_version)
		  check_tag_num 1
                   get_sw_version
                   check_get
                   ;;
# eg: infotool get_hw_model
     get_hw_model)
		check_tag_num 1
                 get_hw_model
                 check_get
                 ;;
      get_device_logo)
       check_tag_num 1
                get_device_logo
                check_get
                ;; 
# eg: info_tool get_device_info
     get_device_info)
		check_tag_num 1
		 touch /info/device_info.prop
		 chmod 644 /info/device_info.prop
		 echo "ro.oem.model=$(get_prop ro.oem.model1)" >/info/device_info.prop
		 echo "ro.oem.softver=$(get_prop ro.oem.softver)" >>/info/device_info.prop
		 echo "ro.oem.hardver=$(get_prop ro.oem.hwmodel)" >>/info/device_info.prop
		 echo "ro.project.version=$(get_prop ro.oem.softver)" >>/info/device_info.prop
		 result=`cat /info/device_info.prop`
                 check_get
		;;
     *)
      error_exit
      ;;
esac
error_exit
