#!/bin/bash

while sleep 2; do

my_string=( $(ioreg -l |grep \"PerformanceStatistics\" | cut -d '{' -f 2 | tr '|' ',' | tr -d '}' | tr ',' '\n'|grep 'Temp\|Fan\|Clock'|cut -d\" -f3|tr -d '='|tr '\n' ',' ) );
IFS=',' read -ra my_array <<< "$my_string"
printf "$(date "+%Y-%m-%d %H:%M:%S")" >> ~/CSVServer/csv/GPUFanPercent.txt && printf ',' >> ~/CSVServer/csv/GPUFanPercent.txt && echo ${my_array[0]} >> ~/CSVServer/csv/GPUFanPercent.txt
printf "$(date "+%Y-%m-%d %H:%M:%S")" >> ~/CSVServer/csv/GPUCoreClock.txt && printf ',' >> ~/CSVServer/csv/GPUCoreClock.txt && echo ${my_array[1]} >> ~/CSVServer/csv/GPUCoreClock.txt
printf "$(date "+%Y-%m-%d %H:%M:%S")" >> ~/CSVServer/csv/GPUMemoryClock.txt && printf ',' >> ~/CSVServer/csv/GPUMemoryClock.txt && echo ${my_array[2]} >> ~/CSVServer/csv/GPUMemoryClock.txt
printf "$(date "+%Y-%m-%d %H:%M:%S")" >> ~/CSVServer/csv/GPUFanSpeed.txt && printf ',' >> ~/CSVServer/csv/GPUFanSpeed.txt && echo ${my_array[3]} >> ~/CSVServer/csv/GPUFanSpeed.txt
printf "$(date "+%Y-%m-%d %H:%M:%S")" >> ~/CSVServer/csv/GPUtemp.txt && printf ',' >> ~/CSVServer/csv/GPUtemp.txt && echo ${my_array[4]} >> ~/CSVServer/csv/GPUtemp.txt;
done;
