#!/bin/bash
. ./settings.sh

if [ -f *.tmp ];
then
	rm *.tmp
fi

rm ${UT_TMP}/*.tmp > /dev/null 2>&1

SPACES="     "

echo "*** Single file report of project $PLATFORM_NAME ***"
echo

# Display the timestamp
. ./02_timestamp.sh

echo
GIT_COMMIT_HASH_VALUE=`git rev-parse HEAD`
echo git hash: $GIT_COMMIT_HASH_VALUE


echo
echo
echo "*** Does the target application build? ***"
. ./30_eval_app_build.sh


echo
echo
echo "*** Compiler warnings ***"
$DB_TOOL $DB < w_report_integration.sql

#echo "`select \".width\", max(length(filename)), \"-5 8 -9\", max(length(error_text)) from v_compiler_results_al; | $DB_TOOL -separator \" \" $DB`"> $UT_TMP/w_report_integration.tmp
echo ".width  -5 8 -9" > $UT_TMP/w_report_integration.tmp
echo ".headers on">> $UT_TMP/w_report_integration.tmp
echo ".mode column">> $UT_TMP/w_report_integration.tmp
echo "select * from v_compiler_results_al;">> $UT_TMP/w_report_integration.tmp

#FOR /F "tokens=* USEBACKQ" %%F IN (`%DB_TOOL% %DB% "select count(*) from v_compiler_results_al;"`) DO SET COUNT_WARNINGS=%%F
COUNT_WARNINGS=`$DB_TOOL $DB "select count(*) from v_compiler_results_al;"`
echo Count of AL warnings: $COUNT_WARNINGS
echo

#echo List of compiler warnings:
$DB_TOOL $DB < $UT_TMP/w_report_integration.tmp

#echo select ".width", max(length(filename)), "-5 8 -9", max(length(error_text)) from v_compiler_results_n_al; | %DB_TOOL% -separator " " %DB% > %UT_TMP%\w_report_integration.tmp
echo ".width  -5 8 -9" > $UT_TMP/w_report_integration.tmp
echo ".headers on">> $UT_TMP/w_report_integration.tmp
echo ".mode column">> $UT_TMP/w_report_integration.tmp
echo "select * from v_compiler_results_n_al;">> $UT_TMP/w_report_integration.tmp

COUNT_WARNINGS=`$DB_TOOL $DB "select count(*) from v_compiler_results_n_al;"`
echo Count of third party warnings: $COUNT_WARNINGS
echo
echo
#echo List of compiler warnings:
$DB_TOOL $DB < $UT_TMP/w_report_integration.tmp

echo "*** Memory size report ***"
#. ./42_analyze_size.bat

#if [ -f $BUILD_BIN_PATH/$MAIN_APP_NAME/${MAIN_APP_NAME}_Size_report.txt ];
#then
#  cat $BUILD_BIN_PATH/$MAIN_APP_NAME/${MAIN_APP_NAME}_Size_report.txt
#else
#  cat $BUILD_BIN_PATH/$MAIN_APP_NAME/${MAIN_APP_NAME}_Size.txt
#fi

echo
echo "*** Group memory footprint ***"
$DB_TOOL $DB < v_section_module.sql
#echo select ".width", "11 -8 -8" from v_section_module_stat; | $DB_TOOL -separator " " $DB > $UT_TMP/v_section_module_stat.tmp
echo ".headers on"> $UT_TMP/v_section_module_stat.tmp
echo ".mode column">> $UT_TMP/v_section_module_stat.tmp

echo "select * from v_section_module_stat;">> $UT_TMP/v_section_module_stat.tmp

$DB_TOOL $DB < $UT_TMP/v_section_module_stat.tmp

echo
echo "*** Module memory footprint ***"
echo "Status based on percent: 0-85 OK, 86-100 WARN, 100+ OVER"
#echo select ".width", max(length(module)), "-8 -8 -5 10 -8 -8 -5 10" from v_section_module; | $DB_TOOL -separator " " $DB > $UT_TMP/v_section_module.tmp
echo ".width -8 -8 -5 10 -8 -8 -5 10" > $UT_TMP/v_section_module.tmp
echo ".headers on">> $UT_TMP/v_section_module.tmp
echo ".mode column">> $UT_TMP/v_section_module.tmp

#echo select size, module, memory from v_section_module;>> $UT_TMP/v_section_module.tmp
echo "select * from v_section_module_report;">> $UT_TMP/v_section_module.tmp

$DB_TOOL $DB < $UT_TMP/v_section_module.tmp

echo
echo
echo "*** Files with QAC or HIS warnings ***"
$DB_TOOL $DB < q_report_integration.sql
#echo select ".width", max(length(filename)), "-7 -7 -7 -7 -7 -7 -7 -7 -7 -7 -7" from v_q_results; | $DB_TOOL -separator " " $DB > $UT_TMP/q_report_integration.tmp
echo ".width -7 -7 -7 -7 -7 -7 -7 -7 -7 -7 -7" > $UT_TMP/q_report_integration.tmp
echo ".headers on">> $UT_TMP/q_report_integration.tmp
echo ".mode column">> $UT_TMP/q_report_integration.tmp
# rem echo select filename, iif(misra_0="0", ".", misra_0) as misra_0, iif(misra_1="0", ".", misra_1) as misra_1, iif(misra_2="0", ".", misra_2) as misra_2, iif(misra_3="0", ".", misra_3) as misra_3, iif(misra_4="0", ".", misra_4) as misra_4, iif(misra_5="0", ".", misra_5) as misra_5, iif(misra_6="0", ".", misra_6) as misra_6, iif(misra_7="0", ".", misra_7) as misra_7, iif(misra_8="0", ".", misra_8) as misra_8, iif(misra_9="0", ".", misra_9) as misra_9, iif(his="0", ".", his) as his from v_q_results;>> %UT_TMP%\q_report_integration.tmp
#echo "select filename, iif(misra_0="0", "-", misra_0) as misra_0, iif(misra_1="0", "-", misra_1) as misra_1, iif(misra_2="0", "-", misra_2) as misra_2, iif(misra_3="0", "-", misra_3) as misra_3, iif(misra_4="0", "-", misra_4) as misra_4, iif(misra_5="0", "-", misra_5) as misra_5, iif(misra_6="0", "-", misra_6) as misra_6, iif(misra_7="0", "-", misra_7) as misra_7, iif(misra_8="0", "-", misra_8) as misra_8, iif(misra_9="0", "-", misra_9) as misra_9, iif(his="0", "-", his) as his from v_q_results;">> $UT_TMP/q_report_integration.tmp
echo "select filename, iif(misra_0=\"0\", \"-\", misra_0) as misra_0, iif(misra_1=\"0\", \"-\", misra_1) as misra_1, iif(misra_2=\"0\", \"-\", misra_2) as misra_2, iif(misra_3=\"0\", \"-\", misra_3) as misra_3, iif(misra_4=\"0\", \"-\", misra_4) as misra_4, iif(misra_5=\"0\", \"-\", misra_5) as misra_5, iif(misra_6=\"0\", \"-\", misra_6) as misra_6, iif(misra_7=\"0\", \"-\", misra_7) as misra_7, iif(misra_8=\"0\", \"-\", misra_8) as misra_8, iif(misra_9=\"0\", \"-\", misra_9) as misra_9, iif(his=\"0\", \"-\", his) as his from v_q_results;">> $UT_TMP/q_report_integration.tmp
# rem echo select filename, misra_0, misra_1, misra_2, misra_3, misra_4, misra_5, misra_6, misra_7, misra_8, misra_9, his from v_q_results;>> %UT_TMP%\q_report_integration.tmp

#echo select "" as "", sum(misra_0) as "", sum(misra_1) as "", sum(misra_2) as "", sum(misra_3) as "", sum(misra_4) as "", sum(misra_5) as "", sum(misra_6) as "", sum(misra_7) as "", sum(misra_8) as "", sum(misra_9) as "", sum(his) as "" from v_q_results;>> %UT_TMP%\q_report_integration.tmp

COUNT_FILES_QAC_WARNINGS=`$DB_TOOL $DB "select printf('Count of files with warnings: %5d', count(*) ) from v_q_results;"`
TOTAL_0_4_WARNINGS=`$DB_TOOL $DB "select printf('Total of 0 - 4 warnings:      %5d', sum(misra_0) + sum(misra_1) + sum(misra_2) + sum(misra_3) + sum(misra_4) ) as total_0_4 from v_q_results;"`
TOTAL_5_9_WARNINGS=`$DB_TOOL $DB "select printf('Total of 5 - 9 warnings:      %5d', sum(misra_5) + sum(misra_6) + sum(misra_7) + sum(misra_8) + sum(misra_9) ) as total_5_9 from v_q_results;"`
TOTAL_HIS_WARNINGS=`$DB_TOOL $DB "select printf('Total of HIS warnings:        %5d', sum(his) ) as total_his from v_q_results;"`
GRAND_TOTAL_WARNINGS=`$DB_TOOL $DB "select printf('Warnings grand total:         %5d', sum(misra_0) + sum(misra_1) + sum(misra_2) + sum(misra_3) + sum(misra_4) + sum(misra_5) + sum(misra_6) + sum(misra_7) + sum(misra_8) + sum(misra_9) + sum(his) ) as grand_total from v_q_results;"`
echo $COUNT_FILES_QAC_WARNINGS
echo $TOTAL_0_4_WARNINGS
echo $TOTAL_5_9_WARNINGS
echo $TOTAL_HIS_WARNINGS
echo $GRAND_TOTAL_WARNINGS

$DB_TOOL $DB < $UT_TMP/q_report_integration.tmp

echo
echo
echo "*** Unit Test compiler statistics ***"
$DB_TOOL $DB < ut_report_compilerstatistics.sql
#echo select ".width", "5", max(length(name)), "9" from v_ut_results; | %DB_TOOL% -separator " " %DB% > %UT_TMP%\ut_report_integration.tmp
echo ".width 16 6"> $UT_TMP/ut_report_compilerstatistics.tmp
echo ".headers on">> $UT_TMP/ut_report_compilerstatistics.tmp
echo ".mode column">> $UT_TMP/ut_report_compilerstatistics.tmp
echo "select project, result from v_ut_results_compiler_statistics;">> $UT_TMP/ut_report_compilerstatistics.tmp

$DB_TOOL $DB < $UT_TMP/ut_report_compilerstatistics.tmp

echo
echo
echo "*** Unit Test results ***"
$DB_TOOL $DB < ut_report_integration.sql
#echo select ".width", "5", max(length(name)), "9" from v_ut_results; | %DB_TOOL% -separator " " %DB% > %UT_TMP%\ut_report_integration.tmp
echo ".width 5 24 9"> $UT_TMP/ut_report_integration.tmp
echo ".headers on">> $UT_TMP/ut_report_integration.tmp
echo ".mode column">> $UT_TMP/ut_report_integration.tmp
echo "select * from v_ut_results;">> $UT_TMP/ut_report_integration.tmp

COUNT_UT_PASSED=`$DB_TOOL $DB "select count(*) from v_ut_results where lower(result)='passed';"`
COUNT_UT_FAILED=`$DB_TOOL $DB "select count(*) from v_ut_results where lower(result)='*failed*';"`
echo "Passed: $COUNT_UT_PASSED   Failed: $COUNT_UT_FAILED"

$DB_TOOL $DB < $UT_TMP/ut_report_integration.tmp


echo
echo
echo "*** Integration Test results ***"
$DB_TOOL $DB < it_report_integration.sql
#echo select ".width", "12", max(length(name)), "9" from v_it_results; | %DB_TOOL% -separator " " %DB% > %UT_TMP%\it_report_integration.tmp
echo ".width 12 9"> $UT_TMP/it_report_integration.tmp
echo ".headers on">> $UT_TMP/it_report_integration.tmp
echo ".mode column">> $UT_TMP/it_report_integration.tmp
echo "select * from v_it_results;">> $UT_TMP/it_report_integration.tmp

COUNT_IT_PASSED=`$DB_TOOL $DB "select count(*) from v_it_results where lower(result)='passed';"`
COUNT_IT_FAILED=`$DB_TOOL $DB "select count(*) from v_it_results where lower(result)='*failed*';"`
echo "Passed: $COUNT_IT_PASSED   Failed: $COUNT_IT_FAILED"

$DB_TOOL $DB < $UT_TMP/it_report_integration.tmp

echo
echo
echo "*** Module coverage statistics ***"
$DB_TOOL $DB < co_report_integration.sql

#echo select ".width", max(length(Project)), "-11", "-11", "-11", "-11", "-11", "-11" from v_ut_coverage_module; | %DB_TOOL% -separator " " %DB% > %UT_TMP%\ut_coverage_module.tmp
echo ".width -11 -11 -11 -11 -11 -11"> $UT_TMP/ut_coverage_module.tmp
echo ".headers on">> $UT_TMP/ut_coverage_module.tmp
echo ".mode column">> $UT_TMP/ut_coverage_module.tmp
echo "select * from v_ut_coverage_module;">> $UT_TMP/ut_coverage_module.tmp
$DB_TOOL $DB < $UT_TMP/ut_coverage_module.tmp


echo
echo
echo "*** Function coverage statistics ***"
# rem $DB_TOOL $DB < co_report_integration.sql
#echo select ".width", "-10", "-10", max(length(function_name)), max(length(project)) from v_ut_coverage; | %DB_TOOL% -separator " " %DB% > %UT_TMP%\co_report_integration.tmp
echo ".width -10 -10"> $UT_TMP/co_report_integration.tmp
echo ".headers on">> $UT_TMP/co_report_integration.tmp
echo ".mode column">> $UT_TMP/co_report_integration.tmp
echo "select * from v_ut_coverage;">> $UT_TMP/co_report_integration.tmp

COUNT_TOTAL=`$DB_TOOL $DB "select count(*) as c from v_ut_coverage_latest;"`
OUTPUT=$COUNT_TOTAL

echo "Count of functions total              :     $OUTPUT"
echo "                                               C0        C1"

COUNT_FULLCOV_C0=`$DB_TOOL $DB "select count(*) as c from v_ut_coverage_latest where C0_percent=100;"`
COUNT_FULLCOV_C1=`$DB_TOOL $DB "select count(*) as c from v_ut_coverage_latest where C1_percent=100;"`
OUTPUT0="${SPACES}${COUNT_FULLCOV_C0}"
OUTPUT1="${SPACES}${COUNT_FULLCOV_C1}"
echo "Count of functions with  100% coverage:     $OUTPUT0     $OUTPUT1"

COUNT_1_99_COV_C0=`$DB_TOOL $DB "select count(*) as c from v_ut_coverage_latest where 0<C0_percent and C0_percent<100;"`
COUNT_1_99_COV_C1=`$DB_TOOL $DB "select count(*) as c from v_ut_coverage_latest where 0<C1_percent and C1_percent<100;"`
OUTPUT0="${SPACES}${COUNT_1_99_COV_C0}"
OUTPUT1="${SPACES}${COUNT_1_99_COV_C1}"
echo "Count of functions with 99-1% coverage:     $OUTPUT0     $OUTPUT1"

COUNT_ZEROCOV_C0=`$DB_TOOL $DB "select count(*) as c from v_ut_coverage_latest where C0_percent=0;"`
COUNT_ZEROCOV_C1=`$DB_TOOL $DB "select count(*) as c from v_ut_coverage_latest where C1_percent=0;"`
OUTPUT0="${SPACES}${COUNT_ZEROCOV_C0}"
OUTPUT1="${SPACES}${COUNT_ZEROCOV_C1}"
echo "Count of functions with    0% coverage:     $OUTPUT0     $OUTPUT1"

$DB_TOOL $DB < $UT_TMP/co_report_integration.tmp


echo
echo
echo "*** Sanity check ***"

echo
echo These files should be reviewed and added to the file_to_module mapping:
echo -----------------------------------------------------------------------
$DB_TOOL $DB "select distinct filename from compiler_map where compiler_map.filename not in (select filename from file_to_module) and compiler_map.filename not in (select filename from file_to_module_blacklist) order by lower(filename);"

echo
echo
echo These files are in the file_to_module table, but are no longer in the map file:
echo -------------------------------------------------------------------------------
$DB_TOOL $DB "select distinct filename from file_to_module where filename not in (select filename from compiler_map) order by 1;"

echo
echo
echo These modules are in the file_to_module, but NOT in the module_size_plan:
echo -------------------------------------------------------------------------
$DB_TOOL $DB "select distinct printf('''%%s''', module) as module from file_to_module where module not in (select module from module_size_plan) order by 1 collate nocase;"

echo
echo
echo These modules are in the module_size_plan, but NOT in the file_to_module:
echo -------------------------------------------------------------------------
$DB_TOOL $DB "select distinct printf('''%%s''', module) as module from module_size_plan where module not in (select module from file_to_module) order by 1 collate nocase;"


echo
echo
echo "*** End of Single file report ***"

