@echo off
cls

set log="%~dp0\AxisIPComponents_x32_addtrace.log"
del %log% >nul 2>nul
set err="%~dp0\err.log"
set er2="%~dp0\er2.log"

set titl=Register AXIS components(Media Parser,Joystick) (x32) on 32\64-bit OS.
echo %titl%
ver
echo %date% %time:~0,-3%
echo.

@echo %titl% >%err%
if exist %err% goto start_action
echo.
echo *******************************************************
echo ERROR! You do not have rights to unregister components.
echo *******************************************************
echo.
goto :end

:start_action
del %err% >nul 2>nul

if %PROCESSOR_ARCHITECTURE%==AMD64 goto LABEL_x64
if %PROCESSOR_ARCHITECTURE%==IA64 goto LABEL_x64
if defined PROCESSOR_ARCHITEW6432 goto LABEL_x64

set dstdir=%CommonProgramFiles%\AxisIPComponents
set regsvr="%systemroot%\System32\regsvr32.exe"
goto LABEL_REG

:LABEL_x64
set dstdir=%CommonProgramFiles(x86)%\AxisIPComponents
set regsvr="%systemroot%\SysWoW64\regsvr32.exe"

:LABEL_REG

set src_amp="%~dp0\AxisMediaParser.dll"
set src_flt="%~dp0\AxisRTPSrcFilter.ax"
set src_jst="%~dp0\AxisJoystickModule.dll"
set src_add="%~dp0\AxisIPComponents_x32_add.bat"
set src_del="%~dp0\AxisIPComponents_x32_del.bat"

set dst_amp="%dstdir%\AxisMediaParser.dll"
set dst_flt="%dstdir%\AxisRTPSrcFilter.ax"
set dst_jst="%dstdir%\AxisJoystickModule.dll"

if exist "%dstdir%" goto scopy

md "%dstdir%" >%err% 2>%er2%
set ertext=[md "%dstdir%"] error = %ErrorLevel%
if ErrorLevel 1 goto exit_err

:scopy
echo Copying files to %dstdir%

copy %src_amp% "%dstdir%" >%err% 2>%er2%
set ertext=[copy %src_amp% "%dstdir%"] error = %ErrorLevel%
if ErrorLevel 1 goto exit_err

copy %src_flt% "%dstdir%" >%err% 2>%er2%
set ertext=[copy %src_flt% "%dstdir%"] error = %ErrorLevel%
if ErrorLevel 1 goto exit_err

copy %src_jst% "%dstdir%" >%err% 2>%er2%
set ertext=[copy %src_jst% "%dstdir%"] error = %ErrorLevel%
if ErrorLevel 1 goto exit_err

copy %src_add% "%dstdir%" >%err% 2>%er2%
set ertext=[copy %src_add% "%dstdir%"] error = %ErrorLevel%
if ErrorLevel 1 goto exit_err

copy %src_del% "%dstdir%" >%err% 2>%er2%
set ertext=[copy src_del% "%dstdir%"] error = %ErrorLevel%
if ErrorLevel 1 goto exit_err

echo.
echo Registering Axis Components

%regsvr% %dst_amp% /s >%err% 2>%er2%
set ertext=[%regsvr% %dst_amp%] error = %ErrorLevel%
if ErrorLevel 1 goto exit_err

%regsvr% %dst_flt% /s >%err% 2>%er2%
set ertext=[%regsvr% %dst_flt%] error = %ErrorLevel%
if ErrorLevel 1 goto exit_err

%regsvr% %dst_jst% /s >%err% 2>%er2%
set ertext=[%regsvr% %dst_jst%] error = %ErrorLevel%
if ErrorLevel 1 goto exit_err

@echo.
echo debug info:
REG QUERY HKCR\TypeLib\{951BE58F-1EF5-4ECF-9556-4A350EADE8A8}\3.28\0\win32

@echo.
echo OK! Axis components were registered successfully.
goto :end

:exit_err
@echo %titl% >%log%
ver >>%log%
@echo %date% %time:~0,-3% >>%log%
@echo %ertext% >>%log%
if exist %err% copy %log%+%err% %log% >nul
if exist %er2% copy %log%+%er2% %log% >nul
echo.
echo ************************************************
echo An error occurred while registering components.
echo See the log: %log%
echo ************************************************

:end
echo.
del %err% >nul 2>nul
del %er2% >nul 2>nul
pause
