#!/bin/sh

. ./conf.py

test x"min_lines" = x"" && exit 1

test -f "$1" || exit $?

sort -t: -k4 -rn -- "$1" >"$1.sorted"

# We do not sort .filtered file because
# it is in fact faster to recompile kernel when all changes
# to same file are tested in sequence.
# For example, if you touch asm/bitops.h, almost everything gets recompiled.
# When you revert that, and touch something else, everything gets recompiled
# AGAIN. Better to touch asm/bitops.h N times in a row, than N times
# interspersed by other files!
cat -- "$1" \
| {
	# This works: grep -e ':[1-9][0-9][0-9]*$' -e ":[${min_lines}-9]\$"
	# but only for min_lines <= 9. Do it hard(er) way for bigger min_lines:
	while IFS=: read -r file line func line_cnt; do
		test "$line_cnt" -ge "$min_lines" && echo "$file:$line:$func:$line_cnt"
	done
  } \
| grep -F -e '.h:' -e '.c:'  \
| sh -c "$exclude_files" >"$1.filtered"

mkdir -p "WORK"
exec <"$1.filtered"
oldfunc=""
seq=0
outname=`printf "%s.%05d" "$1.filtered" "$seq"`
while IFS=: read -r file line func line_cnt; do
	if test x"$func" != x"$oldfunc"; then
		outname=`printf "%s.%05d" "$1.filtered" "$seq"`
	fi
	exec >>"WORK/$outname"
	echo "$file:$line:$func:$line_cnt"
done
