#!/usr/bin/env bash # Common utilities for strix-halo-optimizations scripts set -euo pipefail # Auto-detect project root SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[1]:-${BASH_SOURCE[0]}}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR" && while [[ ! -f Makefile ]] && [[ "$PWD" != "/" ]]; do cd ..; done; pwd)" if [[ "$PROJECT_ROOT" == "/" ]]; then # Fallback: assume lib/ is one level below project root PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" fi # Colors (disabled if not a terminal) if [[ -t 1 ]]; then RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[0;33m' BLUE='\033[0;34m'; CYAN='\033[0;36m'; BOLD='\033[1m' DIM='\033[2m'; RESET='\033[0m' else RED=''; GREEN=''; YELLOW=''; BLUE=''; CYAN=''; BOLD=''; DIM=''; RESET='' fi log_info() { printf "${BLUE}[INFO]${RESET} %s\n" "$*"; } log_success() { printf "${GREEN}[OK]${RESET} %s\n" "$*"; } log_warn() { printf "${YELLOW}[WARN]${RESET} %s\n" "$*"; } log_error() { printf "${RED}[ERR]${RESET} %s\n" "$*" >&2; } log_header() { printf "\n${BOLD}=== %s ===${RESET}\n" "$*"; } is_cmd() { command -v "$1" &>/dev/null; } require_root() { if [[ $EUID -ne 0 ]]; then log_error "This script requires root privileges. Run with sudo." exit 1 fi } confirm() { local prompt="${1:-Continue?}" printf "${YELLOW}%s [y/N] ${RESET}" "$prompt" read -r reply [[ "$reply" =~ ^[Yy]$ ]] } data_dir() { local subdir="${1:-.}" local dir="$PROJECT_ROOT/data/$subdir" mkdir -p "$dir" echo "$dir" } timestamp() { date '+%Y%m%d-%H%M%S'; }