#!/bin/bash

# Script to count C/C++ lines of code in the salmanoff project
# Excludes third-party dependencies, build artifacts, and generated files

# set -e  # Commented out to prevent early exit on empty file lists

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

echo -e "${BLUE}=== Salmanoff Project C/C++ Line Count ===${NC}"
echo

# Change to project root directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
cd "$PROJECT_ROOT"

echo -e "${YELLOW}Project root: $PROJECT_ROOT${NC}"
echo

# Find all C/C++ files, excluding third-party and build artifacts
CPP_FILES=$(find . -name "*.cpp" | grep -v third_party | grep -v build | grep -v b/ | grep -v "Livox-sdk-git" | grep -v CMakeFiles)
H_FILES=$(find . -name "*.h" -o -name "*.hpp" | grep -v third_party | grep -v build | grep -v b/ | grep -v "Livox-sdk-git" | grep -v CMakeFiles)
C_FILES=$(find . -name "*.c" | grep -v third_party | grep -v build | grep -v b/ | grep -v "Livox-sdk-git" | grep -v CMakeFiles)

# Count lines for each file type
echo -e "${BLUE}=== C++ Source Files (.cpp) ===${NC}"
CPP_LINES=0
if [ -n "$CPP_FILES" ]; then
    echo "$CPP_FILES" | xargs wc -l
    CPP_LINES=$(echo "$CPP_FILES" | xargs wc -l | tail -1 | awk '{print $1}')
else
    echo "No C++ source files found"
fi
echo

echo -e "${BLUE}=== Header Files (.h, .hpp) ===${NC}"
H_LINES=0
if [ -n "$H_FILES" ]; then
    echo "$H_FILES" | xargs wc -l
    H_LINES=$(echo "$H_FILES" | xargs wc -l | tail -1 | awk '{print $1}')
else
    echo "No header files found"
fi
echo

echo -e "${BLUE}=== C Source Files (.c) ===${NC}"
C_LINES=0
if [ -n "$C_FILES" ]; then
    echo "$C_FILES" | xargs wc -l
    C_LINES=$(echo "$C_FILES" | xargs wc -l | tail -1 | awk '{print $1}')
else
    echo "No C source files found"
fi
echo

# Calculate total
TOTAL_LINES=$((CPP_LINES + H_LINES + C_LINES))

echo -e "${GREEN}=== SUMMARY ===${NC}"
echo -e "C++ Source Files (.cpp): ${YELLOW}$CPP_LINES${NC} lines"
echo -e "Header Files (.h/.hpp):  ${YELLOW}$H_LINES${NC} lines"
echo -e "C Source Files (.c):     ${YELLOW}$C_LINES${NC} lines"
echo -e "${GREEN}─────────────────────────${NC}"
echo -e "${GREEN}Total C/C++ Lines:       ${YELLOW}$TOTAL_LINES${NC} lines${NC}"
echo

# Show what's excluded
echo -e "${BLUE}=== Excluded from count: ===${NC}"
echo "• third_party/ directory (googletest, etc.)"
echo "• build*/ directories (build artifacts)"
echo "• b/ directories (build artifacts)"
echo "• Livox-sdk-git/ directory (third-party SDK)"
echo "• CMakeFiles/ directories (generated files)"
echo "• Generated files (*.d, *.o, etc.)"
echo

# Optional: Show file count breakdown by directory
echo -e "${BLUE}=== Files by directory: ===${NC}"
echo "C++ Source Files:"
echo "$CPP_FILES" | sed 's|^\./||' | cut -d'/' -f1 | sort | uniq -c | sort -nr | head -10

echo
echo "Header Files:"
echo "$H_FILES" | sed 's|^\./||' | cut -d'/' -f1 | sort | uniq -c | sort -nr | head -10
