#!/bin/sh
set -euo pipefail

# Simple autopkgtest for hipify: run both hipify-clang and hipify-perl

TMPDIR=$(mktemp -d)
cd "$TMPDIR"

cat > sample_clang.cu <<'EOF'
// Minimal CUDA-like API sample for hipify-clang that avoids CUDA language features
// Uses a forward declaration so no CUDA headers are needed.
extern int cudaDeviceSynchronize();
int main() { return cudaDeviceSynchronize(); }
EOF

cat > sample_perl.cu <<'EOF'
// Minimal CUDA sample for hipify-perl that exercises include translation
#include <cuda_runtime.h>
int main() { return 0; }
EOF

echo "[TEST 1] Testing hipify-clang (API call)..."

/usr/bin/hipify-clang --hip-kernel-execution-syntax \
  --extra-arg-before=-nocudainc \
  -o sample_clang.hip.cu \
  sample_clang.cu

# Check 1: output file exists
if [ ! -f sample_clang.hip.cu ]; then
  echo "[TEST 1] hipify-clang did not produce output file" >&2
  exit 1
fi

# Check 2: output contains the expected transformation
if grep -q "hipDeviceSynchronize" sample_clang.hip.cu; then
  echo "[TEST 1] hipify-clang transformed API call as expected"
  echo "[TEST 1] --- sample_clang.hip.cu (first 200 lines) ---"
  head -n 200 sample_clang.hip.cu
  echo
else
  echo "[TEST 1] hipify-clang did not transform API call" >&2
  echo "[TEST 1] --- sample_clang.hip.cu (first 200 lines) ---"
  head -n 200 sample_clang.hip.cu
  exit 1
fi

echo "[TEST 2] Testing hipify-perl..."
/usr/bin/hipify-perl sample_perl.cu > sample-perl.hip.cu

# Check 1: output file exists
if [ ! -f sample-perl.hip.cu ]; then
  echo "[TEST 2] hipify-perl did not produce output file" >&2
  exit 1
fi

# Check 2: output contains the expected include translation
if grep -q "hip_runtime.h" sample-perl.hip.cu; then
  echo "[TEST 2] hipify-perl produced hip_runtime.h as expected"
  echo "[TEST 2] --- sample-perl.hip.cu (first 200 lines) ---"
  head -n 200 sample-perl.hip.cu
  echo
else
  echo "[TEST 2] hipify-perl did not translate include to hip_runtime.h" >&2
  echo "[TEST 2] --- sample-perl.hip.cu (first 200 lines) ---"
  head -n 200 sample-perl.hip.cu
  exit 1
fi

# Check 3: CUDA syntax test (requires CUDA toolkit layout and headers)
echo "[TEST 3] Probing CUDA installation for optional CUDA syntax test..."
CUDA_PATH=""
CUDA_INCLUDE=""

# Try common CUDA layouts
NVCC_BIN=$(command -v nvcc 2>/dev/null || true)
NVCC_PREFIX=""
if [ -n "$NVCC_BIN" ]; then
  NVCC_DIR=$(dirname "$NVCC_BIN")
  NVCC_PREFIX=$(dirname "$NVCC_DIR")
fi
for cand in /usr/lib/cuda /usr/local/cuda "$NVCC_PREFIX"; do
  [ -z "$cand" ] && continue
  if [ -d "$cand" ]; then
    if [ -d "$cand/nvvm" ] && { [ -d "$cand/nvvm/libdevice" ] || [ -e "$cand/nvvm/libdevice" ]; }; then
      CUDA_PATH="$cand"
      break
    fi
  fi
done

# Determine include dir
if [ -n "$CUDA_PATH" ] && [ -d "$CUDA_PATH/include" ]; then
  CUDA_INCLUDE="$CUDA_PATH/include"
elif [ -f /usr/include/cuda_runtime.h ]; then
  CUDA_INCLUDE=/usr/include
fi

if [ -n "$CUDA_PATH" ] && [ -n "$CUDA_INCLUDE" ]; then
  echo "[TEST 3] Found CUDA_PATH=$CUDA_PATH and CUDA_INCLUDE=$CUDA_INCLUDE"
  echo "[TEST 3] Testing hipify-clang with CUDA syntax (__global__ + kernel launch)..."
  cat > sample_cuda_syntax.cu <<'EOF'
#include <cuda_runtime.h>
__global__ void kernel() {}
int main() { kernel<<<1,1>>>(); return 0; }
EOF

  /usr/bin/hipify-clang --hip-kernel-execution-syntax \
    --cuda-path="$CUDA_PATH" --extra-arg-before=-x --extra-arg-before=cuda -I"$CUDA_INCLUDE" \
    -o sample_cuda_syntax.hip.cu sample_cuda_syntax.cu

  # Check 1: output file exists
  if [ ! -f sample_cuda_syntax.hip.cu ]; then
    echo "[TEST 3] hipify-clang (CUDA syntax) did not produce output file" >&2
    exit 1
  fi

  # Check 2: output contains the expected kernel launch transformation
  if grep -q "hipLaunchKernelGGL" sample_cuda_syntax.hip.cu; then
    echo "[TEST 3] hipify-clang transformed kernel launch syntax as expected (CUDA syntax)"
    echo "[TEST 3] --- sample_cuda_syntax.hip.cu (first 200 lines) ---"
    head -n 200 sample_cuda_syntax.hip.cu
    echo
  else
    echo "[TEST 3] hipify-clang did not transform kernel launch syntax (CUDA syntax)" >&2
    echo "[TEST 3] --- sample_cuda_syntax.hip.cu (first 200 lines) ---"
    head -n 200 sample_cuda_syntax.hip.cu
    exit 1
  fi
else
  echo "[TEST 3] Skipping CUDA syntax test: CUDA toolkit layout not detected"
fi

echo "All hipify smoke tests passed."
exit 0
