#!/usr/bin/env bash

algo=$(grep 'META' /hive-config/wallet.conf | awk -F '=' '{print $2}' | tr -d '"' | sed -n 's/.*coin:\([^}]*\).*/\1/p' | tr '[:upper:]' '[:lower:]')
stats=""
khs=0
isoula=1
if ps aux | grep 'apoolminer' | grep 'account' | grep -v 'grep'; then
	isoula=0	
fi

source $MINER_DIR/$CUSTOM_MINER/h-manifest.conf

if [ $isoula -eq 1 ]; then
	gpus_raw="default"
else
	gpus_raw=`curl -s --connect-timeout 3 --max-time 5 http://127.0.0.1:$MINER_REST_PORT/gpu`
fi

if [[ $? -ne 0 || -z $gpus_raw ]]; then
  echo -e "${RED}Failed to get gpus info from 127.0.0.1"
else
  
	if [ $isoula -eq 1 ]; then

algo='aleo'
version="v3.0.6"
stats=""
unit="S/s"
khs=0
hs=()
temp=()
fan=()
numbers=()

khs=$(cat  /hive/miners/custom/k3ok.com_apool_f2/aleominer.log |grep "Speed(S/s)" | awk 'END {print}'| awk '{print $3}')
khs=$(echo "scale=5; $khs / 1000" | bc)

khs_num=$(echo "$khs" | jq -R . | jq -s 'map(tonumber)[0]')

if [[ "$retime" =~ ^[0-9]+$ ]]; then
        if [ "$uptime" -gt "$retime" ]; then
                miner restart
                exit 0
        fi

fi

stats=$(jq -nc --argjson khs "$khs_num" \
        --arg uptime "$uptime" \
        --arg hs_units "$unit" \
        --arg ver "$version" \
        --arg algo "$algo" \
        '{$uptime, "hs_units":$hs_units, "ver":$ver, "algo":$algo, "khs":$khs}')

#		readarray -t arr < <(echo "$gpus_raw" | jq -cr '[.gpu[].proof_rate],.proofrate_total / 1000,.params.version,[.gpu[].bus_id],[.gpu[].temperature]')
#		hs="${arr[0]}"
#		khs="${arr[1]}"
#		version="${arr[2]}"
#		algo="aleo"
#		unit="hs"
#		bus_numbers="${arr[3]}"
#		temp="${arr[4]}"
#		fan=""
#		uptime=""
#  stats=$(jq -nc --arg khs "$khs" \
#                 --argjson hs "$hs" \
#                 --arg hs_units "$unit" \
#                 --argjson temp "$temp" \
#                 --arg ver "$version" \
#                 --arg algo "$algo" \
#                 --argjson bus_numbers "$bus_numbers" \
#                 '{$khs, $hs, "hs_units":$hs_units, 
#                   $temp,  $bus_numbers,
#                    $ver, $algo}')

	
	else
	  data=`echo "$gpus_raw" | jq -cr '.data'`
	  readarray -t temp_data < <(echo "$data" | jq -cr '.uptime, ([.gpus[].proof]|add/1000), [.gpus[].proof], [.gpus[].ctmp], [.gpus[].fan], ([.gpus[].valid]|add), ([.gpus[].inval]|add), [.gpus[].bus] ' 2>/dev/null)
	  unit="it"

	  uptime="${temp_data[0]/s/}"
	  khs="${temp_data[1]}"
	  khs=`echo $khs | sed -E 's/^( *[0-9]+\.[0-9]([0-9]*[1-9])?)0+$/\1/'` #1234.100 -> 1234.1
	  hs="${temp_data[2]}"
	  temp="${temp_data[3]}"
	  fan="${temp_data[4]}"
	  acceped="${temp_data[5]}"
	  rejected="${temp_data[6]}"
	  bus_numbers="${temp_data[7]}"
	  version="$CUSTOM_VERSION"
  
  stats=$(jq -nc --arg khs "$khs" \
                 --argjson hs "$hs" \
                 --arg hs_units "$unit" \
                 --argjson temp "$temp" \
                 --argjson fan "$fan" \
                 --arg acceped "$acceped" \
                 --arg rejected "$rejected" \
                 --arg uptime "$uptime" \
                 --arg ver "$version" \
                 --arg algo "$algo" \
                 --argjson bus_numbers "$bus_numbers" \
                 '{$hs, "hs_units":$hs_units, 
                   $temp, $fan, "ar": [ $acceped|tonumber, $rejected|tonumber], $bus_numbers,
                   "uptime":$uptime|tonumber|floor, $ver, $algo}')

	fi
  echo "$stats"
fi
