#!/bin/bash

get_totals(){
    TOTS="$1"
    TOTAL=0
    for n in $TOTS ; do 
    TOTAL=$(( $TOTAL + $n))
 done
    echo $TOTAL
}

if [ "$1" == "" ] ; then
    echo Must give a kernel version with a v
    exit -1
fi

echo Getting line counts for $1, unsorted first:
rm -f /tmp/junkLC /tmp/eMpTy ; touch /tmp/eMpTy

cd /usr/src/linux
git checkout $1

for dirs in $(find . -maxdepth 1 -type d  ); do

    H_FILES=$(find $dirs -type f -name "*.h" | xargs wc -l /tmp/eMpTy \
	| grep " total" | awk ' {print $1 }')
    
    H_TOT=$(get_totals "$H_FILES")
    
    C_FILES=$(find $dirs -type f -name "*.c" | xargs wc -l /tmp/eMpTy\
	| grep " total" | awk ' {print $1 }')
    C_TOT=$(get_totals "$C_FILES")
    
    S_FILES=$(find $dirs -type f -name "*.S" | xargs wc -l /tmp/eMpTy\
	| grep " total" | awk ' {print $1 }')
    S_TOT=$(get_totals "$S_FILES")
    
    TOTAL=$(( $H_TOT + $C_TOT + $S_TOT ))

#printf "%-20s%12d%12d%12d%15d\n" $dirs $S_TOT $H_TOT $C_TOT $TOTAL
printf "%-20s%12d%12d%12d%15d\n" $dirs $S_TOT $H_TOT $C_TOT $TOTAL >> /tmp/junkLC

done

echo Sorted totals for $1:
echo $1 >/tmp/sortLC
printf "\n%-20s%12s%12s%12s%15s\n\n" "Directory" ".S files" ".h files" ".c files" "TOTAL " >> /tmp/sortLC

sort -r -n -k5 /tmp/junkLC >> /tmp/sortLC

rm -f /tmp/eMpTy /tmp/junkLC

cat /tmp/sortLC

#restore original state if possible, won't work on rc
#git checkout $(uname -r)





