#!/bin/sh
# Start conformance test suite and print results                                      #  
# Created: Alexandra Reznik ext 298                                                   #  
#  Usage: ./conformance.sh <suite_name>.txt                                           # 
# Note: all *.xml files retrieved from $HOME/xml, data files retrieved from $HOME/csv #
#       results and *.log files stored in $HOME/results                               #
#######################################################################################  
dat=`date|nawk '{print $4"-"$2$3}'|nawk -F: '{print $1$2$3}'`;
dir=`pwd`;
data=$HOME/csv;
xml=$HOME/xml;
csv=$HOME/csv;
all_suites="regression.txt international_NS.txt international_SN.txt error.txt prisip_NS.txt prisip_SN.txt simring.txt mcpmnrec.txt mchmnrec.txt MCHBELL.txt BELL.txt BELLHO.txt diversion.txt";

mode=$1;
if [ -z "$mode" ]; then
  echo Insert mode: test or debug
  read mode
fi
rm $dir/*.csv;
logs=`ls -l $xml/*.log|wc -l`;
if [ $logs -ne 0 ]; then 
  echo Delete all log files? Y/N
    read answer
  if [ $answer = 'Y' ] || [ $answer = 'y' ]; then
     rm $xml/*.log;
  fi
fi

file=`cat conf_start.txt|awk '{print $1}'`;
res_name=`echo $file|awk -F\. '{print $1}'`;
results=$HOME/results/${res_name}_$dat;
mkdir $results;
total=`cat conf_start.txt|awk '{print $2}'`;
data_file=`cat conf_start.txt|awk '{print $3}'`;
msg=`cat conf_start.txt|awk '{print $4}'`;
features=`cat conf_start.txt|awk -F\( '{print $2}'|awk -F\) '{print $1}'`;
remote_ip=`cat conf_start.txt|awk -F\) '{print $2}'`;
remote_port=`echo $remote_ip|awk -F\: '{print $2}'`;
if [ -z "$remote_port" ]; then
   remote_port=5060;
fi

log_file="$dir/${file}_log_$dat";
result_file="$dir/${file}_results_$dat";
echo  results will be printed in $result_file;
sleep 2;
echo Start $file `date` > $result_file;
echo Start $file `date` > $log_file; 

uas_start()
{
  echo Start $uas_f >> $log_file;
  echo $uas_ext >> $log_file;
  case $uas_ext in 
#    xml) ./sipp -sf $uas_f -inf $csv/$table -m $total -trace_err -trace_stat -stf ${uas_file}-${dat}.csv $msg $features -p 5061 -trace_screen -bg;
    xml) ./sipp -sf $uas_f -inf $csv/$table -m $total -trace_err -trace_stat -stf ${uas_file}-${dat}.csv $msg $features -p 5080 -trace_screen -bg;
    sleep 1;;
    txt) $uas_f $dat;;
    *) echo $uas_f unknown case >> $log_file;;
  esac 
 uas_act=`ps -ef |grep -v grep|grep -v vi|grep sipp|grep uas|nawk '{print $2}'`;
 #return $uas_act;
}
uac_start()
{ 
  echo $1 verified >> $log_file;
  uac_active=`ps -ef |grep sipp|grep uac|grep -v grep|wc -l`;
  if [ $uac_active -ne 0 ]; then
     echo uac not released >> $log_file;
     ps -ef |grep sipp|grep uac|grep -v grep|nawk '{print $2}'|xargs kill;  
  fi
  echo Start $1 >> $log_file;
  echo $uac_ext >> $log_file; 
  case $uac_ext in
   xml) ./sipp -sf $xml/$1 -trace_err -inf $csv/$table -m $total $remote_ip $msg -trace_screen $features;
        if test $? -ne 0
        then
          return 1;
        else
          return 0;
        fi;;
   txt) $uac_f $dat;;
   *) echo $uac_f unknown case >> $log_file;;
  esac 
}    

result_csv()
{
  d=`date '+%Y-%m-%d'`;
  stat=`tail -1 $1 |awk -F\; '{print $12 $15 $17}'`;
  run_file=`echo $1 |awk -F\- '{print $1}'`;
  for i in $stat
  do
   case $i in
     000) echo $run_file RUN >> $log_file; res="99";;
     110) echo $run_file status = $i >> $log_file; res="0";;
     220) echo $run_file status = $i >> $log_file; res="0";;
     101) echo $run_file status = $i >> $log_file; res="1";;
      *)  echo $run_file status = $i >> $log_file; res="99";;
   esac
  done
} 

print_result()
{
  res=$1;  
  case $res in 
    0) echo Test $2 PASS >>  $result_file;;
    1) echo Test $2 FAIL >>  $result_file;
      if [ $mode = "debug" ]; then
         a="break";
      fi;;
    *) echo Test $2 UNKNOWN >> $result_file;; 
  esac
}

check_ext()
{
   ext=`echo $1|awk -F\. '{print $2}'`;
   case $ext in
     xml) dr=`echo $xml`;;  
     txt) dr=`echo $dir`;;
     *)   echo "!!!! unknown  extension !!!!!!!";
          exit;;
   esac
}
proxy=`grep proxy $file`;
if [ -n "$proxy" ]; then
  $dir/proxy_pr start;
  sleep 10;
fi
file_n=`cat $file |wc -l`;
${dir}/check_clients.sh $result_file &
uac_file=`grep uac $file | nawk '{print $1}'`;
file_n=`grep uac $file |wc -l`;
echo Number of tests $file_n >> $result_file;
uas_pid="0";
for y in $uac_file
do
 a="continue";
 check_ext $y;
 uac_ext=$ext;
 uac_f=${dr}/$y;
 uas_file=`grep $y $file |nawk '{print $2}'`;
 if [ -n "$uas_file" ]; then
  check_ext $uas_file;
  uas_ext=$ext;
  uas_f=$dr/$uas_file;
 else
  uas_f=$uac_f;
 fi 
 table=`grep $y $file |nawk '{print $3}'`;
 if [ -z "$table" ]; then
    table=$data_file;
 fi
 echo $table for $y >> $log_file;
 echo uac=$uac_f uas=$uas_f >> $log_file;
 if [ -f $uas_f ] && [ -f $uac_f ]; then
  echo Launching $y >> $log_file;
  if [ -n "$uas_file" ]; then
   uas_start;
   uas_pid=$uas_act;
   echo  uas pid = $uas_pid >> $log_file;
  fi
   uac_start $y;
   uac_res=`echo $?`;
   if [ "$uac_ext" = xml ]; then
     display_file=`grep $y $result_file`; #validate if file's result printed in $result_file already
   else
    echo uac_ext=$uac_ext uac file=$y result=$uac_res >> $log_file;
    display_file=1;
   fi
   if [ -z "$display_file" ]; then
     echo display_file=$display_file >> $log_file;
     print_result $uac_res $y;
   fi
   sleep 20;
   name=`echo $y|awk -F\. '{print $1}'|awk -F_ '{print $2}'`;
   res_f=`ls *_${name}*-${dat}.csv`;
   if [ -n "$res_f" ]; then 
     for r in $res_f
     do 
          file_done=`echo $r|awk -F- '{print $1}'`;
          display_file=`grep $file_done $result_file|wc -l`;
          if [ $display_file -eq 0 ]; then  #verify if the $r file already displayed in $result_file
             result_csv $r;               #if not displayed, insert the file to $result_file
             print_result $res $run_file;
          fi
     done;
   fi
   sleep 5;
   uas_active=`ps -ef |grep -v grep|grep sipp|grep uas|wc -l`;
   if  [ $uas_active -ge "1" ]; then
      uas_id=`ps -ef|grep -v grep|grep uas|nawk '{print $2}'`;
      for u in $uas_id
      do
       for n in $uas_pid
       do
        if [ $n -eq "$u" ]; then
           /usr/bin/kill -USR2 $uas_pid;
        fi
       done  
      done
      ps -ef|grep -v grep|grep uas|nawk '{print $2}'|xargs kill;
   fi
   $a;
 else
  echo !!ERROR file not exists $uas_f or $uac_f >> $result_file;
 fi
done
echo Stop $file `date` >> $result_file;
$dir/conf_results.sh $result_file;
sleep 3;
ps -ef|grep sipp|grep check_clients.sh|grep -v grep|nawk '{print $2}'|xargs kill;
sleep 1;
if [ -n "$proxy" ]; then
  $dir/proxy_pr stop;
fi
mv $dir/*${dat}* $results/
cd $xml 
lfile=`ls *.log`;
for i in $lfile
do
   #f=`echo $i |nawk -F_ '{print $1"_"$2"_"$4}'`;
   mv $i $results/${i}-${dat};
done
echo "--------------RESULTS moved to $results----------------";
echo "  ";
exit;
