@echo off
::@echo starting %1
::
:: run a Migration project in batch

if [%1%]==[] exit
set wrkfldr=%~dp1
set prjPath=%~1
set prjName=%~n1
set logfldr=%wrkfldr%log
set rptfldr=%wrkfldr%report
set usrfldr=%wrkfldr%usr

::-----------------------------------------------------------------
:: specify custom application settings here: set overrides then append standard settings
::-----------------------------------------------------------------

if exist gmStudioBatch.cfg del gmStudioBatch.cfg 
echo ' session specific settings>gmStudioBatch.cfg
:: add Deterministic compile
if "%NoBld%"=="-NoBld" (
echo NETMakeCmd = "cmd.exe /C pushd '%%LogFolder%%' && echo 'skipping build' > '%%MakeLogPath%%' 2>&1 %%PostCmd%% popd">> gmStudioBatch.cfg
)
::echo NETMakeCmd = "cmd.exe /C pushd '%%LogFolder%%' && call '%%SetEnvCmd%%' %%DevEnv%% %%PreCmd%% '%%MsBuildExe%%' '%%NetProjPath%%' /t:Rebuild /p:Configuration=Debug /p:WarningLevel=1 /p:Platform=AnyCPU /p:Deterministic=True > '%%MakeLogPath%%' 2>&1 %%PostCmd%% popd">> gmStudioBatch.cfg
:: echo TranVbpToolExe     = "\gmTools\tools\pBasic.exe">> gmStudioBatch.cfg
:: echo TranAspToolExe     = "\gmTools\tools\pBasic.exe">> gmStudioBatch.cfg
::echo ReportToolExe = "internal">> gmStudioBatch.cfg
::echo ReportToolExe = "C:\Program Files (x86)\GreatMigrations\gmStudio\gmMetrics.exe" ' needs to be in folder with SCMOperations.dll>> gmStudioBatch.cfg

type "%gmStudioCfg%" >> gmStudioBatch.cfg

::-----------------------------------------------------------------
:: If AutoBatch=YES then  the tasks/tools specified in the
:: migrationSet file (.gmproj) will be performed.  Otherwise, the 
:: set of task/tools specified below will be performed.
:: must be =NO for reports to run.
::-----------------------------------------------------------------

set autoBatch=/AUTOBATCH='YES'

::-----------------------------------------------------------------
:: Select Job Tasks (only used when AUTOBATCH=NO)
::-----------------------------------------------------------------

set tasks=
::set tasks=%tasks%Build VB6,
::set tasks=%tasks%Snapshot a Baseline,
set tasks=%tasks%Reset Task Status,
set tasks=%tasks%Run Translation,
set tasks=%tasks%Deploy Translation,
set tasks=%tasks%Build Translation

::-----------------------------------------------------------------
:: Select Migration Tools (only used when AUTOBATCH=NO)
::-----------------------------------------------------------------

set tools=
set tools=%tools%Apply Task Filter,
set tools=%tools%Validate Source,
set tools=%tools%Update Project Metalang File,
::set tools=%tools%Author Interop Assemblies,
set tools=%tools%Author Interface Descriptions,
::set tools=%tools%Run Assessment Wizard,
set tools=%tools%Set Build Order,
::set tools=%tools%Create BuildSet,
set tools=%tools%Author Resx Files,

::-----------------------------------------------------------------
:: Select Reports
::-----------------------------------------------------------------

set rprts=
::-----------------------------------------------------------------
:: Code Scans
::-----------------------------------------------------------------
set rprts=%rprts%Source Structure,
set rprts=%rprts%Source References,
set rprts=%rprts%Source Members,
set rprts=%rprts%Source GUI Scan,
set rprts=%rprts%Source Code Scan,
::set rprts=%rprts%Iceberg,
::-----------------------------------------------------------------
:: Project Reports
::-----------------------------------------------------------------
set rprts=%rprts%Project Summary,
set rprts=%rprts%Metrics Summary,
::set rprts=%rprts%Migration Set,
::set rprts=%rprts%Interface File Headers,
::set rprts=%rprts%Interface File ProgIds,
::set rprts=%rprts%Code Bundle Metrics,
set rprts=%rprts%.NET Build Logs,
set rprts=%rprts%Translation Logs,
set rprts=%rprts%Translation Audit,
set rprts=%rprts%All Logs,
::-----------------------------------------------------------------
:: Semantic Model Reports
::-----------------------------------------------------------------
set rprts=%rprts%Semantic Definitions,
set rprts=%rprts%Semantic References,
set rprts=%rprts%Semantic Symbols,
set rprts=%rprts%Compiler Audit,
set rprts=%rprts%Unused Symbols,
set rprts=%rprts%Late Calls,
::set rprts=%rprts%Analyzer Audit,
::-----------------------------------------------------------------
:: Utility Reports
::-----------------------------------------------------------------
::set rprts=%rprts%Migration Project List,
::set rprts=%rprts%Target Code Scan,
set rprts=%rprts%Generate Visual Studio Solution,

::-----------------------------------------------------------------
:: Cleanup prior reports/logs
::-----------------------------------------------------------------

if exist "%logfldr%\*.Batch.tab"         del "%logfldr%\*.Batch.tab"
if exist "%logfldr%\*.Session.tab"       del "%logfldr%\*.Session.tab"
                                         
if exist "%rptfldr%\*-AllLog.txt"        del "%rptfldr%\%prjName%*-AllLog.txt"
if exist "%rptfldr%\*-Bldlog.tab"        del "%rptfldr%\%prjName%*-BldLog.tab"
if exist "%rptfldr%\*-BndScan.tab"       del "%rptfldr%\%prjName%*-BndScan.tab"
if exist "%rptfldr%\*-Bundle.tab"        del "%rptfldr%\%prjName%*-Bundle.tab"
if exist "%rptfldr%\*-LibHeaders.tab"    del "%rptfldr%\%prjName%*-LibHeaders.tab"
if exist "%rptfldr%\*-LibProgIds.tab"    del "%rptfldr%\%prjName%*-LibProgIds.tab"
if exist "%rptfldr%\*-Metrics.htm"       del "%rptfldr%\%prjName%*-Metrics.htm"
if exist "%rptfldr%\*-MigSet.tab"        del "%rptfldr%\%prjName%*-MigSet.tab"
if exist "%rptfldr%\*-MigStat.txt"       del "%rptfldr%\%prjName%*-MigStat.txt"
if exist "%rptfldr%\*-SrcGUI.tab"        del "%rptfldr%\%prjName%*-SrcGUI.tab"
if exist "%rptfldr%\*-SrcMember.tab"     del "%rptfldr%\%prjName%*-SrcMember.tab"
if exist "%rptfldr%\*-SrcOrder.txt"      del "%rptfldr%\%prjName%*-SrcOrder.txt"
if exist "%rptfldr%\*-SrcRef.tab"        del "%rptfldr%\%prjName%*-SrcRef.tab"
if exist "%rptfldr%\*-SrcScan.tab"       del "%rptfldr%\%prjName%*-SrcScan.tab"
if exist "%rptfldr%\*-SrcStruct.tab"     del "%rptfldr%\%prjName%*-SrcStruct.tab"
if exist "%rptfldr%\*-TranLog.tab"       del "%rptfldr%\%prjName%*-TranLog.tab"
if exist "%rptfldr%\*-TranAudit.tab"     del "%rptfldr%\%prjName%*-TranAudit.tab"
if exist "%rptfldr%\*-LateCalls.tab"     del "%rptfldr%\%prjName%*-LateCalls.tab"
if exist "%rptfldr%\*-UnusedSymbols.tab" del "%rptfldr%\%prjName%*-UnusedSymbols.tab"

::-----------------------------------------------------------------
:: Edit the files to test special cases
::-----------------------------------------------------------------

::Powershell.exe -executionpolicy remotesigned -File  editFile.ps1 "%prjPath%"

::-----------------------------------------------------------------
:: Run the operations using the batch interface
::-----------------------------------------------------------------
echo ---- START %date% %time% %prjName%

set logstart="/logstart='%cd%\%prjName%.Start.log'"
set savemru="/SAVEMRU='no'"
set mig="/MIG='%prjPath%'"
set tasks="/TASKS='%tasks%'"
set tools="/TOOLS='%tools%'"
set rprts=/REPORTS='%rprts%'" 
set cfg="/CFG='%wrk%\batch\gmStudioBatch.cfg'"
 
"%GmStudioExe%" %autoBatch% %mig% %tasks% %tools% %rprts% %cfg% %logstart% %savemru% /IGNOREMRU='YES'

:: report progress (errors only)

::findstr /c:"Translation Status" /c:"NET Build Status" "%rptfldr%\*-MigStat.txt" 2>&1

echo ------ END %date% %time% %prjName%

::-----------------------------------------------------------------
:: append project reports into aggregate reports
::-----------------------------------------------------------------

if exist "%logfldr%\*.Batch.tab"      findstr ".*" "%logfldr%\*.Batch.tab"      >> RegTest.tab  
if exist "%logfldr%\*.Session.tab"    findstr ".*" "%logfldr%\*.Session.tab"    >> Session.tab  

call :report1 Bldlog.tab
call :report1 AllLog.txt
call :report1 BndScan.tab
call :report1 Bundle.tab
call :report1 LibHeaders.tab
call :report1 LibProgIds.tab
call :report1 Metrics.htm
call :report1 MigSet.tab
call :report1 MigStat.txt
call :report1 SrcGUI.tab
call :report1 SrcMember.tab
call :report1 SrcOrder.txt
call :report1 SrcRef.tab
call :report1 SrcScan.tab
call :report1 SrcStruct.tab
call :report1 TranLog.tab
call :report1 TranAudit.tab
call :report1 AnaRef.tab
call :report1 AnaDef.tab
call :report1 LateCalls.tab
call :report1 UnusedSymbols.tab

exit /b

:report1 
set inprpt=%1
set outrpt=%1
if exist "%rptfldr%\*-%inprpt%" (
   findstr ".*" "%rptfldr%\*-%inprpt%"   >> %outrpt%
   find /v /c "" "%rptfldr%\*-%inprpt%"    >> %outrpt%.wc 2>&1
)
exit /b

::-----------------------------------------------------------------
:: Run again; just some reports
::-----------------------------------------------------------------

goto end

set tasks=
set tools=
set rprts=Code Bundle Metrics,
set rprts=%rprts%Target Code Scan,
set rprts="/REPORTS='%rprts%'"
copy CodeScanList.txt %usrfldr%\CodeScanList.txt
set autoBatch="/AUTOBATCH='NO'"

"%GmStudioExe%" %autoBatch% %mig% %tasks% %tools% %rprts% %cfg% %savemru% /IGNOREMRU='YES'

del %usrfldr%\CodeScanList.txt

if exist "%rptfldr%\*-BndScan.tab"    findstr ".*" "%rptfldr%\*-BndScan.tab"    >> BndScan.tab  
if exist "%rptfldr%\*-Bundle.tab"     findstr ".*" "%rptfldr%\*-Bundle.tab"     >> Bundle.tab  


:end