@echo off
SETLOCAL
call settings.bat

SET "SPACES=     "

if exist *.tmp del *.tmp
if exist %UT_TMP%\*.tmp del %UT_TMP%\*.tmp

echo *** Single file report of project %PLATFORM_NAME% ***
echo.

rem Display the timestamp
call 02_timestamp.bat

echo.
FOR /F %%I IN ('%GIT% rev-parse HEAD') DO SET GIT_COMMIT_HASH_VALUE=%%I
echo git hash: %GIT_COMMIT_HASH_VALUE%


echo.
echo.
echo *** Does the target application build? ***
call 30_eval_app_build.bat


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 .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
echo Count of AL warnings: %COUNT_WARNINGS%

rem echo List of compiler warnings:
%DB_TOOL% %DB% < %UT_TMP%\w_report_integration.tmp

rem ---------------------------------------------------------------------
echo.

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 .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

FOR /F "tokens=* USEBACKQ" %%F IN (`%DB_TOOL% %DB% "select count(*) from v_compiler_results_n_al;"`) DO SET COUNT_WARNINGS=%%F
echo Count of third party warnings: %COUNT_WARNINGS%

rem echo List of compiler warnings:
%DB_TOOL% %DB% < %UT_TMP%\w_report_integration.tmp


echo.
echo.
echo *** Memory size report ***
call 42_analyze_size.bat
if exist %BUILD_BIN_PATH%\%MAIN_APP_NAME%\%MAIN_APP_NAME%_Size_report.txt (
  type %BUILD_BIN_PATH%\%MAIN_APP_NAME%\%MAIN_APP_NAME%_Size_report.txt
) else (
  type %BUILD_BIN_PATH%\%MAIN_APP_NAME%\%MAIN_APP_NAME%_Size.txt
)


echo.
echo *** Group memory footprint ***
%DB_TOOL% %DB% < v_section_module.sql
echo select ".width", max(length("Group"))+2, "-8 -8 -8 -8 -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
echo .headers off>> %UT_TMP%\v_section_module_stat.tmp
echo select * from v_section_module_sum;>> %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 -8 -8 -5 10 -8 -8 -8 -8 -5 10" from v_section_module; | %DB_TOOL% -separator " " %DB% > %UT_TMP%\v_section_module.tmp
echo .headers on>> %UT_TMP%\v_section_module.tmp
echo .mode column>> %UT_TMP%\v_section_module.tmp

rem 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 *** Module section allocation problems ***
echo If the module is considered TRUSTED, and objects are UNTRUSTED, or
echo if the module is considered UNTRUSTED, and objects are TRUSTED
echo select ".width -10 -10 -8 -8", max(length(section)), max(length(filename)), "6", max(length(module)) from v_section_module_sanity; | %DB_TOOL% -separator " " %DB% > %UT_TMP%\v_section_module_sanity.tmp
echo .headers on>> %UT_TMP%\v_section_module_sanity.tmp
echo .mode column>> %UT_TMP%\v_section_module_sanity.tmp

echo select * from v_section_module_sanity;>> %UT_TMP%\v_section_module_sanity.tmp

%DB_TOOL% %DB% < %UT_TMP%\v_section_module_sanity.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 .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
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

FOR /F "tokens=* USEBACKQ" %%F IN (`%DB_TOOL% %DB% "select printf('Count of files with warnings: %%5d', count(*) ) from v_q_results;"`) DO SET COUNT_FILES_QAC_WARNINGS=%%F
FOR /F "tokens=* USEBACKQ" %%F IN (`%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;"`) DO SET TOTAL_0_4_WARNINGS=%%F
FOR /F "tokens=* USEBACKQ" %%F IN (`%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;"`) DO SET TOTAL_5_9_WARNINGS=%%F
FOR /F "tokens=* USEBACKQ" %%F IN (`%DB_TOOL% %DB% "select printf('Total of HIS warnings:        %%5d', sum(his) ) as total_his from v_q_results;"`) DO SET TOTAL_HIS_WARNINGS=%%F
FOR /F "tokens=* USEBACKQ" %%F IN (`%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;"`) DO SET GRAND_TOTAL_WARNINGS=%%F
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", max(length(project)), "6" from v_ut_results_compiler_statistics; | %DB_TOOL% -separator " " %DB% > %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 .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

FOR /F "tokens=* USEBACKQ" %%F IN (`%DB_TOOL% %DB% "select count(*) from v_ut_results where lower(result)='passed';"`) DO SET COUNT_UT_PASSED=%%F
FOR /F "tokens=* USEBACKQ" %%F IN (`%DB_TOOL% %DB% "select count(*) from v_ut_results where lower(result)='*failed*';"`) DO SET COUNT_UT_FAILED=%%F
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 .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

FOR /F "tokens=* USEBACKQ" %%F IN (`%DB_TOOL% %DB% "select count(*) from v_it_results where lower(result)='passed';"`) DO SET COUNT_IT_PASSED=%%F
FOR /F "tokens=* USEBACKQ" %%F IN (`%DB_TOOL% %DB% "select count(*) from v_it_results where lower(result)='*failed*';"`) DO SET COUNT_IT_FAILED=%%F
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 .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 .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

FOR /F "tokens=* USEBACKQ" %%F IN (`%DB_TOOL% %DB% "select count(*) as c from v_ut_coverage_latest;"`) DO SET COUNT_TOTAL=%%F
SET "OUTPUT=%SPACES%%COUNT_TOTAL%"
echo Count of functions total              : %OUTPUT:~-5%

echo                                            C0    C1

FOR /F "tokens=* USEBACKQ" %%F IN (`%DB_TOOL% %DB% "select count(*) as c from v_ut_coverage_latest where C0_percent=100;"`) DO SET COUNT_FULLCOV_C0=%%F
FOR /F "tokens=* USEBACKQ" %%F IN (`%DB_TOOL% %DB% "select count(*) as c from v_ut_coverage_latest where C1_percent=100;"`) DO SET COUNT_FULLCOV_C1=%%F
SET "OUTPUT0=%SPACES%%COUNT_FULLCOV_C0%"
SET "OUTPUT1=%SPACES%%COUNT_FULLCOV_C1%"
echo Count of functions with  100%% coverage: %OUTPUT0:~-5% %OUTPUT1:~-5%

FOR /F "tokens=* USEBACKQ" %%F IN (`%DB_TOOL% %DB% "select count(*) as c from v_ut_coverage_latest where 0<C0_percent and C0_percent<100;"`) DO SET COUNT_1_99_COV_C0=%%F
FOR /F "tokens=* USEBACKQ" %%F IN (`%DB_TOOL% %DB% "select count(*) as c from v_ut_coverage_latest where 0<C1_percent and C1_percent<100;"`) DO SET COUNT_1_99_COV_C1=%%F
SET "OUTPUT0=%SPACES%%COUNT_1_99_COV_C0%"
SET "OUTPUT1=%SPACES%%COUNT_1_99_COV_C1%"
echo Count of functions with 99-1%% coverage: %OUTPUT0:~-5% %OUTPUT1:~-5%

FOR /F "tokens=* USEBACKQ" %%F IN (`%DB_TOOL% %DB% "select count(*) as c from v_ut_coverage_latest where C0_percent=0;"`) DO SET COUNT_ZEROCOV_C0=%%F
FOR /F "tokens=* USEBACKQ" %%F IN (`%DB_TOOL% %DB% "select count(*) as c from v_ut_coverage_latest where C1_percent=0;"`) DO SET COUNT_ZEROCOV_C1=%%F
SET "OUTPUT0=%SPACES%%COUNT_ZEROCOV_C0%"
SET "OUTPUT1=%SPACES%%COUNT_ZEROCOV_C1%"
echo Count of functions with    0%% coverage: %OUTPUT0:~-5% %OUTPUT1:~-5%

%DB_TOOL% %DB% < %UT_TMP%\co_report_integration.tmp


echo.
echo.
echo *** Sanity check ***

echo.
echo These FILES are found in the generated map file, but NOT found in the file_to_module.csv configuration.
echo ACTION: Add these FILES to the file_to_module.csv configuration.
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 found in the file_to_module.csv configuration, but NOT found in the generated map file.
echo ACTION: Delete these FILES from the file_to_module.csv configuration.
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 found in the file_to_module.csv configuration, but NOT found in the module_size_plan.csv configuration.
echo ACTION: Add these MODULES to the module_size_plan.csv configuration.
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 found in the module_size_plan.csv configuration, but NOT found in the file_to_module.csv configuration.
echo ACTION: Add these MODULES to the file_to_module.csv configuration.
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 These SECTIONS are found in the generated map file, but NOT found in the section_to_memory.csv configuration.
echo ACTION: Add these SECTIONS to the section_to_memory.csv configuration.
echo -------------------------------------------------------------------------------
%DB_TOOL% %DB% "select distinct section from compiler_map where section not in (select distinct section from section_to_memory) order by 1 collate nocase;"

echo.
echo.
echo These SECTIONS are found in the section_to_memory.csv, but NOT found in the generated map file.
echo ACTION: Delete these SECTIONS from the section_to_memory.csv configuration.
echo -------------------------------------------------------------------------------
%DB_TOOL% %DB% "select distinct section from section_to_memory where section not in (select distinct section from compiler_map) order by 1 collate nocase;"


echo.
echo.
echo *** End of Single file report ***
