#!/bin/bash
#Copyright (c) 2018, Intel Corporation All Rights Reserved.

#The source code, information and material ("Material") contained #herein is owned by Intel Corporation 
#or its suppliers or licensors, and title to such Material remains #with Intel Corporation or its 
#suppliers or licensors. The Material contains proprietary #information of Intel or its suppliers 
#and licensors. The Material is protected by worldwide copyright laws #and treaty provisions. No part of 
#the Material may be used, copied, reproduced, modified, published, uploaded, posted, transmitted, 
#distributed or disclosed in any way without Intel's prior express written permission. 
#No license under any patent, copyright or other intellectual property rights in the Material is granted 
#to or conferred upon you, either expressly, by implication, inducement, estoppel or otherwise. Any 
#license under such intellectual property rights must be express and approved by Intel in writing.

#Unless otherwise agreed by Intel in writing, you may not remove or alter this notice or any other 
#notice embedded in Materials by Intel or Intels suppliers or licensors in any way.

#this script automates the launch of benchmark avc on  every node.


#Help
if [ "$1" == "--help" ] || [ "$1" == "-h" ] || [ "$1" == "-help" ]; then
	echo "$(cat help.txt)"
	

	exit 0
fi

#license information 
echo "For the purposes of AVC benchmark a big_buck_bunny test clip was downloaded from https://peach.blender.org/about/. Peach open movie project has been licensed under the Creative Commons Attribution 3.0 license."


#Variable declaration

BASEDIR=$(dirname $0)
SPATH=$BASEDIR

VCACTL=/usr/sbin/vcactl
NPATH3=/mnt/vvvalidation/tools/h264/logs
SPATH="$( cd "$(dirname "${BASH_SOURCE[0]}" )" && pwd )"
SPATH2="$(echo ${SPATH%/*})"
SPATH1="$(echo ${SPATH2%/*})"
SPATH3=$SPATH2/h264/logs
SPATH4=$SPATH2/h264/
FILE=$SPATH/MSS_verify.log
vvnodes=$($VCACTL status | wc -l)

echo "Node status"
$VCACTL status




#check if all nodes are up and running
nodes=0
$VCACTL network ip &> $SPATH/bridging/ips_.txt
nodes=`grep -o 'ERROR' $SPATH/bridging/ips_.txt |wc -l`
#nodes=`awk 'END{print NR}' $SPATH/bridging/ips.txt`

if [ $nodes == 0 ] ; then 
echo "Nodes are Network Ready"
else
echo "*****Benchmark needs all nodes in NET_DEVICE_READY state*****"
exit 0
fi

#remove the ips.txt file if already existing
if [ -f $SPATH/bridging/ips_.txt ] ; then rm -f $SPATH/bridging/ips_.txt; fi
#remove the temparature logs already existing
if [ -f $SPATH3/avc_benchmark_temp_log.txt ] ; then rm -f $SPATH3/avc_benchmark_temp_log.txt; fi
if [ -f $SPATH3/avc_benchmark_sorted_temp_log.txt ] ; then rm -f $SPATH3/avc_benchmark_sorted_temp_log.txt; fi


#Get the node IP's in ips.txt file
$VCACTL network ip | grep -v ":" > $SPATH/bridging/ips.txt

#Start Benchmark by first running Keygen, to avoid user input
echo "Running Keygen"
chmod +x keygen
./keygen 2>/dev/null


#**Assigning scripts proper permissions**
echo "Initiating node script"
rm -f $SPATH3/*.txt;
chmod +x $SPATH/density $SPATH4/scripts/generate_parfile_jobfile $SPATH4/scripts/clean_files $SPATH4/scripts/copy_files $SPATH4/bin/sample_multi_transcode_pv3 $SPATH4/bin/sample_multi_transcode_pv4 $SPATH/MSS_verification.sh;
chmod +x $SPATH/avc_benchmark_temperature.sh $SPATH/avc_job.sh


#Verify if MSS is installed
echo "Verifying if MSS installed on all nodes and then running AVC benchmark"


#remove the MSS_Verify log of each node file is already exist
for i in `cat $SPATH/bridging/ips.txt`;
do
   if [ -f $SPATH3/$i"_MSS_verify.log" ] ; then rm -f $SPATH3/$i"_MSS_verify.log"; fi
done

#Run the MSS Verification script on each node and create a log file for each node
for i in `cat $SPATH/bridging/ips.txt`;
do
   scp -q $SPATH/MSS_verification.sh root@$i:/root   
   ssh root@$i "cd /root; ./MSS_verification.sh" > $SPATH3/$i"_MSS_verify.log"
done

#Flag to check if MSS is installed on each node . The benchmark currently requires all nodes to have MSS installed
mss_flag=0
mss_count=0



#Check if MSS_verify.log files for each node are generated
for i in `cat $SPATH/bridging/ips.txt`;
do
	 if [ -s $SPATH3/$i"_MSS_verify.log" ]; then		
		mss_flag=$((mss_flag+1));

		
	 else
		mss_flag=0;
 		echo "***MSS is not installed, Please install MSS on $i node and rerun*** "
		
	 fi
	mss_count=$((mss_count+1));

done


if [ $mss_flag == $mss_count ]; 
then
   echo "****MSS is installed correctly on $mss_flag nodes, continuing to run benchmark script***"
 	
	
	   ./avc_benchmark_temperature.sh $SPATH3/avc_benchmark_temp_log.txt &
	
   sleep 3
	 for i in `cat $SPATH/bridging/ips.txt`;
	  do
	    scp -r -q $SPATH1 root@$i:/mnt/ 
	    scp -q $SPATH/density root@$i:/root
	  done
          echo "Copying files to nodes complete"
         pids=""
	 for i in `cat $SPATH/bridging/ips.txt`;
	  do
	    #ssh root@$i "cd /root; ./density $i" &> /dev/null 2>&1
	    ./avc_job.sh $i &
            pids+=$!
            pids+=" "
	  done
   echo "Waiting for background jobs to finish "
          wait $pids

	sleep 5
	# **Parsing Streams achieved fps results**

        k="_*.txt"
	for i in `cat $SPATH/bridging/ips.txt`;
	do  
         j="$i$k"
	 #echo "scp -r -q root@$i:$NPATH3/$j $SPATH3/"
	 scp -r -q root@$i:$NPATH3/$j $SPATH3/
	done
	

	k="_results.txt"
	node_count=0
	card_count=0

	for i in `cat $SPATH/bridging/ips.txt`;
	do  
		 j="$i$k"
		awk -v var="$i" -v node_no="$node_count" -v card_no="$card_count" 'BEGIN{}{ if ($4 >= 30){ print "Card:",card_no, "Node:",node_no, "IP Address: " var,"achieved_fps:",$4,"Streams:", $6;} } END{print done}' $SPATH3/$j
		node_count=$((node_count+1))

		if [ $node_count == 3 ]; then
		card_count=$((card_count+1))
		node_count=0
		fi

	done
	
	sleep 2

	#Sending Signal to Temparature log script
	pkill -SIGUSR1 -e -f  avc_benchmark_temperature &> /dev/null &
	#pkill -9 -e -f  avc_benchmark_temperature &> /dev/null &
	sleep 1
	echo "Parsing Temparature log"
	
        sed 's/\((.*)\)//g' $SPATH3/avc_benchmark_temp_log.txt |sed 'N;N;N;N;s/\n/ /g' |  sed 's/+//g' | sed 's/°C//g'| sed 's/Core .://g' |  sed 's/://g' > $SPATH3/avc_benchmark_t_temp_log.txt
        
        if [ -f $SPATH3/avc_benchmark_sorted_temp_log.txt ] ; then rm -f $SPATH3/avc_benchmark_sorted_temp_log.txt; fi

	node_count=0
	card_count=0

	for i in `cat $SPATH/bridging/ips.txt`;
	do 
		 grep """Card "$card_count" Cpu "$node_count"" $SPATH3/avc_benchmark_t_temp_log.txt >> $SPATH3/avc_benchmark_sorted_temp.txt
		node_count=$((node_count+1))

		if [ $node_count == 3 ]; then
		card_count=$((card_count+1))
		node_count=0
		fi

	done
        if [ -f $SPATH3/avc_benchmark_t_temp_log.txt ] ; then rm -f $SPATH3/avc_benchmark_t_temp_log.txt; fi
	echo "Completed Successfully , Kindly review logs under h264/logs folder"

	exit 0

## Break if MSS is not installed. 

else
  exit 0
fi


