@REM $Id$
@ECHO OFF

cd /D "%~dp0"

@REM Bestanden met relatieve paden kunnen niet gerenamed worden
@REM rename %1 %1.done

copy %1 %1_%create_stamp%.done
del %1

@ECHO ON
@ECHO rename: %1
@ECHO     to: %1_%create_stamp%.done
@ECHO.
@ECHO Dit vult de testdatabase %targetUSER%
@ECHO Default wordt de productiedatabase van %sourceUSER% overgenomen
@ECHO Ik gebruik nu %dmpfile%
@ECHO.
@ECHO OFF

SET logfile=%1_%create_stamp%.log

@ECHO %DATE% %TIME% >>%logfile%
@ECHO Starting refresh %sourceUSER% from %dmpfile% into %targetUSER% >>%logfile%
@dir %dmpfile% >>%logfile%

sqlplus.exe %oracle_login%@%targetSERVER% @CreateUserTEST.sql %targetUSER% %targetPASS% >>%logfile% 2>>&1
IF errorlevel 1 GOTO nocreate

imp %oracle_login%@%targetSERVER% file=%dmpfile% toid_novalidate=%targetUSER%.MLD_T_UITVOERTIJD fromuser=%sourceUSER% touser=%targetUSER% log=%targetUSER%.log COMPILE=N 2>>%logfile%

ECHO Running %postrefreshsql% >>%logfile%
EXIT | sqlplus.exe %targetUSER%/%targetPASS%@%targetSERVER% @%postrefreshsql% >>%logfile% 2>>&1

REM Autoupgrade:
sqlplus %targetUSER%/%targetPASS%@%targetSERVER% @getfcltschema.sql
SET currschema=%errorlevel%
                      SET script=DB%currschema%to%mindatabaseschema%e.sql
IF NOT EXIST %script% SET script=DB%currschema%to%mindatabaseschema%d.sql
IF NOT EXIST %script% SET script=DB%currschema%to%mindatabaseschema%c.sql
IF NOT EXIST %script% SET script=DB%currschema%to%mindatabaseschema%b.sql
IF NOT EXIST %script% SET script=DB%currschema%to%mindatabaseschema%a.sql
IF NOT EXIST %script% SET script=DB%currschema%to%mindatabaseschema%.sql
IF NOT EXIST %script% GOTO noupgrade

ECHO About to perform upgrade %script%>>%logfile%
EXIT | sqlplus.exe %targetUSER%/%targetPASS%@%targetSERVER% @%script% >>%logfile% 2>>&1

:noupgrade

ECHO Running anon_%fac_refresh_anon%.sql >>%logfile%
EXIT | sqlplus.exe %targetUSER%/%targetPASS%@%targetSERVER% @anon_%fac_refresh_anon%.sql >>%logfile% 2>>&1

IF x%deletedmp%==x1 DEL %dmpfile%

GOTO end

:nocreate
@ECHO ===============================================================================
@ECHO Het is niet gelukt gebruiker %targetUSER%@%targetSERVER% goed aan te maken
ECHO Het is niet gelukt gebruiker %targetUSER%@%targetSERVER% goed aan te maken >>%logfile%
goto end

:end
@ECHO OFF
@REM Stuur een email naar de aanvrager voor verversen database.
@REM In refresh_bat.bat zijn de system-variabelen smptsendusing, smtpserver, smtpserverport, notifyemail en targetUSER gezet.
@REM Deze worden doorgegeven aan emailonrefreshdb.js.

@ECHO Sending e-mail to %NOTIFYEMAIL% >>%logfile%
cscript -e:javascript .\emailonrefreshdb.js >>%logfile% 2>>&1

@ECHO Done refresh %sourceUSER% from %dmpfile% into %targetUSER% >>%logfile%
@ECHO %DATE% %TIME% >>%logfile%
