@echo off
setlocal ENABLEDELAYEDEXPANSION
cd /d %~dp0

set ProgFiles86Root=%ProgramFiles(x86)%
if not "%ProgFiles86Root%"=="" goto win64
set ProgFiles86Root=%ProgramFiles%
:win64

set zip=\responder-3.0.4.zip
set zipfile="!%cd!!zip!"
if not exist !zipfile! goto zipnotfound

set responder=C:\node\pyrusconnect-responder\
set config=config\default.json
set configfile=%responder%%config%

echo Copying config file...
copy %configfile% . /Y

echo Deleting log files...

set pattern=*.log*
set files=%responder%!pattern!
del %files%

set pattern=daemon\*.log
set files=%responder%!pattern!
del %files%

echo Deleting modules...
set pattern=node_modules
set files=%responder%!pattern!
rmdir %files% /S /Q

echo Unpacking new files from !zipfile! to C:\node...
Call :UnZipFile "C:\node\" !zipfile!

echo Restoring original config file...
copy default.json %configfile% /Y
del default.json

echo Copying node.exe.config file...
echo %ProgFiles86Root%
copy "C:\node\node.exe.config" "%ProgFiles86Root%\nodejs\node.exe.config" /Y

echo Done.
exit /b

:zipnotfound
echo Error: !zipfile! not found
exit /b

:UnZipFile <ExtractTo> <newzipfile>
set vbs="%temp%\_.vbs"
if exist %vbs% del /f /q %vbs%
>%vbs%  echo Set fso = CreateObject("Scripting.FileSystemObject")
>>%vbs% echo If NOT fso.FolderExists(%1) Then
>>%vbs% echo fso.CreateFolder(%1)
>>%vbs% echo End If
>>%vbs% echo set objShell = CreateObject("Shell.Application")
>>%vbs% echo set FilesInZip=objShell.NameSpace(%2).items
>>%vbs% echo objShell.NameSpace(%1).CopyHere FilesInZip, 16
>>%vbs% echo Set fso = Nothing
>>%vbs% echo Set objShell = Nothing
cscript //nologo %vbs%
if exist %vbs% del /f /q %vbs%