#!/bin/bash
# by hzwuboxiao

rm -rf std_chk.error wubox.res
std_cfg_files=$(python -c 'import parserutil;parserutil.ConfigUtil("'$2'").get_prop_file_name()')
#错误配置项总数
wrong_item_nums=0
#字符最大宽度,用于格式化显示
max_width=0
#配置文件个数,用于统计每个配置文件的错误配置项个数
file_count=0
#文件是否错误的标志
error_file_flag=0
for std_cfg_file in $std_cfg_files
do
   error_file_flag=0
   std_prop_file=${std_cfg_file//[\[\',\]]/}
   files=`find $1/src/main/resources/performance/ -name  $std_prop_file`
   filename_written_flag=0
   std_prop_keys=$(python -c 'import parserutil;parserutil.ConfigUtil("'$2'").get_keys_by_prop_file("'$std_prop_file'")')
   for std_prop_key in $std_prop_keys;do
      std_prop_key_=${std_prop_key//[\[\',\]]/}
      std_prop_value=$(python -c 'import parserutil;parserutil.ConfigUtil("'$2'").get_value_by_key_and_section("'$std_prop_file'","'$std_prop_key_'")')
      check_value=`grep "$std_prop_key_=\(.*\)" $files | awk -F '=' '{print $2}'`
      if [ ! "$std_prop_value"x == "$check_value"x ];then
          if [ ! $error_file_flag -eq 1 ];then
             ((file_count++))
             echo "" 1>>std_chk.error
             error_file_flag=1
          fi
          ((wrong_item_nums++))
          if [ ! $filename_written_flag -eq 1 ];then
              echo "配置文件"${files##*/} 1>>std_chk.error
              echo "标准配置项separatorcharsperformance配置项" 1>>std_chk.error
              filename_written_flag=1
          fi
          current_width=`echo $std_prop_key_"="$std_prop_value | wc -L`
          if [ $current_width -gt $max_width ];then
              max_width=$current_width
          fi
          echo -e $std_prop_key_"="$std_prop_value"separatorchars\c" 1>>std_chk.error
          result=`grep $std_prop_key_"=.*" $files`
          if [ -z "$result" ];then
             echo "缺少左边的标准配置项" 1>>std_chk.error
          else
             echo $result 1>>std_chk.error
          fi
      fi
   done
done
file_count_pos=1
while [[ $file_count_pos -le $file_count ]];do
   if [[ $file_count_pos -eq $file_count ]];then
       error_item_pre=`grep -n ".properties" std_chk.error | head -"$file_count_pos" | tail -1 | awk -F ':' '{print $1}'`
       lines_nums=`cat std_chk.error | wc -l`
      # echo $lines_nums" === "$error_item_pre
       sed -i "$error_item_pre"'s/$/&有'"$((lines_nums-error_item_pre-1))"'个配置项错误/g' std_chk.error
       break
   fi
   error_item_pre=`grep -n ".properties" std_chk.error | head -"$file_count_pos" | tail -1 | awk -F ':' '{print $1}'`
   error_item_next=`grep -n ".properties" std_chk.error | head -"$((++file_count_pos))" | tail -1 |  awk -F ':' '{print $1}'`
   sed -i "$error_item_pre"'s/$/&有'"$((error_item_next-error_item_pre-3))"'个配置项错误/g' std_chk.error
   ((file_count_pos++))
done
echo $1"有"$wrong_item_nums"个配置项错误" 1>>wubox.res
cat std_chk.error | while read eachline;do
  left_val=`echo $eachline | awk -F 'separatorchars' '{print $1}'`
  right_val=`echo $eachline | awk -F 'separatorchars' '{print $2}'`
  is_title=`echo $left_val | grep '标准配置项'`
  if [[ "$is_title" != "" ]];then
      printf '%-'"$((max_width+20))"'s %-s\n' $left_val $right_val 1>>wubox.res
  else
      printf '%-'"$((max_width+15))"'s %-s\n' $left_val $right_val 1>>wubox.res
  fi
done
cat wubox.res