@echo off
setlocal enabledelayedexpansion

set CONFIG_USER_PATH=..\..\..\..\user
set CONFIG_PM51_USER_PATH=..\project\user
set CONFIG_FILE=..\..\..\..\out\.config

IF EXIST "%CONFIG_FILE%" (
    REM Do nothing
) ELSE (
    echo Please check .config file or run project_config.sh first.
    pause
    exit /b 1
)

call :find_config %CONFIG_FILE% CONFIG_VENDOR config_vendor_str
REM echo %config_vendor_str%

call :get_value "%config_vendor_str%" CONFIG_VENDOR
REM echo %config_val%

set CONFIG_VENDOR=%config_val%

REM echo %CONFIG_VENDOR%

call :find_config %CONFIG_FILE% CONFIG_PROJECT config_project_str
REM echo %config_project_str%

call :get_value "%config_project_str%" CONFIG_PROJECT
REM echo %config_val%

set CONFIG_PROJECT=%config_val%

REM echo %CONFIG_PROJECT%

copy %CONFIG_USER_PATH%\%CONFIG_VENDOR%\%CONFIG_PROJECT%\BoardConfig.h %CONFIG_PM51_USER_PATH%\BoardConfig.h
copy %CONFIG_USER_PATH%\%CONFIG_VENDOR%\%CONFIG_PROJECT%\ProjectConfig.h %CONFIG_PM51_USER_PATH%\ProjectConfig.h
copy %CONFIG_USER_PATH%\%CONFIG_VENDOR%\%CONFIG_PROJECT%\Keypad\Src\KeyboardConfig.c %CONFIG_PM51_USER_PATH%\KeyboardConfig.c
copy %CONFIG_USER_PATH%\%CONFIG_VENDOR%\%CONFIG_PROJECT%\Keypad\Inc\Keyboard.h %CONFIG_PM51_USER_PATH%\Keyboard.h
copy %CONFIG_USER_PATH%\%CONFIG_VENDOR%\%CONFIG_PROJECT%\Keypad\Inc\KeyboardConfig.h %CONFIG_PM51_USER_PATH%\KeyboardConfig.h
copy %CONFIG_USER_PATH%\%CONFIG_VENDOR%\%CONFIG_PROJECT%\EDID\hdmi\hv_edid_HdmiTMDS_NoVRR_2560X1440@144.h %CONFIG_PM51_USER_PATH%\hv_edid_HdmiTMDS_NoVRR_2560X1440@144.h
copy %CONFIG_USER_PATH%\%CONFIG_VENDOR%\%CONFIG_PROJECT%\Typec\TypecConfig.h %CONFIG_PM51_USER_PATH%\TypecConfig.h
copy %CONFIG_USER_PATH%\%CONFIG_VENDOR%\%CONFIG_PROJECT%\Pinshare\src\hv_cal_Pinshare_51 %CONFIG_PM51_USER_PATH%\Pinshare.c

goto :eof

:find_config
set filename=%1
set find_string=%2
set config=%3

for /f "tokens=*" %%a in ('type "%filename%" ^| find /i "%find_string%"') do (
    set %config%=%%a
)

goto :eof

:get_value
set "input=%~1"
set "key=%~2"

set "key_value="
for /f "tokens=1,* delims=?=" %%a in ("%input%") do (
    if "%%a"=="export %key% " set "key_value=%%b"
)
set "key_value=%key_value: =%"
set "config_val=!key_value!"
goto :eof
