#!/bin/bash

source /opt/tool/pvi_test.sh
source /opt/tool/icm20602.sh
source /opt/tool/get-cpu-temp.sh
source /opt/tool/get-sata-reg-val.sh
source /opt/tool/get-sata-state.sh
source /opt/tool/get-sata-err-log.sh

pvi_init
icm20602_init

FILE="/opt/collect/collect.log"
#MAX Size=22G
FILE_MAX_SIZE=23622320128

function get_file_size() {
echo `ls -l ${FILE} | awk '{ print $5 }'`
}

function collect_info_save() {
pvi_temp_get pviTemp
pvi_current_get Isource
pvi_volt_get Vdd
pvi_pratio_get Psource

temp_get  icmTemp
accel_get AccelX AccelY AccelZ
gyro_get  GyroX  GyroY  GyroZ 

cpu_temp_get TempCpu0 TempCpu1

echo -e "I: ${Isource}    V: ${Vdd}    P: ${Psource}    T: ${pviTemp}\n" >> $FILE
echo -e "AccelX: ${AccelX}\tAccelY: ${AccelY}\tAccelZ: ${AccelZ}\tT: ${icmTemp}" >> $FILE
echo -e "GyroX : ${GyroX}\tGyroY : ${GyroY}\tGyroZ : ${GyroZ}\n" >> $FILE
echo -e "TempCpu0: ${TempCpu0}\tTempCpu1: ${TempCpu1}\n" >> $FILE

#argc=$#
#echo ${argc}
for arg in $*
do
echo -e "${arg} collect information:" >> $FILE
sata_reg_val_get ${arg} >> $FILE
ST=`sata_health_get ${arg}`
echo "SataHealthSt: ${ST}" >> $FILE
sata_err_log_get ${arg} >> $FILE     
done
}

SataName="/dev/sda /dev/sdb /dev/sdc /dev/sdd /dev/sde"

if [ $# -eq 1 ]; then
    while [ true ]                                                                  
    do 

	current_time=$(date "+%Y-%m-%d %H:%M:%S")
	echo -e "\n****************Collect Time: ${current_time}****************" >> $FILE                                                                                              
	collect_info_save ${SataName}
	
	Size=`get_file_size`
	#echo ${Size}
    if [ $Size -gt $FILE_MAX_SIZE ] ; then 
      echo > ${FILE}
#     echo "clear"
    fi

    sleep $1
    done
else
	echo "Usage:saturn-cycle-collect.sh times"
fi



 
