@echo off

rem
rem  This script will cleanup after Windows Setup (dpinst.exe)
rem  which may leave Registry keys and Directories after 
rem  Sangoma Driver is uninstalled. The script is envoked
rem  by Setup.exe and should NOT be used directly (at
rem  command line).
rem
rem  Version: 1.2
rem  Date June 29, 2010
rem  Author: David Rokhvarg <davidr@sangoma.com>

set WP_CURRENT_DIR=%cd%
set WP_LOG_FILE=%windir%\system32\drivers\wanpipelog.txt
set FOUND_DIRS=0
set IS_XP=0

rem re-set content of the Log file
rem echo File is reset. > %WP_LOG_FILE% 2>&1

rem echo WP_CURRENT_DIR is %WP_CURRENT_DIR%
rem echo WP_LOG_FILE is %WP_LOG_FILE%

rem =============================================================
rem Registry cleanup
rem =============================================================
rem Output of both stdout and stderr will be redirected to nul.

set GUID_DEVCLASS_SANGOMA_ADAPTER_STR={680D0063-63ED-4E66-A2CC-6F54CB331CF5}
reg delete HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\%GUID_DEVCLASS_SANGOMA_ADAPTER_STR% /f  > nul 2>&1


rem =============================================================
rem Driver Store cleanup
rem =============================================================

set XP_DriverStoreDir=%windir%\system32\DRVSTORE
set Vista_DriverStoreDir=%windir%\system32\DriverStore\FileRepository

rem 
rem  Detect which directory exist and based on it we'll know the OS: XP or Vista
rem  Output of both stdout and stderr will be redirected to nul
rem  because user is NOT interested in full directory list being
rem  printed to stdout.
rem
dir %Vista_DriverStoreDir% > nul 2>&1
if errorlevel 1 (set IS_XP=1)

set WpDriverStoreDir=%Vista_DriverStoreDir%
if %IS_XP%==1 (set WpDriverStoreDir=%XP_DriverStoreDir%)

rem This is only for testing, comment it out
rem set WpDriverStoreDir=%WP_CURRENT_DIR%
rem set WpDriverStoreDir=J:\development\tmp\DriverStore

echo %DATE% %TIME% >> %WP_LOG_FILE%
echo Searching Driver Store... >> %WP_LOG_FILE%
echo Driver Store Directory: %WpDriverStoreDir% >> %WP_LOG_FILE%

rem =============================================================
set SEARCH_DIR=%WpDriverStoreDir%\sngbus.inf_*
if %IS_XP%==1 (set SEARCH_DIR=%WpDriverStoreDir%\sngbus_*)
for /d %%d in (%SEARCH_DIR%) do (call :delete_DriverStore_dir_function %%d)
goto delete_DriverStore_sdladrv_dirs

rem =============================================================
:delete_DriverStore_sdladrv_dirs
set SEARCH_DIR=%WpDriverStoreDir%\sdladrv.inf_*
if %IS_XP%==1 (set SEARCH_DIR=%WpDriverStoreDir%\sdladrv_*)
for /d %%d in (%SEARCH_DIR%) do (call :delete_DriverStore_dir_function %%d)
goto delete_DriverStore_sprotocol_dirs

rem =============================================================
:delete_DriverStore_sprotocol_dirs
set SEARCH_DIR=%WpDriverStoreDir%\sprotocol.inf_*
if %IS_XP%==1 (set SEARCH_DIR=%WpDriverStoreDir%\sprotocol_*)
for /d %%d in (%SEARCH_DIR%) do (call :delete_DriverStore_dir_function %%d)
goto delete_DriverStore_netsango_dirs

rem =============================================================
:delete_DriverStore_netsango_dirs
set SEARCH_DIR=%WpDriverStoreDir%\netsango.inf_*
if %IS_XP%==1 (set SEARCH_DIR=%WpDriverStoreDir%\netsango_*)
for /d %%d in (%SEARCH_DIR%) do (call :delete_DriverStore_dir_function %%d)
goto check_dirs_found

:check_dirs_found
if %FOUND_DIRS%==1 goto finished_removing_DriverStore_directories
goto no_DriverStore_dirs_found

:delete_DriverStore_dir_function
echo Warning: Removing Directory: %1 >> %WP_LOG_FILE%
rmdir /s /q %1 >> %WP_LOG_FILE%
set FOUND_DIRS=1
goto :eof

:finished_removing_DriverStore_directories
echo Finished removing DriverStore directories! >> %WP_LOG_FILE%
goto remove_all_driver_directories_and_files

:no_DriverStore_dirs_found
echo No Sangoma-related DriverStore directories found. >> %WP_LOG_FILE%
goto remove_all_driver_directories_and_files

:delete_Dir_function_no_warning
rmdir /s /q %1 >> %WP_LOG_FILE% 2>&1
goto :eof

:delete_File_function_no_warning
del /q %1 >> %WP_LOG_FILE% 2>&1
goto :eof

:delete_File_from_System32drivers_dir
call :delete_File_function_no_warning %windir%\System32\drivers\%1
goto :eof

:delete_File_from_System32_dir
call :delete_File_function_no_warning %windir%\System32\%1
goto :eof

rem ##########################################
:remove_all_driver_directories_and_files

rem ===========================
rem SngBus files & directories:
rem ===========================
call :delete_Dir_function_no_warning %windir%\sang_ec_files
call :delete_File_from_System32drivers_dir SngBus.sys
call :delete_File_from_System32_dir SngBusCfg.dll

rem ===========================
rem sdladrv files & directories:
rem ===========================
call :delete_File_from_System32_dir sdlacfg.dll
call :delete_File_from_System32_dir wanpipemon.exe
call :delete_File_from_System32_dir stail.exe
call :delete_File_from_System32_dir wan_xilinx_test.exe
call :delete_File_from_System32_dir wan_aftup.exe
call :delete_File_from_System32_dir wanec_client.exe
call :delete_File_from_System32_dir stelephony.dll
call :delete_File_from_System32_dir libsangoma.dll
call :delete_File_from_System32_dir waneclib.dll
call :delete_File_from_System32_dir wanrouter.exe
call :delete_File_from_System32_dir wancfglib.dll

call :delete_File_from_System32drivers_dir sdladrv.sys
rem call :delete_File_from_System32drivers_dir wanpipelog.txt
goto end
rem ##########################################

:end

