#!@SKG_BASH@

#1. create baseline coverage data file
# lcov -c -i -d appdir -o app_base.info
#2. perform test
# appdir/test
#3. create test coverage data file
# lcov -c -d appdir -o app_test.info
#4. combine baseline and test coverage data
# lcov -a app_base.info -a app_test.info -o app_total.info

P="@CMAKE_BINARY_DIR@"
mkdir -p ${P}/cov > /dev/null 2>&1
cd ${P}/cov
for i in `find $P -name *.gcda -exec dirname {} \; |sort -u | grep -v skgtest`
do
	OPTIONS=${OPTIONS}" -d "${i}
done

if [[ $1 = "-b" ]] ; then
	echo "remove .gcda"
	find $P -name *.gcda -exec rm {} \;
	RC=$?
	if [[ $RC != 0 ]] ; then
		cd -
  		exit $RC
	fi
else
	echo "STEP 1: lcov $OPTIONS -c -o ${P}/cov/skrooge.info"
	lcov $OPTIONS -c -o ${P}/cov/skrooge.info > /dev/null
	RC=$?
	if [[ $RC != 0 ]] ; then
		cd -
  		exit $RC
	fi

	echo "STEP 2.1: lcov -r ${P}/cov/skrooge.info '*.moc' -o ${P}/cov/skrooge.info"
	lcov -r ${P}/cov/skrooge.info '*.moc' -o ${P}/cov/skrooge.info > /dev/null
	RC=$?
	if [[ $RC != 0 ]] ; then
		cd -
	  	exit $RC
	fi

	echo "STEP 2.2: lcov -r ${P}/cov/skrooge.info '/usr/*' -o ${P}/cov/skrooge.info"
	lcov -r ${P}/cov/skrooge.info '/usr/*' -o ${P}/cov/skrooge.info > /dev/null
	RC=$?
	if [[ $RC != 0 ]] ; then
		cd -
	  	exit $RC
	fi

	echo "STEP 2.3: lcov -r ${P}/cov/skrooge.info 'moc_*.cpp' -o ${P}/cov/skrooge.info"
	lcov -r ${P}/cov/skrooge.info 'moc_*.cpp' -o ${P}/cov/skrooge.info > /dev/null
	RC=$?
	if [[ $RC != 0 ]] ; then
		cd -
  		exit $RC
	fi

	echo "STEP 2.4: lcov -r ${P}/cov/skrooge.info '*.h' -o ${P}/cov/skrooge.info"
	lcov -r ${P}/cov/skrooge.info '*.h' -o ${P}/cov/skrooge.info > /dev/null
	RC=$?
	if [[ $RC != 0 ]] ; then
		cd -
  		exit $RC
	fi

	echo "STEP 2.5: lcov -r ${P}/cov/skrooge.info '*_settings.cpp' -o ${P}/cov/skrooge.info"
	lcov -r ${P}/cov/skrooge.info '*_settings.cpp' -o ${P}/cov/skrooge.info > /dev/null
	RC=$?
	if [[ $RC != 0 ]] ; then
		cd -
  		exit $RC
	fi
	
	echo "STEP 2.6: lcov -r ${P}/cov/skrooge.info '*designerplugin.cpp' -o ${P}/cov/skrooge.info"
	lcov -r ${P}/cov/skrooge.info '*designerplugin.cpp' -o ${P}/cov/skrooge.info > /dev/null
	RC=$?
	if [[ $RC != 0 ]] ; then
		cd -
  		exit $RC
	fi
	
	echo "STEP 3: genhtml --frames -t "skrooge coverage" --legend ${P}/cov/skrooge.info -o ${P}/cov/full"
	genhtml --frames -t "skrooge coverage" --legend ${P}/cov/skrooge.info  -o ${P}/cov/full > /dev/null
	RC=$?
	if [[ $RC != 0 ]] ; then
		cd -
	  	exit $RC
	fi

	echo "STEP 4: genhtml -t "skrooge coverage" --no-source --legend ${P}/cov/skrooge.info -o ${P}/cov/light"
	genhtml -t "skrooge coverage" --no-source --legend ${P}/cov/skrooge.info  -o ${P}/cov/light > /dev/null
	RC=$?
	if [[ $RC != 0 ]] ; then
		cd -
	  	exit $RC
	fi
fi

cd -
exit 0
