@echo off
Set _Args=%*
if `%1` neq `` Set "_Args=%_Args:"=""%"
if /i "%UserName%" == "SYSTEM" (Goto GotAdmin) else (reg query "HKLM\SYSTEM\ControlSet001\Control\MiniNT" 1>nul 2>nul&&Goto GotAdmin)
if exist %WinDir%\System32\fltMC.exe fltMC 1>nul 2>nul||mshta VBScript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c """"%~f0"" %_Args%""",,"runas",1)(Window.Close) 2>nul&&Exit /b

:GotAdmin
Title 搜狗拼音&if exist %WinDir%\System32\ureg.dll Mode 40,11 2>nul
Pushd "%CD%"&cd /d "%~dp0"
REM 判断启动条件
Set SGver=&for /f "delims=" %%i in ('dir /a:d /o:d /b 2^>nul') do if exist "%%~i\Data\runtime.ini" if exist "%%~i\sgim_py.bin" Set "SGver=%%~i"
if not defined SGver Call :EchoX "cf.: 找不到 [版本号] 文件夹。"&Call :Delay 5 +&Exit /b
find /? 1>nul 2>nul&&Set "findstr=find /i "||Set findstr=
findstr /? 1>nul 2>nul&&Set "findstr=findstr /i /c:"
if not defined findstr Call :EchoX "cf.: 找不到 find.exe 文件。"&Call :Delay 5 +&Exit /b
reg /? 1>nul 2>nul||(Call :EchoX "cf.: 找不到 reg.exe 文件。"&Call :Delay 5 +&Exit /b)
REM 卸载初始化
cd "%SGver%" 1>nul 2>nul&if defined CommonProgramW6432 (Set NirCmd=_Green\NirCmd64.exe) else Set NirCmd=_Green\NirCmd.exe
Set NirCmd=&if exist "%NirCmd%" for /f %%i in ('%NirCmd% consolewrite ok 2^>nul') do Set "NirCmd=%NirCmd%"

:Begin
tskill /? 1>nul 2>nul&&Set "KillSGT=tskill SGTool"||(Set "KillSGT=taskkill /f /im SGTool.exe /t"&if defined NirCmd Set "KillSGT="%NirCmd%" killprocess SGTool")
Set "KillSGT=del /q SGTool.run 1>nul 2>nul&%KillSGT% 1>nul 2>nul"
%KillSGT%&&Call :Delay 2
copy /y SGTool.bak SGTool.exe 1>nul 2>nul&del /q SGTool.bak 1>nul 2>nul
ren SGTool.nop SGTool._op 1>nul 2>nul

:UnInsta
Call :EchoX "8f.:正在卸载中，请稍候。。。"
SGTool.exe --appid=install -u 1>nul 2>nul
for %%i in ("%CD%\SogouTSF.dll" "%WinDir%\System32\SogouTSF.ime" "%WinDir%\SysWOW64\SogouTSF.ime") do regsvr32 /s /u "%%~i"
net stop SogouSvc 1>nul 2>nul&sc delete SogouSvc 1>nul 2>nul&reg delete "HKLM\SYSTEM\CurrentControlSet\Services\SogouSvc" /f 1>nul 2>nul
for %%i in ("SG_SogouPY" "SG_SogouPY64" "SG_SogouTSF" "SG_SogouTSF64" "SG_SGTool") do Call :BlockNet - "%%~i"
for %%i in ("" "WOW6432Node\") do (
    reg delete "HKLM\SOFTWARE\%%~iSogouInput" /f 1>nul 2>nul
    reg delete "HKLM\SOFTWARE\%%~iSogouComponents" /f 1>nul 2>nul
    reg delete "HKLM\SOFTWARE\Classes\%%~iAppID\{42BC8AF2-30A3-4A62-9586-812EDA0747ED}" /f 1>nul 2>nul
    reg delete "HKLM\SOFTWARE\Classes\%%~iAppID\{60E38716-01BE-4AF1-8794-4B090BDA98D6}" /f 1>nul 2>nul
    reg delete "HKLM\SOFTWARE\Classes\%%~iAppID\SogouImeBroker.EXE" /f 1>nul 2>nul
    reg delete "HKLM\SOFTWARE\Classes\%%~iAppID\SogouPinyinUpdate.EXE" /f 1>nul 2>nul
    reg delete "HKLM\SOFTWARE\Classes\%%~iCLSID\{86EA3EF3-5BF4-4EA4-B05B-749F84EB7AF4}" /f 1>nul 2>nul
    reg delete "HKLM\SOFTWARE\Classes\%%~iCLSID\{B6891650-D273-4F34-84FF-AAC043EC8956}" /f 1>nul 2>nul
    reg delete "HKLM\SOFTWARE\Classes\%%~iCLSID\{CC43AF26-60C3-4612-B58D-27A07A40E90B}" /f 1>nul 2>nul
    reg delete "HKLM\SOFTWARE\Classes\%%~iCLSID\{E7EA138E-69F8-11D7-A6EA-00065B844310}" /f 1>nul 2>nul
    reg delete "HKLM\SOFTWARE\Classes\%%~iInterface\{016AF059-1078-42B3-9D21-7F75589D41CC}" /f 1>nul 2>nul
    reg delete "HKLM\SOFTWARE\Classes\%%~iInterface\{8A829337-30D7-478C-B8AD-31B2B2569468}" /f 1>nul 2>nul
    reg delete "HKLM\SOFTWARE\Classes\%%~iTypeLib\{923E08BA-41BA-46D7-B5C8-19AEFFB15EB0}" /f 1>nul 2>nul
    reg delete "HKLM\SOFTWARE\Classes\%%~iTypeLib\{D4E13478-2426-453B-AA7C-CC57667506C3}" /f 1>nul 2>nul
    reg delete "HKLM\SOFTWARE\%%~iMicrosoft\Internet Explorer\Low Rights\ElevationPolicy\{0A6E682E-614C-4fda-BBAD-9C71DB6C1B2C}" /f 1>nul 2>nul
    reg delete "HKLM\SOFTWARE\%%~iMicrosoft\Internet Explorer\Low Rights\ElevationPolicy\{4D6DE936-912B-4aa1-A167-38A29A691337}" /f 1>nul 2>nul
    reg delete "HKLM\SOFTWARE\%%~iMicrosoft\Internet Explorer\Low Rights\ElevationPolicy\{7AF645B2-3F1F-4639-81A1-39D794DC8A7D}" /f 1>nul 2>nul
    reg delete "HKLM\SOFTWARE\%%~iMicrosoft\Internet Explorer\Low Rights\ElevationPolicy\{C7F9CD7C-5AC6-470b-A50F-F7CC28D0771B}" /f 1>nul 2>nul
    reg delete "HKLM\SOFTWARE\%%~iMicrosoft\Internet Explorer\Low Rights\ElevationPolicy\{C7F9CD7C-5AC6-470b-A50F-F7CC28D0771C}" /f 1>nul 2>nul
    reg delete "HKLM\SOFTWARE\%%~iMicrosoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION" /f /v "SGMyInput.exe" 1>nul 2>nul
    reg delete "HKLM\SOFTWARE\%%~iMicrosoft\Windows\CurrentVersion\Uninstall\Sogou Input" /f 1>nul 2>nul
)
for %%i in ("HKLM" "HKCU") do (
    reg delete "%%~i\SOFTWARE\Microsoft\CTF\TIP\{CC43AF26-60C3-4612-B58D-27A07A40E90B}" /f 1>nul 2>nul
    reg delete "%%~i\SOFTWARE\Microsoft\CTF\TIP\{E7EA138E-69F8-11D7-A6EA-00065B844310}" /f 1>nul 2>nul
)
for %%i in ("SogouInput" "SogouInput.ppup" "SogouInput.user" "SogouInput.ppup.user" "SogouInput.store.user") do reg delete "HKCU\SOFTWARE\%%~i" /f 1>nul 2>nul
for %%i in ("SGMouseInput" "SGSmartAssistant") do reg delete "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /f /v "%%~i" 1>nul 2>nul
for %%i in (".scpf" "sgreg" "SogouComponentPackageFile" "SogouImeBroker.SogouBroker" "SogouImeBroker.SogouBroker.1" "SogouPinyinUpdate.SGUpdate" "SogouPinyinUpdate.SGUpdate.1") do reg delete "HKLM\SOFTWARE\Classes\%%~i" /f 1>nul 2>nul
for %%i in ("2796BAE63F1801E277261BA0D77770028F20EEE4" "DF3C24F9BFD666761B268073FE06D1CC8D4F82A4" "B1BC968BD4F49D622AA89A81F2150152A41D829C") do reg delete "HKLM\SOFTWARE\Microsoft\SystemCertificates\AuthRoot\Certificates\%%~i" /f 1>nul 2>nul
for %%i in ("SogouCellDict" "SogouSkinFile") do reg delete "HKLM\SOFTWARE\Classes\%%~i\Entry\SogouPY" /f 1>nul 2>nul
for /f "tokens=2*" %%i in ('reg query "HKLM\SOFTWARE\Classes\SogouCellDict\Shell\Open\Command" /ve 2^>nul') do echo "%%~j"|%findstr%"%SGver%" 1>nul 2>nul&&Call :DelCDSF .scel SogouCellDict
for /f "tokens=2*" %%i in ('reg query "HKLM\SOFTWARE\Classes\SogouSkinFile\Shell\Open\Command" /ve 2^>nul') do echo "%%~j"|%findstr%"%SGver%" 1>nul 2>nul&&Call :DelCDSF .ssf SogouSkinFile
for %%i in ("CUAS" "DirectSwitchHotkeys\00001000" "Assemblies\0x00000409") do reg delete "HKCU\SOFTWARE\Microsoft\CTF\%%~i" /f 1>nul 2>nul
reg delete "HKLM\SOFTWARE\Microsoft\SystemCertificates\ROOT\Certificates\7BAEDDDA4AF9DF8BE966FD8FF389D3A1DB4E8997" /f 1>nul 2>nul
Call :DelReg "HKCU\SOFTWARE\Chromium" "BLBeacon"
Set EUDC=EUDC.TTE&for /f "tokens=2*" %%i in ('reg query "HKCU\EUDC\936" /v "SGPYBackupSystemDefaultEUDCFont" 2^>nul') do Set "EUDC=%%~j"
reg add "HKCU\EUDC\936" /f /v "SystemDefaultEUDCFont" /t REG_SZ /d "%EUDC%" 1>nul 2>nul
reg delete "HKCU\EUDC\936" /f /v "SGPYBackupSystemDefaultEUDCFont" 1>nul 2>nul
assoc .=. 1>nul 2>nul&reg delete "HKCR\." /f 1>nul 2>nul
for /f "tokens=2*" %%i in ('reg query "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Desktop" 2^>nul') do Set "Desktop=%%~j"
for /f "tokens=2*" %%i in ('reg query "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Programs" 2^>nul') do Set "Programs=%%~j"
for /f "tokens=3*" %%i in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Common Desktop" 2^>nul') do Set "CDesktop=%%~j"
for /f "tokens=3*" %%i in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Common Programs" 2^>nul') do Set "CPrograms=%%~j"
for /f "tokens=3*" %%i in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Common AppData" 2^>nul') do Set "CAppData=%%~j"
for %%i in ("属性设置" "输入法管理器" "符号大全" "常用诗词" "截图工具" "手写输入" "语音输入" "跨屏输入" "图片表情" "点点输入" "搜狗 - 点点输入" "颜文字") do (
    del /q "%Desktop%\%%~i.lnk" 1>nul 2>nul&del /q "%CDesktop%\%%~i.lnk" 1>nul 2>nul
    del /q "%%~i.lnk" 1>nul 2>nul&del /q "%~dp0%%~i.lnk" 1>nul 2>nul
)
for %%i in ("搜狗拼音" "搜狗输入法") do rd /s /q "%Programs%\%%~i" 1>nul 2>nul&rd /s /q "%CPrograms%\%%~i" 1>nul 2>nul
for %%i in ("\LOG" "\Logs" "" ".users" ".users.backup") do (
    rd /s /q "%AppData%\SogouPY%%~i" 1>nul 2>nul
    rd /s /q "%UserProfile%\AppData\LocalLow\SogouPY%%~i" 1>nul 2>nul
    rd /s /q "%UserProfile%\AppData\LocalLow\SogouSA%%~i" 1>nul 2>nul
)
for %%i in ("%Public%\SogouInput" "%AppData%\SogouInput" "%CAppData%\SogouInput" "%AppData%\SGAIAssistant" "%CAppData%\SGAIAssistant") do rd /s /q "%%~i" 1>nul 2>nul
for %%i in ("CEF\User Data\Dictionaries" "CEF\User Data" "CEF") do rmdir "%AppData%\..\LocaL\%%~i" 1>nul 2>nul
for %%i in ("System32" "SysWOW64") do rd /s /q "%WinDir%\%%~i\IME\SogouPY" 1>nul 2>nul
for %%i in ("sgim_core.bin" "sgim_charvalid.bin") do del /f /q "%CD%\%%~i" 1>nul 2>nul
for %%i in ("sgim_bigram_serdata.bin" "sgim_gd_old_sys_serdata.bin") do del /f /q "%CD%\%%~i.*" 1>nul 2>nul
for %%i in (".dmp" ".rpt" "_tmp.bin") do del /f /q "%CD%\Data\*%%~i" 1>nul 2>nul
for %%i in ("debug.log" "..\debug.log" "..\Components\debug.log" "cef_record_nocheck.log" "Data\SmartAssistant.log" "Data\SGSmartAssistant.log") do attrib -s -h "%CD%\%%~i" 1>nul 2>nul&del /f /q "%CD%\%%~i" 1>nul 2>nul
for %%i in ("debug.log" "cef_record_nocheck.log") do attrib -s -h "%WinDir%\%%~i" 1>nul 2>nul&del /f /q "%WinDir%\%%~i" 1>nul 2>nul
for %%i in ("debug.log" "SogouPY.ime" "SogouTSF.ime" "nsm7A10.tmp" "nsv967F.tmp") do del /f /q "%WinDir%\System32\%%~i" 1>nul 2>nul&del /f /q "%WinDir%\SysWOW64\%%~i" 1>nul 2>nul
attrib -s -h "%UserProfile%\debug.log" 1>nul 2>nul&del /f /q "%UserProfile%\debug.log" 1>nul 2>nul
:: rd /s /q "..\Components" 1>nul 2>nul&rd /s /q "..\SogouExe" 1>nul 2>nul
del /f /q "%CD%\*_tmp.bin" 1>nul 2>nul
del /q "%~dp0更多设置.lnk" 1>nul 2>nul
Goto End

:End
%KillSGT%
Set SGCount=0&for %%i in ("%WinDir%\System32\SogouPY.ime" "%WinDir%\SysWOW64\SogouPY.ime") do if exist %%i Set /a SGCount+=1
if %SGCount% gtr 0 Call :EchoX "cf.:文件被占用，请重启后再卸载一次！！"&Call :Delay 5 +
Exit /b

:DelReg :: 删除注册表项 (父项路径\子项)，删除后父项无任何子项则也删除 <%1=PReg|父项路径> <%2=SReg|子项>。
Set "PReg=%~1"&Set "SReg=%~2"
reg delete "%PReg%\%SReg%" /f 1>nul 2>nul
ver|%findstr%" 5." 1>nul 2>nul&&Set n=-2||Set n=0
for /f "delims=" %%i in ('reg query "%PReg%" /s 2^>nul') do Set /a n+=1
if %n% lss 1 reg delete "%PReg%" /f 1>nul 2>nul
Goto :eof

:BlockNet :: 屏蔽应用联网
Set "aName=%~2"&Set "aPath=%~3"
if defined aName if "%~1" == "+" (
    netsh advfirewall firewall add rule name="%aName%_BlockNet" dir=out program="%aPath%" action=block 1>nul 2>nul
REM netsh advfirewall firewall add rule name="%aName%_BlockNet" dir=in program="%aPath%" action=block 1>nul 2>nul
) else netsh advfirewall firewall delete rule name="%aName%_BlockNet" 1>nul 2>nul
Goto :eof

:DelCDSF :: 删除关联[词库与皮肤]文件 <%1=.scel|.ssf> <%2=SogouCellDict|SogouSkinFile>
reg delete "HKLM\SOFTWARE\Classes\%~1" /f 1>nul 2>nul
reg delete "HKLM\SOFTWARE\Classes\%~2" /f 1>nul 2>nul
reg delete "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\%~1" /f 1>nul 2>nul
reg delete "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\ApplicationAssociationToasts" /f /v "%~2_%~1" 1>nul 2>nul
Goto :eof

:Delay :: 延迟操作 <%1=Sec|延迟秒数> [%2=+|显示倒计时]。
if "%~2" == "+" (Set #=2) else Set #=1&if defined NirCmd @start "" /wait "%NirCmd%" wait %~1000 1>nul 2>nul&Goto :eof
if exist %WinDir%\System32\timeout.exe (timeout /t %~1 %#%>nul) else if exist %WinDir%\System32\choice.exe (choice /t %~1 /d y /n >nul) else ping 127.1 -n %~1 >nul
Goto :eof

:EchoX :: 显示彩色文字 (不支持半角字符 \ / : * ? " < >|. % ! ~)。
Setlocal EnableDelayedExpansion
Set echox=EchoX.exe&&!echox! 1>nul 2>nul||(Set echox=&mkdir "%TEMP%\EchoX" 2>nul&&attrib +s +h "%TEMP%\EchoX" 2>nul)
for %%a in (%*) do (
    Set "param=%%~a"&Set "color=!param:~0,2!"&(if not exist %WinDir%\System32\findstr.exe if not defined echox Set "color=xx")
    Set n=0&(if "!param:~2,2!" == "::" Set n=1)&(if "!param:~2,2!" == ".:" Set n=2)
    if !n! gtr 0 (
        if /i "!color!" == "xx" (Set /p="_!param:~4!"<nul) else (Set param=%%~nxa&if defined echox (!echox! -c !color! -n "!param:~4!") else (Pushd "%TEMP%\EchoX" 2>nul&>"!param:~4!",Set /p= <nul&findstr /a:!color! .* "!param:~4!*"&del "!param:~4!"&Popd))
        if !n! == 2 echo.
    ) else if defined param Set /p="_!param!"<nul
)
Endlocal&Goto :eof