@echo off
setlocal EnableDelayedExpansion

REM 获取当前批处理文件所在的目录
set SCRIPT_DIR=%~dp0

REM 查找符合条件的文件（以 隐写者 开头，后缀为 .exe）
for %%f in ("%SCRIPT_DIR%隐写者*.exe") do set PROGRAM_PATH=%%f

REM 设置 Hash Modifier 程序的路径
set HASH_MODIFIER_PATH=%SCRIPT_DIR%tools\hash_modifier.exe
set HASH_MODIFIER_ICON=%SCRIPT_DIR%tools\modules\favicon_hash_modifier.ico
set POWERSHELL_SCRIPT_PATH=%SCRIPT_DIR%tools\run_hash_modifier.ps1
REM 设置 PowerShell 启动脚本路径
set LAUNCH_SCRIPT_PATH=%SCRIPT_DIR%tools\launch_from_selection.ps1

REM 如果没有找到符合条件的文件，则退出
if not defined PROGRAM_PATH (
    echo No matching 隐写者*.exe file found!
    timeout 3
    endlocal
    exit /b 1
)

if not exist "%HASH_MODIFIER_PATH%" (
    echo Hash Modifier executable not found at %HASH_MODIFIER_PATH%
    timeout 3
    endlocal
    exit /b 1
)

REM 创建 PowerShell 脚本
echo Creating PowerShell script...
(
    echo param([string]$FilePath^)
    echo.
    echo $hashModifierPath = Join-Path $PSScriptRoot "hash_modifier.exe"
    echo.
    echo function Show-Notification {
    echo     param ([string]$Title, [string]$Message^)
    echo     Add-Type -AssemblyName System.Windows.Forms
    echo     $global:balmsg = New-Object System.Windows.Forms.NotifyIcon
    echo     $path = (Get-Process -id $pid^).Path
    echo     $balmsg.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path^)
    echo     $balmsg.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Info
    echo     $balmsg.BalloonTipText = $Message
    echo     $balmsg.BalloonTipTitle = $Title
    echo     $balmsg.Visible = $true
    echo     $balmsg.ShowBalloonTip(5000^)
    echo }
    echo.
    echo try {
    echo     $logPath = Join-Path $PSScriptRoot "hash_modifier_log.txt"
    echo     $output = ^& $hashModifierPath $FilePath 2^>^&1 ^| Out-String
    echo     $exitCode = $LASTEXITCODE
    echo     $log = "执行时间: $(Get-Date)`n"
    echo     $log += "命令: $hashModifierPath $FilePath`n"
    echo     $log += "退出代码: $exitCode`n"
    echo     $log += "输出:`n$output`n"
    echo     $log += "--------------------------------------------------`n"
    echo     $log ^| Out-File -Append -FilePath $logPath
    echo     if ($exitCode -eq 0 -and $output -match "Modified file hash^|Hash modification completed"^) {
    echo         Show-Notification -Title "哈希值修改" -Message "操作已成功完成。文件哈希值已修改。"
    echo     } else {
    echo         Show-Notification -Title "哈希值修改" -Message "操作可能未成功完成。请检查日志文件了解详情。"
    echo     }
    echo } catch {
    echo     $errorMessage = $_.Exception.Message
    echo     Show-Notification -Title "哈希值修改" -Message "发生错误。详细信息已记录到日志文件。"
    echo     "错误: $errorMessage" ^| Out-File -Append -FilePath $logPath
    echo }
) > "%POWERSHELL_SCRIPT_PATH%"

echo PowerShell script created at %POWERSHELL_SCRIPT_PATH%

REM 将路径中的反斜杠替换为双反斜杠
set PROGRAM_PATH=%PROGRAM_PATH:\=\\%
set HASH_MODIFIER_PATH=%HASH_MODIFIER_PATH:\=\\%
set HASH_MODIFIER_ICON=%HASH_MODIFIER_ICON:\=\\%
set POWERSHELL_SCRIPT_PATH=%POWERSHELL_SCRIPT_PATH:\=\\%
set LAUNCH_SCRIPT_PATH=%LAUNCH_SCRIPT_PATH:\=\\%

REM 创建临时注册表文件
set TEMP_REG_FILE=%TEMP%\install_context_menu.reg

echo Windows Registry Editor Version 5.00 > "%TEMP_REG_FILE%"
echo. >> "%TEMP_REG_FILE%"

REM ========== 文件右键菜单 - 包含两个解除隐写选项 ==========
echo [HKEY_CLASSES_ROOT\*\shell\Steganographier] >> "%TEMP_REG_FILE%"
echo "MUIVerb"="Steganographier" >> "%TEMP_REG_FILE%"
echo "SubCommands"="hideMp4;hideMkv;revealFileCLI;revealFileGUI;modifyHash;openHashModifier" >> "%TEMP_REG_FILE%"
echo "Icon"="%PROGRAM_PATH%" >> "%TEMP_REG_FILE%"
echo. >> "%TEMP_REG_FILE%"

REM ========== 文件夹右键菜单 - 包含 revealDir（批量处理） ==========
echo [HKEY_CLASSES_ROOT\Directory\shell\Steganographier] >> "%TEMP_REG_FILE%"
echo "MUIVerb"="Steganographier" >> "%TEMP_REG_FILE%"
echo "SubCommands"="hideMp4;hideMkv;revealDir;modifyHash;openHashModifier" >> "%TEMP_REG_FILE%"
echo "Icon"="%PROGRAM_PATH%" >> "%TEMP_REG_FILE%"
echo. >> "%TEMP_REG_FILE%"

REM ========== 子命令定义 ==========

REM 隐写为MP4
echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\hideMp4] >> "%TEMP_REG_FILE%"
echo @="隐写为MP4文件(无密码)" >> "%TEMP_REG_FILE%"
echo. >> "%TEMP_REG_FILE%"

echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\hideMp4\command] >> "%TEMP_REG_FILE%"
echo @="\"%PROGRAM_PATH%\" -i \"%%1\" -o \"%%1_hidden.mp4\" -t mp4" >> "%TEMP_REG_FILE%"
echo. >> "%TEMP_REG_FILE%"

REM 隐写为MKV
echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\hideMkv] >> "%TEMP_REG_FILE%"
echo @="隐写为MKV文件(无密码)" >> "%TEMP_REG_FILE%"
echo. >> "%TEMP_REG_FILE%"

echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\hideMkv\command] >> "%TEMP_REG_FILE%"
echo @="\"%PROGRAM_PATH%\" -i \"%%1\" -o \"%%1_hidden.mkv\" -t mkv" >> "%TEMP_REG_FILE%"
echo. >> "%TEMP_REG_FILE%"

REM 解除单文件隐写（CLI模式 - 传统方式，根据密码本）
echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\revealFileCLI] >> "%TEMP_REG_FILE%"
echo @="解除隐写(根据密码本)" >> "%TEMP_REG_FILE%"
echo. >> "%TEMP_REG_FILE%"

echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\revealFileCLI\command] >> "%TEMP_REG_FILE%"
echo @="\"%PROGRAM_PATH%\" -i \"%%1\" -r" >> "%TEMP_REG_FILE%"
echo. >> "%TEMP_REG_FILE%"

REM 解除单个或多个文件的隐写（GUI模式 - 打开GUI并自动填充）
echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\revealFileGUI] >> "%TEMP_REG_FILE%"
echo @="批量解除隐写(打开GUI)" >> "%TEMP_REG_FILE%"
echo. >> "%TEMP_REG_FILE%"

echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\revealFileGUI\command] >> "%TEMP_REG_FILE%"
REM 调用 PowerShell 脚本传递选中的文件
echo @="powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -File \"%LAUNCH_SCRIPT_PATH%\"" >> "%TEMP_REG_FILE%"
echo. >> "%TEMP_REG_FILE%"

REM 批量解除文件夹下所有隐写（用于文件夹右键菜单）
echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\revealDir] >> "%TEMP_REG_FILE%"
echo @="解除此文件夹下所有隐写(批量)" >> "%TEMP_REG_FILE%"
echo. >> "%TEMP_REG_FILE%"

echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\revealDir\command] >> "%TEMP_REG_FILE%"
echo @="\"%PROGRAM_PATH%\" -i \"%%1\" -rd -rdgui" >> "%TEMP_REG_FILE%"
echo. >> "%TEMP_REG_FILE%"

REM 修改哈希值
echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\modifyHash] >> "%TEMP_REG_FILE%"
echo @="修改文件/文件夹哈希值" >> "%TEMP_REG_FILE%"
echo "Icon"="%HASH_MODIFIER_ICON%" >> "%TEMP_REG_FILE%"
echo. >> "%TEMP_REG_FILE%"

echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\modifyHash\command] >> "%TEMP_REG_FILE%"
echo @="powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -File \"%POWERSHELL_SCRIPT_PATH%\" \"%%1\"" >> "%TEMP_REG_FILE%"
echo. >> "%TEMP_REG_FILE%"

REM 打开哈希修改器GUI
echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\openHashModifier] >> "%TEMP_REG_FILE%"
echo @="打开哈希修改器GUI" >> "%TEMP_REG_FILE%"
echo "Icon"="%HASH_MODIFIER_ICON%" >> "%TEMP_REG_FILE%"
echo. >> "%TEMP_REG_FILE%"

echo [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\openHashModifier\command] >> "%TEMP_REG_FILE%"
echo @="\"%HASH_MODIFIER_PATH%\" --gui" >> "%TEMP_REG_FILE%"
echo. >> "%TEMP_REG_FILE%"

REM ========== 文件夹空白处右键 - 打开隐写者GUI ==========
echo [HKEY_CLASSES_ROOT\Directory\Background\shell\openSteganographier] >> "%TEMP_REG_FILE%"
echo @="打开隐写者GUI" >> "%TEMP_REG_FILE%"
echo "Icon"="%PROGRAM_PATH%" >> "%TEMP_REG_FILE%"
echo. >> "%TEMP_REG_FILE%"

echo [HKEY_CLASSES_ROOT\Directory\Background\shell\openSteganographier\command] >> "%TEMP_REG_FILE%"
echo @="\"%PROGRAM_PATH%\"" >> "%TEMP_REG_FILE%"
echo. >> "%TEMP_REG_FILE%"

REM 导入注册表
regedit /s "%TEMP_REG_FILE%"

REM 删除临时注册表文件
del "%TEMP_REG_FILE%"

REM 
REM ---------------------------------------------
REM --- 写入卸载程序信息到注册表 ---
REM ---------------------------------------------

REM %~dp0 是本脚本所在的路径, %~dp0%~n0.bat 就是本脚本的完整路径

REM 移除末尾的反斜杠
set "INSTALL_PATH=%~dp0"
set "INSTALL_PATH_CLEAN=%INSTALL_PATH:~0,-1%"

REM 查找同目录下的卸载脚本（支持文件名如 03-卸载隐写者v1.3.5.bat）
set "UNINSTALL_FILE="
for %%u in ("%~dp003-卸载隐写者*.bat") do set "UNINSTALL_FILE=%%~nxu"

REM 如果未找到，使用默认文件名
if not defined UNINSTALL_FILE (
    set "UNINSTALL_FILE=03-卸载隐写者.bat"
)

REM 提取文件名（不含扩展名）并尝试从中解析版本号（v后面的部分）
set "BASE_NAME=%UNINSTALL_FILE%"
setlocal EnableDelayedExpansion
set "BASE_NAME=!BASE_NAME:~0,-4!"  REM 去掉 .bat
REM 默认版本
set "VERSION=1.3.4"
for /f "tokens=2 delims=vV" %%a in ("!BASE_NAME!") do set "VERSION=%%a"
endlocal & set "VERSION=%VERSION%"

REM 定义卸载注册表项的路径
set "UNINSTALL_KEY=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\SteganographierGUI"

REM 开始写入信息 (Geek Uninstaller 会读取这些信息)
reg add %UNINSTALL_KEY% /v DisplayName /t REG_SZ /d "隐写者 (SteganographierGUI)" /f
reg add %UNINSTALL_KEY% /v DisplayVersion /t REG_SZ /d "%VERSION%" /f
reg add %UNINSTALL_KEY% /v Publisher /t REG_SZ /d "层林尽染" /f
reg add %UNINSTALL_KEY% /v InstallLocation /t REG_SZ /d "%INSTALL_PATH_CLEAN%" /f

REM 关键：指定卸载命令（使用已检测到的卸载脚本文件名）
reg add %UNINSTALL_KEY% /v UninstallString /t REG_SZ /d "\"%INSTALL_PATH_CLEAN%\%UNINSTALL_FILE%\"" /f

REM 关键：指定卸载图标
reg add %UNINSTALL_KEY% /v DisplayIcon /t REG_SZ /d "\"%INSTALL_PATH_CLEAN%\modules\favicon.ico\"" /f

REM 其他辅助信息
reg add %UNINSTALL_KEY% /v EstimatedSize /t REG_DWORD /d 97000 /f
reg add %UNINSTALL_KEY% /v NoModify /t REG_DWORD /d 1 /f
reg add %UNINSTALL_KEY% /v NoRepair /t REG_DWORD /d 1 /f

echo 卸载信息已注册到系统。

echo Installation complete! 
endlocal