#!/usr/bin/env bash
# Runs hasher and mirrors console output into the Hive miner log.

set -o pipefail

miner_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$miner_dir" || exit 1

[[ -f "$miner_dir/h-manifest.conf" ]] && . "$miner_dir/h-manifest.conf"

mkdir -p "$(dirname "$CUSTOM_LOG_BASENAME")"
log_file="${CUSTOM_LOG_BASENAME}.log"
config_file="${CUSTOM_CONFIG_FILENAME:-$miner_dir/hasher.conf}"
miner_bin="$miner_dir/hasher"

# Fresh log per miner start makes h-stats.sh parse only the current run.
: > "$log_file"

if [[ ! -x "$miner_bin" ]]; then
  echo "ERROR: hasher binary not found or not executable: $miner_bin" | tee -a "$log_file"
  echo "Put the miner binary at $miner_bin and run: chmod +x $miner_bin" | tee -a "$log_file"
  exit 1
fi

if [[ ! -f "$config_file" ]]; then
  echo "WARN: config file not found, creating it via h-config.sh" | tee -a "$log_file"
  "$miner_dir/h-config.sh"
fi

cmdline="$(cat "$config_file" 2>/dev/null)"

echo "Starting hasher with args: $cmdline" | tee -a "$log_file"

if command -v stdbuf >/dev/null 2>&1; then
  stdbuf -oL -eL "$miner_bin" $cmdline 2>&1 | tee -a "$log_file"
else
  "$miner_bin" $cmdline 2>&1 | tee -a "$log_file"
fi
