@echo off

setlocal EnableDelayedExpansion

echo INFO: start uninstalling Office Integration.

echo INFO: Check whether an old DI 32 Bit version is present
reg query HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\forcont\Forcont.FactoryAddinFX2 > NUL 2>&1
if errorlevel 1 (
  echo INFO: No old DI 32 Bit was found, so there's nothing to be done.
) else (
  echo INFO: Old DI 32 Bit was was detected, try to uninstall it.
  for /f "tokens=2*" %%i in ('Reg Query "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\forcont\Forcont.FactoryAddinFX2" /v "Dir" 2^>NUL') do set "DllDir32=%%j"
  if defined DllDir32 (
	if not exist "!DllDir32!\OfficeAddinFX2.dll" (
	  echo ERROR: Failed to unregister OfficeAddinFX2.dll, !DllDir32!\OfficeAddinFX2.dll doesn't exist!
	  exit /B 2
	)
	echo INFO: Try to unregister !DllDir32!\OfficeAddinFX2.dll
	%windir%\SysWOW64\regsvr32.exe /s /u "!DllDir32!\OfficeAddinFX2.dll"
    if errorlevel 1 (
      echo ERROR: Failed to unregister OfficeAddinFX2.dll.
	  exit /B 3
    )
  ) else ( 
	echo ERROR: Unable to determine path to OfficeAddinFX2.dll
	exit /B 1
  )
  
  reg query HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\forcont\Forcont.FactoryAddinFX2 > NUL 2>&1 

  if not errorlevel 1 (
    echo ERROR: Failed to unregister OfficeAddinFX2.dll. Registry key HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\forcont\Forcont.FactoryAddinFX2 still exists.
	exit /B 4
  )
  echo INFO: OfficeAddinFX2.dll has been successfully unregistered.
  echo INFO: Old DI 32 Bit version has been successfully uninstalled.
)

echo INFO: Check whether FDC additional components 32 Bit are present
reg query HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\forcont\Forcont.FactoryAddin > NUL 2>&1

if errorlevel 1 (
  echo INFO: No FDC additional components 32 Bit were found, so there's nothing to be done.
) else (
  echo INFO: FDC additional components 32 Bit were detected, try to uninstall them.
  for /f "tokens=2*" %%i in ('Reg Query "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\forcont\Forcont.FactoryAddin" /v "Dir" 2^>NUL') do set "DllDir32FDC=%%j"
  if defined DllDir32FDC (
    if not exist "!DllDir32FDC!\OfficeAddin.dll" (
	  echo ERROR: Failed to unregister OfficeAddin.dll, !DllDir32FDC!\OfficeAddin.dll doesn't exist!
	  exit /B 6
	)
	echo INFO: Try to unregister !DllDir32FDC!\OfficeAddin.dll
	%windir%\SysWOW64\regsvr32.exe /s /u "!DllDir32FDC!\OfficeAddin.dll"
    if errorlevel 1 (
      echo ERROR: Failed to unregister OfficeAddin.dll.
	  exit /B 7
    )
  ) else ( 
	echo ERROR: Unable to determine path to OfficeAddin.dll
	exit /B 5
  )
  
  reg query HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\forcont\Forcont.FactoryAddin > NUL 2>&1 

  if not errorlevel 1 (
    echo ERROR: Failed to unregister OfficeAddin.dll. Registry key HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\forcont\Forcont.FactoryAddin still exists.
	exit /B 8
  )
  echo INFO: OfficeAddin.dll has been successfully unregistered.
  echo INFO: FDC additional components 32 Bit have been successfully uninstalled.
)

echo INFO: Check whether an old DI 64 Bit version is present
reg query HKEY_LOCAL_MACHINE\SOFTWARE\forcont\Forcont.FactoryAddinFX2 > NUL 2>&1
if errorlevel 1 (
  echo INFO: No old DI 64 Bit was found, so there's nothing to be done.
) else (
  echo INFO: Old DI 64 Bit was was detected, try to uninstall it.
  for /f "tokens=2*" %%i in ('Reg Query "HKEY_LOCAL_MACHINE\SOFTWARE\forcont\Forcont.FactoryAddinFX2" /v "Dir" 2^>NUL') do set "DllDir64=%%j"
  if defined DllDir64 (
	if not exist "!DllDir64!\OfficeAddinFX2-64.dll" (
	  echo ERROR: Failed to unregister OfficeAddinFX2-64.dll, !DllDir64!\OfficeAddinFX2-64.dll doesn't exist!
	  exit /B 10
	)
	echo INFO: Try to unregister !DllDir64!\OfficeAddinFX2-64.dll
	%windir%\SysWOW64\regsvr32.exe /s /u "!DllDir64!\OfficeAddinFX2-64.dll"
    if errorlevel 1 (
      echo ERROR: Failed to unregister OfficeAddinFX2-64.dll.
	  exit /B 11
    )
  ) else ( 
	echo ERROR: Unable to determine path to OfficeAddinFX2-64.dll
	exit /B 9
  )
  
  reg query HKEY_LOCAL_MACHINE\SOFTWARE\forcont\Forcont.FactoryAddinFX2 > NUL 2>&1 

  if not errorlevel 1 (
    echo ERROR: Failed to unregister OfficeAddinFX2.dll-64. Registry key HKEY_LOCAL_MACHINE\SOFTWARE\forcont\Forcont.FactoryAddinFX2 still exists.
	exit /B 12
  )
  echo INFO: OfficeAddinFX2-64.dll has been successfully unregistered.
  echo INFO: Old DI 64 Bit version has been successfully uninstalled.
)

echo INFO: Check whether FDC additional components 64 Bit are present
reg query HKEY_LOCAL_MACHINE\SOFTWARE\forcont\Forcont.FactoryAddin > NUL 2>&1

if errorlevel 1 (
  echo INFO: No FDC additional components 64 Bit were found, so there's nothing to be done.
) else (
  echo INFO: FDC additional components 64 Bit were detected, try to uninstall them.
  for /f "tokens=2*" %%i in ('Reg Query "HKEY_LOCAL_MACHINE\SOFTWARE\forcont\Forcont.FactoryAddin" /v "Dir" 2^>NUL') do set "DllDir64FDC=%%j"
  if defined DllDir64FDC (
	if not exist "!DllDir64FDC!\OfficeAddin-64.dll" (
	  echo ERROR: Failed to unregister OfficeAddin-64.dll, !DllDir64FDC!\OfficeAddin-64.dll doesn't exist!
	  exit /B 14
	)
	echo INFO: Try to unregister !DllDir64FDC!\OfficeAddin-64.dll
	%windir%\SysWOW64\regsvr32.exe /s /u "!DllDir64FDC!\OfficeAddin-64.dll"
    if errorlevel 1 (
      echo ERROR: Failed to unregister OfficeAddin-64.dll.
	  exit /B 15
    )
  ) else ( 
	echo ERROR: Unable to determine path to OfficeAddin-64.dll
	exit /B 13
  )

  reg query HKEY_LOCAL_MACHINE\SOFTWARE\forcont\Forcont.FactoryAddin > NUL 2>&1 

  if not errorlevel 1 (
    echo ERROR: Failed to unregister OfficeAddin-64.dll. Registry key HKEY_LOCAL_MACHINE\SOFTWARE\forcont\Forcont.FactoryAddin still exists.
	exit /B 16
  )
  echo INFO: OfficeAddin-64.dll has been successfully unregistered.
  echo INFO: FDC additional components 64 Bit have been successfully uninstalled.
)

rem since there is no defined method to check if redemption is installed just run regsvr32 /u and check errorlevel
echo INFO: Try to unregister Redemption.dll.
%windir%\SysWOW64\regsvr32.exe /s /u .\Redemption\Redemption.dll
if errorlevel 1 (
  echo WARN: Failed to unregister Redemption.dll.
) else (
  echo INFO: Redemption.dll has been successfully unregistered.
)

echo INFO: Try to unregister Redemption64.dll.
%windir%\SysWOW64\regsvr32.exe /s /u .\Redemption\Redemption64.dll
if errorlevel 1 (
  echo WARN: Failed to unregister Redemption64.dll.
) else (
  echo INFO: Redemption64.dll has been successfully unregistered.
)

rem please uncomment the following commented lines if adobe acrobat integration has to be removed as well
rem echo INFO: try to uninstall adobe acrobat integration plugin
rem del "C:\Programme\Adobe\Acrobat 9.0\Acrobat\plug_ins\factoryStorage.api"
rem if errorlevel 1 (
rem   echo ERROR: failed to delete "C:\Programme\Adobe\Acrobat 9.0\Acrobat\plug_ins\factoryStorage.api". please delete it manually!
rem   exit /B 5
rem )
rem echo INFO: adobe acrobat integration plugin has been successfully removed

echo INFO: Office Integration has been successfully uninstalled.
exit /B 0