#!/bin/bash
# Wrapper script to start cgminer with correct flags based on selected mode

# Ensure we are in the script directory
cd "$(dirname "$0")"

MODE_FILE="mining_mode.conf"
CONF_FILE="cgminer.conf"
LOG_FILE="cgminer.log"

# Kill any existing instances that might block the port/device
killall -9 cgminer 2>/dev/null
sleep 1

# Cleanup stale semaphore locks from potential crashes
rm -f /tmp/cgminer-usb-*

# Default mode
MODE="sha256"

# Force SHA256 flags (no --scrypt)
FLAGS=""

# Ensure correct pool is used (Debugging hint)
echo "[$(date)] Starting miner in SHA256 ONLY mode" >> "$LOG_FILE"
echo "[$(date)] Config file: $CONF_FILE" >> "$LOG_FILE"

# Execute cgminer
# Forced API listening via command line args to ensure it's active
# Added --api-allow W:127.0.0.1 explicitly just in case config is ignored
exec /usr/local/bin/cgminer $FLAGS --gridseed-options freq=850 --api-listen --api-allow W:127.0.0.1 -c "$CONF_FILE" >> "$LOG_FILE" 2>&1
