#!/usr/bin/env bash

logfile="/var/log/miner/custom/mcminer.log"

declare -A hs
unit="S/s"
algo="ama"
version="0.2.0"

# Pull GPU stats arrays from Hive env if present (jq expects gpu_stats JSON)
gpu_stats_nvidia=$(jq '[.brand, .temp, .fan, .power, .busids, .mtemp, .jtemp] | transpose | map(select(.[0] == "nvidia")) | transpose' <<< $gpu_stats)
gpu_temp=$(jq -c '[.[1][]]' <<< "$gpu_stats_nvidia")
gpu_fan=$(jq -c '[.[2][]]' <<< "$gpu_stats_nvidia")
gpu_bus=$(jq -c '[.[4][]]' <<< "$gpu_stats_nvidia")
gpu_count_env=$(jq '.busids | select(. != null) | length' <<< $gpu_stats)

# Miner/Worker names from banner
miner_name=$(grep "Moonclub Name:" "$logfile" | tail -n1 | awk -F': ' '{print $2}')
worker_name=$(grep "Moonclub Worker:" "$logfile" | tail -n1 | awk -F': ' '{print $2}')

# Uptime from either Moonclub Uptime or generic Uptime
last_uptime=$(grep -E "Moonclub Uptime:|Uptime:" "$logfile" | tail -n1 | awk -F': ' '{print $2}')
uptime=0
if [[ $last_uptime =~ ([0-9]+)m([0-9]+)s ]]; then
  minutes=${BASH_REMATCH[1]}; seconds=${BASH_REMATCH[2]}; uptime=$((minutes*60+seconds))
elif [[ $last_uptime =~ ([0-9]+)s ]]; then
  uptime=${BASH_REMATCH[1]}
fi

# GPU count from log if available
gpu_count=$(grep -E "Moonclub Number of GPUs:|Number of GPUs:" "$logfile" | tail -n1 | awk '{print $NF}')
[[ -z "$gpu_count" ]] && gpu_count=$gpu_count_env
[[ -z "$gpu_count" ]] && gpu_count=0

# Per-GPU hashrate lines: "Moonclub GPU i: <rate> attempts/sec"
declare -a gpu_ats
for i in $(seq 0 $((gpu_count - 1))); do
  raw_val=$(grep "Moonclub GPU $i:" "$logfile" | tail -n1 | sed -E "s/.*GPU $i: ([0-9,.]+) attempts\/sec.*/\1/")
  if [[ -z "$raw_val" ]]; then
    # fallback to generic "GPU i:" without prefix
    raw_val=$(grep "GPU $i:" "$logfile" | tail -n1 | sed -E "s/.*GPU $i: ([0-9,.]+) attempts\/sec.*/\1/")
  fi
  val=${raw_val//,/}
  [[ -z "$val" ]] && val=0
  gpu_ats[$i]=$val
done

# Total kH/s
total_attempts=0
for v in "${gpu_ats[@]}"; do
  total_attempts=$(echo "$total_attempts + $v" | bc)
done
total_khs=$(echo "scale=3; $total_attempts / 1000" | bc)

# Per-GPU kH/s
for ((i=0; i<gpu_count; i++)); do
  hs[$i]=$(echo "scale=3; ${gpu_ats[$i]} / 1000" | bc)
done

# Accepted/Rejected shares
accepted_by_pool=$(grep -E "Moonclub Accepted by Pool:|Accepted by Pool:" "$logfile" | tail -n1 | awk '{print $NF}')
rejected_by_pool=$(grep -E "Moonclub Rejected by Pool:|Rejected by Pool:" "$logfile" | tail -n1 | awk '{print $NF}')
[[ -z "$accepted_by_pool" ]] && accepted_by_pool=0
[[ -z "$rejected_by_pool" ]] && rejected_by_pool=0

# JSON out
hs_json=$(printf '%s\n' "${hs[@]}" | jq -R . | jq -s .)

stats=$(jq -n       --arg miner "$miner_name"       --arg worker "$worker_name"       --argjson gpu_count "$gpu_count"       --argjson uptime "$uptime"       --argjson total_khs "$total_khs"       --arg algo "$algo"       --arg ver "$version"       --argjson temp "${gpu_temp}"       --argjson fan "${gpu_fan}"       --arg hs_units "$unit"       --argjson hs "$hs_json"       --argjson accepted "$accepted_by_pool"       --argjson rejected "$rejected_by_pool"       '{
    miner: $miner,
    worker: $worker,
    uptime: $uptime,
    total_khs: $total_khs,
    algo: $algo,
    ver: $ver,
    $temp,
    $fan,
    hs_units: $hs_units,
    hs: $hs,
    ar: [$accepted, $rejected],
    bus_numbers: $gpu_count
  }')

echo "$stats"
echo "$total_khs"
