@echo off
fltmc >nul || mshta vbscript:CreateObject("Shell.Application").ShellExecute("%~dpnx0","%*",,"runas",1)(window.close) && exit

if not "%1"=="" (
    call :%1 %2 1>nul 2>nul
    goto :End
)

title WPS Install Manager
mode con: cols=70 lines=20
echo.
echo   [1] English
echo   [2] 简体中文
choice /c 123 >nul
if "%errorLevel%"=="1" (
    set "lang=en_US"
    rename "%~dp0mui\en_US\lang.conf.bak" lang.conf 1>nul 2>nul
    rename "%~dp0mui\zh_CN\lang.conf" lang.conf.bak 1>nul 2>nul
) else if "%errorLevel%"=="2" (
    set "lang=zh_CN"
    title WPS 安装管理器
    rename "%~dp0mui\zh_CN\lang.conf.bak" lang.conf 1>nul 2>nul
    rename "%~dp0mui\en_US\lang.conf" lang.conf.bak 1>nul 2>nul
)
cls
echo.
if "%lang%"=="en_US" (
    echo   [1] Install
    echo   [2] Uninstall
) else if "%lang%"=="zh_CN" (
    echo   [1] 安装
    echo   [2] 卸载
)
choice /c 12 >nul
cls
echo.
if "%errorLevel%"=="1" (
    if "%lang%"=="en_US" (
        echo   Installing, please wait...
    ) else if "%lang%"=="zh_CN" (
        echo   正在安装，请稍候...
    )
    call "%~dpnx0" Install
    cls
    echo.
    if "%lang%"=="en_US" (
        echo   Installation is complete
    ) else if "%lang%"=="zh_CN" (
        echo   安装完成
    )
) else if "%errorLevel%"=="2" (
    if "%lang%"=="en_US" (
        echo   Uninstalling, please wait...
    ) else if "%lang%"=="zh_CN" (
        echo   正在卸载，请稍候...
    )
    call "%~dpnx0" Uninstall
    cls
    echo.
    if "%lang%"=="en_US" (
        echo   Uninstallation is complete
    ) else if "%lang%"=="zh_CN" (
        echo   卸载完成
    )
)
timeout /t 2 >nul
exit

:Install
    :: Convert "\" in dir string to "\\"
    set "wpsDir=%~dp0"
    set "wpsDir=%wpsDir:\=\\%"
    (
        echo Windows Registry Editor Version 5.00
        call :RegSetAssoc wps  wps 1
        call :RegSetAssoc wpt  wps 2
        call :RegSetAssoc doc  wps 3
        call :RegSetAssoc docx wps 3
        call :RegSetAssoc docm wps 3
        call :RegSetAssoc dot  wps 4
        call :RegSetAssoc dotx wps 4
        call :RegSetAssoc dotm wps 4
        call :RegSetAssoc et   et  1
        call :RegSetAssoc ett  et  2
        call :RegSetAssoc xls  et  3
        call :RegSetAssoc xlsx et  3
        call :RegSetAssoc xlsm et  3
        call :RegSetAssoc csv  et  3
        call :RegSetAssoc xlt  et  4
        call :RegSetAssoc xltx et  4
        call :RegSetAssoc xltm et  4
        call :RegSetAssoc dps  wpp 1
        call :RegSetAssoc dpt  wpp 2
        call :RegSetAssoc ppt  wpp 3
        call :RegSetAssoc pptx wpp 3
        call :RegSetAssoc pptm wpp 3
        call :RegSetAssoc pps  wpp 3
        call :RegSetAssoc ppsx wpp 3
        call :RegSetAssoc ppsm wpp 3
        call :RegSetAssoc pot  wpp 4
        call :RegSetAssoc potx wpp 4
        call :RegSetAssoc potm wpp 4
        if exist "%~dp0mui\en_US\lang.conf" (
            call :RegAddNewFileItem docx "DOCX Document"
            call :RegAddNewFileItem xlsx "XLSX Sheet" true "%wpsDir%mui\\default\\templates\\newfile.xlsx"
            call :RegAddNewFileItem pptx "PPTX Presentation"
        ) else if exist "%~dp0mui\zh_CN\lang.conf" (
            call :RegAddNewFileItem docx "DOCX 文档"
            call :RegAddNewFileItem xlsx "XLSX 表格" true "%wpsDir%mui\\default\\templates\\newfile.xlsx"
            call :RegAddNewFileItem pptx "PPTX 幻灯片"
        )
    ) >"%Temp%\InstallWPS.reg"
    :: echo [HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\WPS]
    :: echo "DisplayName"="WPS"
    :: echo "DisplayIcon"="%wpsDir%wps.exe"
    :: echo "DisplayVersion"="10.1.0.7698"
    :: echo "UninstallString"="%wpsDir%%~nx0"
    reg import "%Temp%\InstallWPS.reg"
    del /f "%Temp%\InstallWPS.reg"
    assoc .=.
goto :eof

:Uninstall
    (
        echo Windows Registry Editor Version 5.00
        call :RegDelAssoc wps
        call :RegDelAssoc wpt
        call :RegDelAssoc doc
        call :RegDelAssoc docx
        call :RegDelAssoc docm
        call :RegDelAssoc dot
        call :RegDelAssoc dotx
        call :RegDelAssoc dotm
        call :RegDelAssoc et
        call :RegDelAssoc ett
        call :RegDelAssoc xls
        call :RegDelAssoc xlsx
        call :RegDelAssoc xlsm
        call :RegDelAssoc csv
        call :RegDelAssoc xlt
        call :RegDelAssoc xltx
        call :RegDelAssoc xltm
        call :RegDelAssoc dps
        call :RegDelAssoc dpt
        call :RegDelAssoc ppt
        call :RegDelAssoc pptx
        call :RegDelAssoc pptm
        call :RegDelAssoc pps
        call :RegDelAssoc ppsx
        call :RegDelAssoc ppsm
        call :RegDelAssoc pot
        call :RegDelAssoc potx
        call :RegDelAssoc potm
        call :RegDelAssoc potm
        call :RegDelAssoc potm
        echo [-HKEY_CURRENT_USER\SOFTWARE\Kingsoft]
    ) >"%Temp%\UninstallWPS.reg"
    :: echo [-HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\WPS]
    reg import "%Temp%\UninstallWPS.reg"
    del /f "%Temp%\UninstallWPS.reg"
    rmdir /s /q "%AppData%\Kingsoft"
    rmdir /s /q "%ProgramData%\Kingsoft\office6"
    assoc .=.
goto :eof

:RegDelAssoc :: <ext=%1>
    echo [-HKEY_CURRENT_USER\SOFTWARE\Classes\WPS.%1]
    echo [HKEY_CURRENT_USER\SOFTWARE\Classes\.%1]
    echo @=""
goto :eof

:RegSetAssoc :: <ext=%1> <exeName=%2> <iconIndex=%3>
    echo [HKEY_CURRENT_USER\SOFTWARE\Classes\WPS.%1\DefaultIcon]
    echo @="%wpsDir%%2.exe,%3"
    echo [HKEY_CURRENT_USER\SOFTWARE\Classes\WPS.%1\shell\open\command]
    echo @="\"%wpsDir%%2.exe\" \"%%1\""
    echo [HKEY_CURRENT_USER\SOFTWARE\Classes\.%1]
    echo @="WPS.%1"
goto :eof

:RegAddNewFileItem :: <ext=%1> <fileDesc=%2> [isRealFile=%3] [filePath=%4]
    echo [HKEY_CURRENT_USER\SOFTWARE\Classes\WPS.%1]
    echo @=%2
    echo [HKEY_CURRENT_USER\SOFTWARE\Classes\.%1]
    echo @="WPS.%1"
    echo [HKEY_CURRENT_USER\SOFTWARE\Classes\.%1\WPS.%1\ShellNew]
    if "%3"=="" (
        echo "NullFile"=""
    ) else (
        echo "FileName"=%4
    )
goto :eof

:End
