@echo off
setlocal enabledelayedexpansion

:: 目标目录（当前用户的 Local\LTspice）
set "DEST=%LOCALAPPDATA%\LTspice"
set "DESKTOP=%USERPROFILE%\Desktop"
set "EXE_NAME=LTspice.exe"
set "TARGET=%DEST%\%EXE_NAME%"

:: 1. 创建目标目录（如果不存在）
if not exist "%DEST%" (
    mkdir "%DEST%"
    echo 已创建目录: %DEST%
)

:: 2. 解压 examples.zip
if exist "examples.zip" (
    echo 正在解压 examples.zip ...
    powershell -command "Expand-Archive -Path 'examples.zip' -DestinationPath '%DEST%' -Force"
    echo 解压完成。
) else (
    echo 警告: 未找到 examples.zip，请确保该文件与批处理在同一目录。
)

:: 3. 解压 lib.zip
if exist "lib.zip" (
    echo 正在解压 lib.zip ...
    powershell -command "Expand-Archive -Path 'lib.zip' -DestinationPath '%DEST%' -Force"
    echo 解压完成。
) else (
    echo 警告: 未找到 lib.zip，请确保该文件与批处理在同一目录。
)

:: 4. 创建桌面快捷方式（如果 LTspice.exe 存在）
if exist "%TARGET%" (
    echo 正在创建桌面快捷方式 ...
    powershell -command "$WshShell = New-Object -comObject WScript.Shell; $Shortcut = $WshShell.CreateShortcut('%DESKTOP%\LTspice.lnk'); $Shortcut.TargetPath = '%TARGET%'; $Shortcut.Save()"
    echo 快捷方式已创建: %DESKTOP%\LTspice.lnk
) else (
    echo 错误: 未找到 %TARGET%，请确认 LTspice.exe 已放置在 %DEST% 目录下。
    echo 将跳过快捷方式和文件关联。
    goto :end
)

:: 5. 关联 .asc、.asy、.net、.plt 等扩展名（用户级，无需管理员）
echo 正在关联 LTspice 文件类型（当前用户）...
powershell -command "
$exts = @('.asc', '.asy', '.net', '.plt');
$progId = 'LTspice.Document';
$cmd = '\"%TARGET%\" \"%1\"';
foreach ($ext in $exts) {
    # 设置扩展名默认 ProgID
    $key = New-Item -Path \"HKCU:\Software\Classes\$ext\" -Force;
    $key.SetValue('', $progId);
    # 设置 ProgID 下的打开命令
    $cmdKey = New-Item -Path \"HKCU:\Software\Classes\$progId\shell\open\command\" -Force;
    $cmdKey.SetValue('', $cmd);
}
# 可选：设置默认图标（使用 LTspice.exe 的第一个图标）
$iconKey = New-Item -Path \"HKCU:\Software\Classes\$progId\DefaultIcon\" -Force;
$iconKey.SetValue('', '\"%TARGET%\",0');
"
echo 文件关联完成。

:end
echo 操作完成。
pause