@echo off
setlocal

set work_dir=.
set base_name=all_summary_jp
set perform_replace=1
set "tts=voicevox"
set "monologue=0"
set "voices=四国めたん;ずんだもん;春日部つむぎ;満別花丸;青山龍星"
set temp_dir=tmp_jp

set "input_script=%work_dir%\%base_name%.txt"
set "script_base=%work_dir%\%base_name%-converted"

rem perform_replace が空なら script_path を input_script に置き換える
if "%perform_replace%"=="" (
    set "script_base=%base_name%"
)

set "script_path=%script_base%.md"
set "output_mp3=%work_dir%\%script_base%.mp3"

set tkprog_X_path=D:\git\tkProg\tkprog_COE
set replace_path1=D:\doc1.current\replace.ini
set replace_path2=D:\git\tkProg\tkprog_COE\converter\replace.ini

set tinterval=0.5
set speak_rate=150
set fspeak_rate=1.0
set "aquestalk_path=D:\App\App\Multimedia\aquestalkplayer\AquesTalkPlayer.exe"
set voicevox_path=D:/App/App/Multimedia/VOICEVOX
set start_engine_py=%tkprog_X_path%\ai\start_voicevox_engine.py
set endpoint=http://127.0.0.1:50021


echo.
echo ============================================================
echo 0. 必要であればVOICEVOXサーバを起動
echo ============================================================
if "%tts%"=="voicevox" (
:  start cmd /K %voicevox_path%\vv-engine\run.exe
  python "%start_engine_py%" --engine-path %voicevox_path%\vv-engine\run.exe
)

echo.
echo ============================================================
echo 1. 読み上げ原稿の文字列置換
echo ============================================================

if not "%perform_replace%"=="" (
  call :DELETE_OUTPUT "%script_path%"
  if errorlevel 1 goto ERROR

  python "%tkprog_X_path%\converter\replace.py" ^
    -i "%input_script%" ^
    -o "%script_path%" ^
    -r "%replace_path2%" ^
    -s "%replace_path1%" ^
    --pause 0

  if errorlevel 1 (
    echo [ERROR] replace.py の実行に失敗しました。
    goto ERROR
  )

  call :CHECK_OUTPUT "%script_path%"
  if errorlevel 1 goto ERROR
)

echo.
echo ============================================================
echo 2. 音声ファイルの作成
echo ============================================================

call :DELETE_OUTPUT "%output_pptx%"
if errorlevel 1 goto ERROR

python "%tkprog_X_path%\ai\speak.py" ^
    --tts="%tts%" ^
    -i "%script_path%" ^
    -o "%output_mp3%" ^
    --infile="%script_path%" ^
    --outfile="%output_mp3%" ^
    --monologue=%monologue% ^
    --voices="%voices%" ^
    --replace="" ^
    --tinterval=%tinterval% ^
    --endpoint=%endpoint% ^
    --fspeak_rate=%fspeak_rate% ^
    --fspeak_pitch=0.0 ^
    --aquestalk_path="%aquestalk_path%" ^
    --temp_dir=%temp_dir% ^
    --pause=0

if errorlevel 1 (
    echo [ERROR] speak.py の実行に失敗しました。
    goto ERROR
)

call :CHECK_OUTPUT "%output_mp3%"
if errorlevel 1 goto ERROR

echo.
echo ============================================================
echo すべての処理が正常に完了しました。
echo ============================================================
echo MP3: "%output_mp3%"
echo.

endlocal
exit /b 0


:DELETE_OUTPUT
set "target_file=%~1"

if exist "%target_file%" (
    echo [INFO] 既存ファイルを削除します:
    echo        "%target_file%"

    del /f /q "%target_file%"

    if exist "%target_file%" (
        echo [ERROR] ファイルを削除できませんでした:
        echo         "%target_file%"
        exit /b 1
    )
)

exit /b 0


:CHECK_OUTPUT
set "target_file=%~1"

if not exist "%target_file%" (
    echo [ERROR] 出力ファイルが作成されていません:
    echo         "%target_file%"
    exit /b 1
)

for %%F in ("%target_file%") do (
    if %%~zF LEQ 0 (
        echo [ERROR] 出力ファイルのサイズが0です:
        echo         "%target_file%"
        exit /b 1
    )

    echo [OK] 出力ファイルを確認しました:
    echo      "%target_file%"
    echo      サイズ: %%~zF bytes
)

exit /b 0


:ERROR
echo.
echo ============================================================
echo 処理を中止しました。
echo ============================================================
echo.

endlocal
exit /b 1
