#!/usr/bin/env bash
set -u
set -o pipefail

LLAMA_DIR="/home/adminek/llama.cpp"
BUILD_DIR="$LLAMA_DIR/build"
OUT_DIR="${OUT_DIR:-$HOME/rocm-hip-diagnostics}"
LOG_FILE="$OUT_DIR/log.txt"

mkdir -p "$OUT_DIR"
exec 3>&1
log(){ echo "[$(date '+%F %T')] $*" | tee -a "$LOG_FILE" >&3; }
run(){ log "RUN: $*"; bash -lc "$*" >>"$LOG_FILE" 2>&1; return $?; }
section(){ log ""; log "=== $* ==="; }

: > "$LOG_FILE"
log "Start diagnostyki ROCm/HIP/CMake dla llama.cpp"
log "Katalog źródeł: $LLAMA_DIR"
log "Katalog build: $BUILD_DIR"
log "Katalog diagnostyczny: $OUT_DIR"

section "System"
run "date"
run "uname -a"
run "cat /etc/os-release"
run "id"
run "env | sort | egrep 'ROCM|HIP|HSA|LD_LIBRARY_PATH|PATH|CMAKE|CC|CXX' || true"

section "Pakiety"
run "dpkg -l | egrep 'rocm|hip|amdhip|rocm-core|rocblas|hipblas|clang|llvm|cmake' || true"
run "apt-cache policy cmake hipcc hip-runtime-amd hip-dev rocm-hip-runtime rocm-core rocm-llvm hipblas hipblas-dev hipblaslt hipblaslt-dev || true"

section "Pliki ROCm"
run "ls -ld /opt/rocm /opt/rocm-* 2>/dev/null || true"
run "find /opt/rocm -maxdepth 3 \( -name hipcc -o -name hipconfig -o -name amdclang++ -o -name clang++ -o -name hip-lang-config.cmake -o -name '*Config.cmake' \) 2>/dev/null | sort || true"
run "find /opt/rocm -maxdepth 5 \( -path '*/cmake/hip-lang/*' -o -path '*/hip/cmake/*' -o -path '*/lib/cmake/*' -o -path '*/lib64/cmake/*' \) 2>/dev/null | sort | head -n 300 || true"
run "ls -l /opt/rocm/bin /opt/rocm/llvm/bin 2>/dev/null || true"
run "ls -l /opt/rocm/lib /opt/rocm/lib64 2>/dev/null || true"
run "ls -l /opt/rocm/lib/rocblas/library/TensileLibrary*gfx906*.dat 2>/dev/null || true"

section "HIP narzędzia"
run "which hipcc hipconfig amdclang++ clang++ cmake || true"
run "hipcc --version || true"
run "hipconfig --full || true"
run "hipconfig --path || true"
run "hipconfig --rocmpath || true"
run "/opt/rocm/bin/rocminfo | head -n 200 || true"

section "CMake moduły"
run "cmake --version"
run "grep -n 'CMAKE_HIP_COMPILER_ROCM_ROOT\|hip-lang-config.cmake\|hipconfig --rocmpath' /usr/share/cmake-*/Modules/CMakeDetermineHIPCompiler.cmake 2>/dev/null || true"
run "sed -n '1,260p' /usr/share/cmake-3.22/Modules/CMakeDetermineHIPCompiler.cmake 2>/dev/null | tail -n 120 || true"

section "Repo llama.cpp"
run "test -d '$LLAMA_DIR'"
run "cd '$LLAMA_DIR' && git rev-parse --is-inside-work-tree && git rev-parse HEAD && git status --short || true"
run "cd '$LLAMA_DIR' && grep -RIn 'GGML_HIP\|LLAMA_HIPBLAS\|GGML_HIPBLAS\|enable_language(HIP)' CMakeLists.txt ggml 2>/dev/null | head -n 200 || true"

section "Minimalne próby CMake"
run "rm -rf '$OUT_DIR/test-build-1' && mkdir -p '$OUT_DIR/test-build-1'"
run "cd '$LLAMA_DIR' && cmake -S '$LLAMA_DIR' -B '$OUT_DIR/test-build-1' -DCMAKE_BUILD_TYPE=Release -DGGML_HIP=ON -DGGML_HIPBLAS=OFF -DAMDGPU_TARGETS=gfx906 || true"
run "test -f '$OUT_DIR/test-build-1/CMakeFiles/CMakeError.log' && cp '$OUT_DIR/test-build-1/CMakeFiles/CMakeError.log' '$OUT_DIR/CMakeError-test1.log' || true"
run "test -f '$OUT_DIR/test-build-1/CMakeFiles/CMakeOutput.log' && cp '$OUT_DIR/test-build-1/CMakeFiles/CMakeOutput.log' '$OUT_DIR/CMakeOutput-test1.log' || true"

run "rm -rf '$OUT_DIR/test-build-2' && mkdir -p '$OUT_DIR/test-build-2'"
run "cd '$LLAMA_DIR' && env ROCM_PATH=/opt/rocm HIP_PATH=/opt/rocm HIP_ROOT_DIR=/opt/rocm HIPCXX=/opt/rocm/llvm/bin/clang++ cmake -S '$LLAMA_DIR' -B '$OUT_DIR/test-build-2' -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=/opt/rocm -DROCM_ROOT=/opt/rocm -DHIP_ROOT=/opt/rocm -DHIP_PATH=/opt/rocm -DGGML_HIP=ON -DGGML_HIPBLAS=OFF -DAMDGPU_TARGETS=gfx906 || true"
run "test -f '$OUT_DIR/test-build-2/CMakeFiles/CMakeError.log' && cp '$OUT_DIR/test-build-2/CMakeFiles/CMakeError.log' '$OUT_DIR/CMakeError-test2.log' || true"
run "test -f '$OUT_DIR/test-build-2/CMakeFiles/CMakeOutput.log' && cp '$OUT_DIR/test-build-2/CMakeFiles/CMakeOutput.log' '$OUT_DIR/CMakeOutput-test2.log' || true"

run "rm -rf '$OUT_DIR/test-build-3' && mkdir -p '$OUT_DIR/test-build-3'"
run "cd '$LLAMA_DIR' && env ROCM_PATH=/opt/rocm HIP_PATH=/opt/rocm HIP_ROOT_DIR=/opt/rocm HIPCXX=/opt/rocm/bin/amdclang++ cmake -S '$LLAMA_DIR' -B '$OUT_DIR/test-build-3' -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=/opt/rocm -DROCM_ROOT=/opt/rocm -DHIP_ROOT=/opt/rocm -DHIP_PATH=/opt/rocm -DGGML_HIP=ON -DGGML_HIPBLAS=OFF -DAMDGPU_TARGETS=gfx906 || true"
run "test -f '$OUT_DIR/test-build-3/CMakeFiles/CMakeError.log' && cp '$OUT_DIR/test-build-3/CMakeFiles/CMakeError.log' '$OUT_DIR/CMakeError-test3.log' || true"
run "test -f '$OUT_DIR/test-build-3/CMakeFiles/CMakeOutput.log' && cp '$OUT_DIR/test-build-3/CMakeFiles/CMakeOutput.log' '$OUT_DIR/CMakeOutput-test3.log' || true"

section "Podsumowanie"
log "Diagnostyka zakończona. Najważniejsze pliki są w: $OUT_DIR"
log "Do analizy przydadzą się szczególnie: log.txt, CMakeError-test*.log, CMakeOutput-test*.log"
