@echo off
REM set arg1=%1
REM echo %arg1%
set MSBuildPath="C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\MSBuild.exe"
set WorkSpace="C:\JenkinsHomeDir\workspace\Staging-RCM_API\RetailCamModules.sln"
set MSBuildAction=/t:RetailCamModules
set Build=%MSBuildPath% %WorkSpace% %MSBuildAction%
set ProjectName=RCMAPI

for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set folderName=%ldt:~0,4%%ldt:~4,2%%ldt:~6,2%_%ldt:~8,2%%ldt:~10,2%%ldt:~12,2%
set BuildOutputDirectory=C:\Build\%ProjectName%\BuildOutput\
set BuildOutputFile=%BuildOutputDirectory%BuildOutput_%ProjectName%_%folderName%.txt

if not exist %BuildOutputDirectory% (
    echo create dir %BuildOutputDirectory%
    mkdir %BuildOutputDirectory%
) 

echo %Build%
%Build% >> %BuildOutputFile%

::Find text file consist of Build Succeeded.
>nul findstr /c:"Build succeeded." %BuildOutputFile% && (
    echo "Build Succeeded from Batch Script."
    goto :runDBMigrations
) || (
    echo "Build Failed from Batch Script."
)

goto :eof

:runDBMigrations
    set ProjectRoot=C:\JenkinsHomeDir\workspace\Staging-RCM_API\
    set EF6Directory=packages\EntityFramework.6.4.0\tools\net45\any\
    set BuildDirectory=RetailCamModules\bin\
    set EF6exeName=ef6.exe
    set DLLName=RetailCamModules.dll
    REM set DBCon="Data Source=dbcontent6848.footfallcam.com,41433;Initial Catalog=RetailCamControlPanel;User ID=sa;Password=cFg7unduUE;Max Pool Size=3000"
    set DBCon="Data Source=192.168.2.72,1433;Initial Catalog=RetailCamModule;User ID=ffc_user;Password=qwer!234;Max Pool Size=3000"

    for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
    set folderName=%ldt:~0,4%%ldt:~4,2%%ldt:~6,2%_%ldt:~8,2%%ldt:~10,2%%ldt:~12,2%
    echo %folderName%
    REM set DBMigrationDirectory=C:\Build\%ProjectName%\DBMigrations\
    set DBMigrationDirectory=C:\RetailCamUpdater-v2\DBMigrations\Staging\RCM\
    set DBMigrationFile=%DBMigrationDirectory%Migration_RCM_%folderName%.txt
    echo %DBMigrationFile%
    
    if not exist %DBMigrationDirectory% (
        echo create dir %DBMigrationDirectory%
        mkdir %DBMigrationDirectory%
    ) 
    
    cd %ProjectRoot%%BuildDirectory%
    
    if not exist "%ProjectRoot%%BuildDirectory%%EF6exeName%" (
        echo not exist file
        copy %ProjectRoot%%EF6Directory%%EF6exeName% %ProjectRoot%%BuildDirectory%%EF6exeName%
    )
    
    if exist %ProjectRoot%%BuildDirectory%%EF6exeName% (
        echo Run Database Migration
        REM echo %EF6exeName% database update --assembly %DLLName% --connection-string=%DBCon% --connection-provider="System.Data.SqlClient" --script
        REM %EF6exeName% database update --assembly %DLLName% --connection-string=%DBCon% --connection-provider="System.Data.SqlClient" --script >> %DBMigrationFile%
        %EF6exeName% database update --assembly %DLLName% --connection-string=%DBCon% --connection-provider="System.Data.SqlClient" --verbose >> %DBMigrationFile%
    )
goto :eof

:eof


