#!/bin/bash

# Define the output log file
LOGFILE="code_metrics.log"

# start with a clean log file
> "$LOGFILE"

# --- Start of Log Entry ---
# Append a separator and a timestamp to the log file
echo "==================================================" >> "$LOGFILE"
echo "Log Entry: $(date +'%Y-%m-%d %H:%M:%S')" >> "$LOGFILE"
echo "--------------------------------------------------" >> "$LOGFILE"

# --- Command 1: Ctags Prototype Count ---
echo "Function Prototypes in Headers:" >> "$LOGFILE"
ctags -x --c-kinds=p *.h tokenizer/*.h | awk '{count[$4]++} END {total=0; for (f in count) {printf "%-20s %d\n", f, count[f]; total += count[f]} printf "%-20s %d\n", "TOTAL", total}' >> "$LOGFILE"
echo "" >> "$LOGFILE" # Add a blank line for readability

# --- Command 2: Word Count (Lines of Code) ---
echo "Lines of Code (excluding _test.c):" >> "$LOGFILE"
# Note: This command can fail if no .c/.h files exist. We add '2>/dev/null' to suppress errors.
wc -l $(ls *.c *.h tokenizer/*.c tokenizer/*.h 2>/dev/null | grep -v '_test\.c$') | awk '{if ($2 != "total") {total += $1; printf "%-20s %d\n", $2, $1}} END {printf "%-20s %d\n", "TOTAL", total}' >> "$LOGFILE"

# --- End of Log Entry ---
echo "==================================================" >> "$LOGFILE"
echo "" >> "$LOGFILE" # Add a final blank line

echo "Code metrics have been appended to $LOGFILE"