@echo off
if /i "%UserName%" == "SYSTEM" (Goto GotAdmin) else (reg query "HKLM\SYSTEM\ControlSet001\Control\MiniNT" 1>nul 2>nul&&Goto GotAdmin)
:BatchGotAdmin
Set _Args=&Set Args=%*
if `%1` neq `` Set "_Args=%Args:"=""%"
cd /d "%WinDir%\System32" 2>nul&if exist fltMC.exe fltMC 1>nul 2>nul||(echo CreateObject^("Shell.Application"^).ShellExecute "cmd.exe","/c """"%~f0"" %_Args%""",,"runas",1 >"%TEMP%\GA.vbs"&(CScript 1>nul 2>nul&&CScript //nologo "%TEMP%\GA.vbs" 1>nul 2>nul||"%TEMP%\GA.vbs" 2>nul)&del /a/f/q "%TEMP%\GA.vbs" 2>nul&Exit /b)

:GotAdmin
Pushd "%CD%"&cd /d "%~dp0"
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 1
reg /? 1>nul 2>nul||(Call :EchoX "cf.: 找不到 reg.exe 文件。"&Call :Delay 5 +&Exit /b 1)
Set "Mode=if exist %WinDir%\System32\ureg.dll Mode"
REM 判断输入参数
Set hint=&Set choice=
if /i "%~1" == "install" Goto Install
if /i "%~1" == "uninsta" Goto UnInsta

:Start
Title &ver|%findstr%" 5." 1>nul 2>nul&&Title CareUEyes&(%Mode% 35,15 2>nul)&Color 2f
Set flag=√メＱ№
Set caption=护眼软件 CareUEyes
Set hint=启用提示
Set err=0

:Menu
Cls&Call :EchoX "标题:「 " "2e::%caption%" "xx.: 」"
echo ----------------------------------
echo 菜单:
Call :EchoX "      " "f2::①" "  绿化便携        " "9f.:%flag:~0,1%"
echo.
Call :EchoX "      " "f2::②" "  清理卸载        " "8f.:%flag:~1,1%"
echo.
Call :EchoX "      " "f2::③" "  退出            " "6f.:%flag:~2,1%"
echo.
echo ----------------------------------
if %err% gtr 0 Call :EchoX "cf::输入无效请重新输入！" "cf.:(%flag:~3,1%%err%)"
Set choice=&Set /p choice=选择: ||Goto Menu
Set "choice=%choice:"=%"
Set "choice=%choice: =%"
if /i "%choice%" == "1" Cls&Goto Install
if /i "%choice%" == "i" Cls&Goto Install
if /i "%choice%" == "2" Cls&Goto UnInsta
if /i "%choice%" == "u" Cls&Goto UnInsta
if /i "%choice%" == "3" Popd&Exit
if /i "%choice%" == "q" Popd&Exit
Set /a err+=1&Goto Menu

:Install
if /i "%~2" == "#" Title &(%Mode% 36,4 2>nul)&Color 2f&echo.&echo      正在绿化便携，请稍候 。。。
if defined hint Call :EchoX "9f.:正在绿化便携，请稍候 。。。"
Set hint=&Call :UnInsta&Set hint=%hint%
REM 添加注册表项
if defined CommonProgramW6432 (Set xcpu=WOW6432Node\) else Set xcpu=
reg add "HKCU\SOFTWARE\CareUEyes" /f /v "code" /t REG_SZ /d "MTExMQ==" 1>nul 2>nul
reg add "HKCU\SOFTWARE\CareUEyes" /f /v "ver" /t REG_DWORD /d "0" 1>nul 2>nul
reg add "HKCU\SOFTWARE\Classes\%xcpu%CLSID\{FC36FD6A-7586-4ad1-8CBF-EB8AB7A51533}" /f /v "uuid" /t REG_SZ /d "YWRhYTM5MzJiZGU1ODFiZGM2MDZiNzI0YjUxMTJjNjA=" 1>nul 2>nul
REM 恢复配置文件
mkdir "%AppData%\CareUEyes" 2>nul
if not exist Setting_v2.dat (
(echo [common]
 echo antidisturb=1
 echo autorun=0
 echo autoupdate=0
 echo close2tray=1
 echo license=MQ==
 echo lang=lang_cn
 echo option_red_dot1=1
 echo reviewed=1
 echo [screen]
 echo advanced_api_guide=1
 echo [focus]
 echo focus_blur_hk=65^|5
 echo focus_hk2=81^|5
 echo [hotkey]
 echo disable_hk=87^|5
 echo editing_hk=69^|5
 echo reading_hk=82^|5
 echo [magic_window]
 echo magic_toolbar=1) >Setting_v2.dat)
mklink "%AppData%\CareUEyes\Setting_v2.dat" "%~dp0Setting_v2.dat" 1>nul 2>nul||copy /y "%~dp0Setting_v2.dat" "%AppData%\CareUEyes\Setting_v2.dat" 1>nul 2>nul
if /i "%~2" == "@" Goto End
REM 创建桌面快捷方式
Set "XD=%CD:!=ˇ%"&Setlocal EnableDelayedExpansion
Set "XD=!XD:%%=%%%%!"
Endlocal&Set "XD=%XD:ˇ=!%"
for /f "tokens=2*" %%i in ('reg query "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Desktop" 2^>nul') do Set "Desktop=%%~j"
Call :CreateLnk "%Desktop%\CareUEyes.lnk" "%XD%\CareUEyes.exe" "" "%XD%\CareUEyes.exe" 0 "电脑护眼软件"
Goto End

:UnInsta
if /i "%~2" == "#" Title &(%Mode% 36,4 2>nul)&Color 8f&echo.&echo      正在清理卸载，请稍候 。。。
if defined hint Call :EchoX "8f.:正在清理卸载，请稍候 。。。"
REM 结束进程
taskkill /f /im CareUEyes.exe 1>nul 2>nul&tskill CareUEyes 1>nul 2>nul&Pecmd KILL CareUEyes 1>nul 2>nul
REM 清理注册表项
reg delete "HKCU\SOFTWARE\CareUEyes" /f 1>nul 2>nul
reg delete "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /f /v "CareUEyes" 1>nul 2>nul
for %%i in ("HKCU" "HKLM") do for %%j in ("" "WOW6432Node\") do for %%k in ("{35125042-FD30-44fb-9006-2A5ACC5D8A2E}" "{FC36FD6A-7586-4ad1-8CBF-EB8AB7A51533}" "{05A62E4C-7FFA-4de2-A875-DB50C66A705C}") do reg delete "%%~i\SOFTWARE\Classes\%%~jCLSID\%%~k" /f 1>nul 2>nul
reg delete "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ICM" /f /v "GdiICMGammaRange" 1>nul 2>nul
reg delete "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{5BADBF50-D68C-4BE5-9DF1-BBD3D0192334}_is1" /f 1>nul 2>nul
Call :DelReg "HKCU\SOFTWARE\Google\Chrome\NativeMessagingHosts" "com.careueyes.dimmer"
Call :DelReg "HKCU\SOFTWARE\Google\Chrome" "NativeMessagingHosts"
Call :DelReg "HKCU\SOFTWARE\Google" "Chrome"
REM 删除配置文件
rd /s/q "%AppData%\CareUEyes" 1>nul 2>nul
REM 删除桌面快捷方式
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=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=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 Programs" 2^>nul') do Set "CPrograms=%%~j"
del /a/f/q "%Desktop%\CareUEyes.lnk" 1>nul 2>nul&del /a/f/q "%CDesktop%\CareUEyes.lnk" 1>nul 2>nul
rd /s/q "%Programs%\CareUEyes" 1>nul 2>nul&rd /s/q "%CPrograms%\CareUEyes" 1>nul 2>nul
(for %%i in (2 u) do if /i "%choice%" == "%%~i" Goto End)&if /i "%~1" == "uninsta" (Goto End) else Goto :eof

:End
if defined choice (
    Cls&if defined hint (Call :EchoX "2e.:操作完成！！"&Call :Delay 2 +)
    Goto Start
) else Set hint=&Exit /b 0

: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

:CreateLnk :: 创建快捷方式 (%号需要转义)。
Set "CLnk=%~1"&Set "CTPath=%~2"&Set "CArgs=%~3"&Set "CIcon=%~4"&Set "CIndex=%~5"&Set "CDesc=%~6"&Set "CWDir=%~7"&Set "CWStyle=%~8"
(if defined CLnk Set "CLnk=%CLnk:^^=^%")&(if defined CTPath Set "CTPath=%CTPath:^^=^%")&(if defined CArgs Set "CArgs=%CArgs:^^=^%")&(if defined CIcon Set "CIcon=%CIcon:^^=^%")&(if defined CWDir Set "CWDir=%CWDir:^^=^%")
SCut.exe /? 1>nul 2>nul&&SCut.exe /p "%CLnk%" /t "%CTPath%" /a "%CArgs%" /w "%CWDir%" /i "%CIcon%" /n "%CIndex%" /d "%CDesc%" /s "normal" /u 1>nul 2>nul&&Goto :eof
if not defined CWDir if not defined CWStyle Shortcut.exe 1>nul 2>nul&&Shortcut.exe "%CLnk%" "%CTPath%" "%CArgs%" "%CIcon%" %CIndex% nor "" "%CDesc%" 1>nul 2>nul&&Goto :eof
(echo Set Link=WScript.CreateObject^("WScript.Shell"^).CreateShortcut^("%CLnk%"^)
 echo Link.TargetPath="%CTPath%"
 if defined CArgs echo Link.Arguments="%CArgs%"
 if defined CIcon if defined CIndex echo Link.IconLocation="%CIcon%,%CIndex%"
 if defined CDesc echo Link.Description="%CDesc%"
 if defined CWDir echo Link.WorkingDirectory="%CWDir%"
 if defined CWStyle echo Link.WindowStyle=%CWStyle% '1 默认窗口, 3 最大化, 7 最小化
 echo Link.Save) >"%TEMP%\SCut.vbs
(CScript 1>nul 2>nul&&CScript //nologo "%TEMP%\SCut.vbs" 1>nul 2>nul||"%TEMP%\SCut.vbs" 2>nul)&del /a/f/q "%TEMP%\SCut.vbs" 2>nul&Goto :eof

:Delay :: 延迟操作 <%1=Sec|延迟秒数> [%2=+|显示倒计时]。
if "%~2" == "+" (Set #=2) else Set #=1
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