echo off
echo.

set conn=
set logfile=
set patchfile=
set install_dir=
set os=%PROGRAMFILES(x86)%
set winxp=0
set cwd=%~dp0%
set errno=0

ver | find "XP" > nul
if %ERRORLEVEL% == 0 set winxp=1

if defined os (
	set logfile="%PROGRAMFILES(x86)%\Bosch\VTX-VCI\VCI Software (Mazda-VCM-II)\VCI Identification Service\2.3.87.8.txt"
	set install_dir="%PROGRAMFILES(x86)%\Bosch\VTX-VCI\VCI Software (Mazda-VCM-II)\VCI Identification Service"
	set patchfile="%PROGRAMFILES(x86)%\Bosch\VTX-VCI\VCI Software (Mazda-VCM-II)\VCI Identification Service\2.3.87.8.patch"
) else (
	set logfile="%PROGRAMFILES%\Bosch\VTX-VCI\VCI Software (Mazda-VCM-II)\VCI Identification Service\2.3.87.8.txt"
	set install_dir="%PROGRAMFILES%\Bosch\VTX-VCI\VCI Software (Mazda-VCM-II)\VCI Identification Service"
	set patchfile="%PROGRAMFILES%\Bosch\VTX-VCI\VCI Software (Mazda-VCM-II)\VCI Identification Service\2.3.87.8.patch"
)
echo %date%-%time%: Checking if VCM II Software is installed on this PC
if not exist %install_dir% (
	echo %date%-%time%: ########################################
	echo %date%-%time%: VCM II Software not installed on this PC
	echo %date%-%time%: ########################################
	set errno=1
	goto exit
)	

echo. >> %logfile%
echo %date%-%time%: VCM II Software is installed on this PC >> %logfile%

::echo %date%-%time%: Checking For Admin Rights >> %logfile%

:: Check for Adminstrator Privledges
::>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

::REM --> If error flag set, we do not have admin.
::if '%errorlevel%' NEQ '0' (
::	echo %date%-%time%: #####################################
::    echo %date%-%time%: Needs to run with Admin Rights
::	echo %date%-%time%: Batch file started without Admin Rights >> %logfile%
::	echo %date%-%time%: #####################################
::	set errno=1
::	goto exit
::	)

echo %date%-%time%: Installing VCM II SW Components

echo %date%-%time%: Stopping MazdaVCMIIVCIIdentService >> %logfile%
:: Stopping VCI Identification Service
>nul 2>&1 sc stop MazdaVCMIIVCIIdentService
if '%errorlevel%' NEQ '0' (
	echo.
	echo %date%-%time%: ###############################################################
	echo %date%-%time%: Error Patching [Stopping vci-ident] VCM II Software error=%errorlevel%
	echo %date%-%time%: Error Patching [Stopping vci-ident] VCM II Software error=%errorlevel% >> %logfile%
	echo %date%-%time%: ###############################################################
	set errno=1
	goto exit
)
echo %date%-%time%: MazdaVCMIIVCIIdentService has stopped >> %logfile% 
echo %date%-%time%: Copying new vci-ident.exe to %install_dir% >> %logfile% 

:: Need to check if running on a 32 bit or 64 bit OS
>nul 2>&1 xcopy "%cwd%vci-ident.exe" %install_dir%\ /Y /Q
if %errorlevel% NEQ 0 (
	echo %date%-%time%: ############################################################
	echo %date%-%time%: Error Patching [Copying File] VCM II Software error=%errorlevel%
	echo %date%-%time%: Error Patching [Copying File] VCM II Software error=%errorlevel% >> %logfile%
	echo %date%-%time%: ############################################################
	set errno=1
	goto exit
)
echo %date%-%time%: New vci-ident.exe has been copied >> %logfile%
echo %date%-%time%: Starting new MazdaVCMIIVCIIdentService >> %logfile%

:: Starting VCI Identification Service
>nul 2>&1 sc start MazdaVCMIIVCIIdentService
if '%errorlevel%' NEQ '0' (
	echo %date%-%time%: ############################################################
	echo %date%-%time%: Error Patching [Starting vci-ident] VCM II Software Reboot PC
	echo %date%-%time%: Error Patching [Starting vci-ident] VCM II Software Reboot PC >> %logfile%
	echo %date%-%time%: ############################################################
	set errno=1
	goto exit
)
echo %date%-%time%: MazdaVCMIIVCIIdentService has started >> %logfile%
echo %date%-%time%: +++The VCM II Software has been patched+++
echo %date%-%time%: +++The VCM II Software has been patched+++ >> %logfile%

:: Looking for VCM II Network Adapter
echo %date%-%time%: Looking for VCM II Network Adapter. If found will enable it. >> %logfile%
for /f "tokens=1-2 delims=," %%i in ('getmac /v /fo csv ^| findstr "ETAS VCI"') do set conn=%%i

:: If the adapter was found, the VCM II is connected to the PC, enable the connection
echo %date%-%time%: Checking for VCM II
if defined conn (
	if %winxp% 	EQU 0 ( 
		echo %date%-%time%: A VCM II is detected on the PC Network Adapter: %conn%
		echo %date%-%time%: A VCM II is detected on the PC Network Adapter: %conn% >> %logfile%
		>nul 2>&1 netsh interface set interface name=%conn% admin=enable
		if %ERRORLEVEL% EQU 0 (
			echo %date%-%time%: The VCM II's USB Interface is Enabled
			echo %date%-%time%: The VCM II's USB Interface is Enabled >> %logfile%
		) else (
			echo %date%-%time%: #########################################
			echo %date%-%time%: Error Enabling the VCM II's USB Interface
			echo %date%-%time%: Error Enabling the VCM II's USB Interface errorlevel=%errorlevel% >> %logfile%
			echo %date%-%time%: #########################################
		)	
	) else (
		echo %date%-%time%: WIN XP Detected.
		echo %date%-%time%: WIN XP Detected, Cannot enable interface >> %logfile%
	)	
) else (
	echo %date%-%time%: A VCM II has NOT been Detected
	echo %date%-%time%: A VCM II has NOT been Detected >> %logfile%
)
echo.
echo. > %patchfile%

:exit
exit /B %errno%
