@echo off
setlocal enabledelayedexpansion
title Installer Mod Bahasa Indonesia - Danganronpa
color 0A

:: 1. Definisikan Direktori Sumber
set "SOURCE_DIR=%~dp0File_Mod_by_ArenaModID"

if not exist "%SOURCE_DIR%" (
    echo [ERROR] Folder sumber tidak ditemukan!
    echo Pastikan folder "File_Mod_by_ArenaModID" berada di tempat yang sama dengan script ini.
    echo.
    pause
    exit /b
)

:: 2. Pencarian Cerdas Direktori Tujuan (BUG FIXED)
echo Mencari direktori tujuan, mohon tunggu...
set "TARGET_DIR="

:: Skenario A: Jika user melakukan "Extract Here" (Folder DR1 langsung di sebelah script)
if exist "%~dp0DR1 (PC) [MANUAL MODE]\EXTRACTED\WAD\dr1_data_us\Dr1\data\us\script" (
    set "TARGET_DIR=%~dp0DR1 (PC) [MANUAL MODE]\EXTRACTED\WAD\dr1_data_us\Dr1\data\us\script"
    goto :TargetFound
)

:: Skenario B: Jika user melakukan "Extract to tools" (Folder DR1 ada di dalam sub-folder)
for /d %%D in ("%~dp0*") do (
    if exist "%%D\DR1 (PC) [MANUAL MODE]\EXTRACTED\WAD\dr1_data_us\Dr1\data\us\script" (
        set "TARGET_DIR=%%D\DR1 (PC) [MANUAL MODE]\EXTRACTED\WAD\dr1_data_us\Dr1\data\us\script"
        goto :TargetFound
    )
)

:TargetFound
if "%TARGET_DIR%"=="" (
    cls
    color 0C
    echo [ERROR] Direktori tujuan tidak ditemukan!
    echo Pastikan file "tools.zip" sudah di-extract dan proses ekstrak telah dijalankan.
    echo.
    pause
    exit /b
)

:: 3. Hitung Total File Secara Cepat
for /f %%A in ('dir /s /b /a-d "%SOURCE_DIR%" 2^>nul ^| find /c /v ""') do set TOTAL_FILES=%%A

:: 4. Bersihkan Layar untuk Tampilan Utama
cls
color 0A
echo =========================================================
echo  INSTALLER MOD BAHASA INDONESIA - TRIGGER HAPPY HAVOC
echo =========================================================
echo.
echo  Target    : %TARGET_DIR%
echo  Total File: %TOTAL_FILES% file mod
echo.
echo  Proses copy sedang berjalan. Mohon tidak menutup jendela ini...
echo.

:: 5. Eksekusi Copy Menggunakan PowerShell (Flattening: Hanya File, Tanpa Folder)
powershell -NoProfile -Command "$src='%SOURCE_DIR%'; $dst='%TARGET_DIR%'; $files = Get-ChildItem -Path $src -Recurse -File; $total = $files.Count; $i = 0; foreach ($file in $files) { $i++; $destFile = Join-Path $dst $file.Name; Copy-Item -Path $file.FullName -Destination $destFile -Force; Write-Progress -Activity 'Menyalin File Mod...' -Status ('Selesai: {0}%% ({1}/{2} file)' -f [math]::Floor(($i/$total)*100), $i, $total) -PercentComplete (($i/$total)*100) }"

:: 6. Selesai
echo.
echo =========================================================
echo  100%% COMPLETED!
echo  Semua file mod berhasil disalin dan ditimpa ke dalam folder script.
echo =========================================================
echo.
echo Tekan tombol apa saja untuk keluar...
pause >nul