#!/bin/bash
# (C) 2015 Jan-Simon Möller dl9pf@gmx.de
# (C) 2017 Behan Webster behanw@converseincode.com
# GPLv2
# Boardfile NR and LOC png generator

BOARDFOLDERS="arch/arm/plat-* arch/arm/mach-*"
TMPDIR="$(pwd)/tmp"
mkdir -p "$TMPDIR"

DATAFILE=$(mktemp "$TMPDIR/tmp.data1.XXXXXX")
DATAFILE2=$(mktemp "$TMPDIR/tmp.data2.XXXXXX")
OUTPUTFILE="$TMPDIR/boardfiles.png"
OUTPUTFILE2="$TMPDIR/boardloc.png"

FILESBOARD="$TMPDIR/files.board"
LOCBOARD="$TMPDIR/loc.board"
TICSBOARD="$TMPDIR/tics.board"
GNUPLOTBOARD="$TMPDIR/gnuplot.board"

# quick and dirty sanity check
if test ! -d arch/arm -o ! -d .git; then
echo "This script must be executed in a toplevel git tree of the kernel!"
exit 1
fi

echo ""
echo ""
echo "ATTENTION!"
echo ""
echo "This script will call a series of"
echo "'git reset --hard' and DELETE"
echo "all uncommited changes in the working tree."
echo ""
echo "Only run in an unused kernel git tree."
echo ""
echo "YOU HAVE BEEN WARNED!"
echo ""
echo "To continue press enter, otherwise CTRL-C."
# shellcheck disable=SC2034
read aw


Y=0
TICS=""
function generatedata {
	local VER=$1
	git reset -q --hard "$VER"
	echo "I: Generating $VER"
	# of files files
	echo -n "$Y;" >> "$DATAFILE"
	# shellcheck disable=SC2086
	X=$(find $BOARDFOLDERS -type f | wc -l)
	echo "$X" >> "$DATAFILE"
	# Lines of code
	echo -n "$Y;" >> "$DATAFILE2"
	# shellcheck disable=SC2086
	( find $BOARDFOLDERS -type f -print0 | xargs -0 cat ) | wc -l >> $DATAFILE2
	#
	# TICS is used to record the kernel version tag for the plotting in the end
	if test x"$TICS" == x"" ; then
		TICS="'$VER' $Y"
	else
		TICS="$TICS , '$VER' $Y"
	fi

	export Y=$((Y+1));
}

# Shortcut - to save us from running stuff over and over again.
# But need to remove files if we really want a new run.
if test -f "$FILESBOARD" -a -f "$LOCBOARD" -a -f "$TICSBOARD"; then
	Y=$(tail -1 "$FILESBOARD" | cut -d";" -f1)
	TICS=$(cat "$TICSBOARD")
	rm -f "$DATAFILE" "$DATAFILE2"
	DATAFILE="$FILESBOARD"
	DATAFILE2="$LOCBOARD"
	echo "USING cached files $TMPDIR/*.board"
	echo "If you want fresh data clean up with"
	echo "rm -rf $TMPDIR/*.board"

else

	echo "Generating from scratch ..."
	for VER in $(git tag | egrep '^v[0-9.]*$' | tail -n +2 | sed -e 's/^v//' \
		| sort -t. -k 1,1n -k 2,2n -k 3,3n | sed -e 's/^/v/') ; do

		generatedata "$VER"
	done

	mv $DATAFILE "$FILESBOARD"
	mv $DATAFILE2 "$LOCBOARD"
	echo "$TICS" > "$TICSBOARD"
	# cleanup mktemps
fi

##############
# Postprocessing
# generate gnuplot script
cat <<ENDPLOT >"$GNUPLOTBOARD"
set term png size 1024,768
set output '$OUTPUTFILE'
set key off
set title 'Boardfiles in arch/arm/'
set xlabel 'Kernel Versions' offset 0,-2
set xlabel 'Kernel Versions'
set ylabel 'NR of Boardfiles'
set datafile separator ';' 
set xrange [0:$Y] 
set xtics ($TICS)
set xtics rotate by 90 offset -0,-3
plot '$FILESBOARD' with linespoints
set output '$OUTPUTFILE2'
set title 'Board LOC in arch/arm/'
set xlabel 'Kernel Versions' offset 0,-2
set ylabel 'KLOC of Boardfiles'
plot '$LOCBOARD' using 1:(\$2/1000.) with linespoints
ENDPLOT

gnuplot "$GNUPLOTBOARD"

echo ""
echo "DONE. File should be in $OUTPUTFILE and $OUTPUTFILE2"
