@echo off
setlocal EnableExtensions

set "OA_EXE="

for %%P in ("%ProgramFiles(x86)%" "%ProgramFiles%") do (
  if not "%%~P"=="" (
    for /d %%D in ("%%~P\*OA*") do (
      if exist "%%~fD\Login.exe" (
        if not defined OA_EXE set "OA_EXE=%%~fD\Login.exe"
      )
    )
  )
)

if not defined OA_EXE (
  echo Cannot find OA client Login.exe under Program Files.
  echo Please install the Maoming OA PC client first, then run this file again.
  pause
  exit /b 1
)

reg add "HKCU\Software\Classes\maoming-oa" /ve /d "URL:Maoming OA Client" /f >nul
reg add "HKCU\Software\Classes\maoming-oa" /v "URL Protocol" /d "" /f >nul
reg add "HKCU\Software\Classes\maoming-oa\DefaultIcon" /ve /d "\"%OA_EXE%\",0" /f >nul
reg add "HKCU\Software\Classes\maoming-oa\shell\open\command" /ve /d "\"%OA_EXE%\" \"%%1\"" /f >nul

if errorlevel 1 (
  echo Protocol registration failed.
  pause
  exit /b 1
)

echo Registered successfully.
echo OA client: %OA_EXE%
echo You can now open maoming-oa://open from the OA web page.
pause