@echo off
@if not "%OS%"=="Windows_NT" goto :EXIT

rem ****************************************************************
rem  Script for UniFi Replication
rem  Created: 02/23/2000 by Dmitri Levin
rem  Last Modified: 12/07/2000 Dmitri
rem ****************************************************************
rem at 9:00 /next: cmd /c e:\replictn\apply-ai.bat

rem ****************************************************************
rem      Variable definitions
rem ****************************************************************

set main-dir=E:\replictn
set db-name=unifipc
set prod-db=E:\Database\UniFipc\%db-name%
set replic-dir=\\nt_los_reports\reports
set log-file=%main-dir%\replictn.log

rem to Stop Replication create empty file named stop-replic.txt
if exist %main-dir%\stop-replic.txt goto :EXIT

rem standard variables
set DLC=D:\DLC83C
set path=%DLC%\BIN;%path%
set save-dir=%main-dir%\save
set tmp-file=%main-dir%\test.out
set date-file=%main-dir%\today.out
set replic-db=%replic-dir%\%db-name%
set replic-ai=%main-dir%\%db-name%
set messge=

rem ****************************************************************
rem  Main Block
rem ****************************************************************

for /f "delims=," %%A in ( 'date /t' ) do set curr-day=%%A
set file-day=
if exist %date-file% ( for /f "delims=," %%A in ( %date-file% ) do set file-day=%%A)
if not "%curr-day%"=="%file-day%" call :PROCEDURE-FIRST-RUN

call :PROCEDURE-TIME
echo Replication Start --- %curr-time% >> %log-file%

rem  Check replication is available
if not exist %replic-db%.db echo Replication Database is not available >> %log-file% 
if not exist %replic-db%.db set messge=Reports database is not present!?
if not exist %replic-db%.db CALL :PROCEDURE-E-Mail
if not exist %replic-db%.db goto :NO-REPLICATION

if exist %replic-db%.lk echo Replication Database is in use >> %log-file%
if exist %replic-db%.lk goto :NO-REPLICATION

rem Sends e-mail in case any AI extents left over from last AI iteration
if exist %replic-ai%.ai1 set messge=at least one AI file was not applied to Replication database since last time
if exist %replic-ai%.ai1 CALL :PROCEDURE-E-Mail

call :PROCEDURE-REPLICATE

rem Switch to New AI extent
%COMSPEC% /C _rfutil.exe %prod-db% -C aimage new

call :PROCEDURE-REPLICATE

:NO-REPLICATION

rem  Delete temporary file
del %tmp-file%

rem ****************************************************************
rem  Main Block End
rem ****************************************************************
goto :EXIT


rem ****************************************************************
rem  FIRST-RUN PROCEDURE - is running only during first replication of the day
rem ****************************************************************
:PROCEDURE-FIRST-RUN

  set line=*******************************************

  rem Log File entry for the day
  echo %line%  >> %log-file%
  date /T >> %log-file%
  echo %line%  >> %log-file%

  date /t > %date-file%

rem ****************************************************************
rem  FIRST-RUN PROCEDURE END
rem ****************************************************************
goto :EXIT

rem ****************************************************************
rem  REPLICATE PROCEDURE
rem ****************************************************************
:PROCEDURE-REPLICATE

  :LOOP

    rem  Find first FULL AI extent
    %COMSPEC% /C _rfutil.exe %prod-db% -C aimage full > %tmp-file%

    for /f %%A in ( %tmp-file% ) do ( call :REPLIC %%A )
    goto :EXIT


      :REPLIC
      if not exist %1 goto :EXIT
      echo Extent %1 is full >> %log-file%

      rem Copy AI extent to replication destination
      call :PROCEDURE-AI-COUNT
      copy %1 %replic-ai%.ai%ai-num%
      call :PROCEDURE-TIME
      echo Extent %1 is copied to %replic-ai%.ai%ai-num% %curr-time% >> %log-file%

      rem  Empty AI extnet
      %COMSPEC% /C _rfutil.exe %prod-db% -C aimage empty

      goto :LOOP

rem ****************************************************************
rem  REPLICATE PROCEDURE END
rem ****************************************************************
goto :EXIT


rem ****************************************************************
rem  TIME PROCEDURE
rem ****************************************************************
:PROCEDURE-TIME

  for /f %%A in ( 'time /t' ) do set curr-time=%%A 

goto :EXIT

rem ****************************************************************
rem  AI COUNT PROCEDURE find the maximum after-image number + 1 and store it in ai-num
rem ****************************************************************
:PROCEDURE-AI-COUNT

  set ai-num=0
  for %%A in ( %replic-ai%.ai* ) do call :PROCESS1 %%A
  goto :ADD1

  :PROCESS1
    rem Get the number. I assume here that neither NT name nor directories have a periods in the name
    for /f "delims=. tokens=2" %%A in ("%1") do set tmp-num=%%A

    rem remove 'ai' from the number
    set tmp-num=%tmp-num:~2%

    rem find the maximum ai number and store it in ai-num
    set /a value1=%tmp-num%
    set /a value2=%ai-num%
    if %value1% GTR %value2% set ai-num=%tmp-num%
  goto :EXIT

  :ADD1
  rem Add 1 to last ai-num
  set /a value=%ai-num%
  set /a value+=1
  set ai-num=%value%

goto :EXIT

rem ****************************************************************
rem E-Mail PROCEDURE 
rem ****************************************************************
:PROCEDURE-E-Mail

  set email-file=%main-dir%\message.txt

  echo Check replication on NT_LOS_REPORTS > %email-file%
  echo %messge% >> %email-file%
  echo . >> %email-file%
  date /t >> %email-file%
  CALL :PROCEDURE-TIME
  echo at %curr-time% >> %email-file%
  echo . >> %email-file%
  echo replic.bat script >> %email-file%

  blat.exe %email-file% -t to-address@company.com -c copy-to-address@company.com -s "Replication not finished. Check NT_Target." -i NTscript@company.com -f from-address@company.com -server SMTP_MAIL

  del %email-file%

goto :EXIT


:EXIT
1