# 确定文件源
$sourceFiles = @("keybindings.json", "settings.json")

# 获取当前用户目录
$userProfile = [System.Environment]::GetFolderPath("UserProfile")

# 确定目标路径
switch ($env:OS) {
    "Windows_NT" {
        $targetPath = Join-Path -Path $userProfile -ChildPath "AppData\Roaming\Code\User"
    }
    "Unix" {
        if (Test-Path "$userProfile/.config/Code/User") {
            $targetPath = Join-Path -Path $userProfile -ChildPath ".config/Code/User"
        } else {
            Write-Host "目标目录 $userProfile/.config/Code/User 不存在"
            exit
        }
    }
    "Darwin" {
        $targetPath = Join-Path -Path $userProfile -ChildPath "Library/Application Support/Code/User"
    }
    default {
        Write-Host "不支持的操作系统"
        exit
    }
}

# 确保目标路径存在
if (-not (Test-Path -Path $targetPath)) {
    Write-Host "目标路径 $targetPath 不存在"
    exit
}

# 获取当前日期,不包括年份
$dateString = (Get-Date).ToString("MMddHHmmss")

# 遍历源文件并复制
foreach ($file in $sourceFiles) {
    $sourceFilePath = Join-Path -Path $PSScriptRoot -ChildPath $file
    $destinationFilePath = Join-Path -Path $targetPath -ChildPath $file
    
    # 打印源路径和目标路径
    Write-Host "源文件路径: $sourceFilePath"
    Write-Host "目标文件路径: $destinationFilePath"
	Write-Host ""
    
    if (Test-Path -Path $sourceFilePath) {
        if (Test-Path -Path $destinationFilePath) {
            # 如果目标路径下文件存在,重命名现有文件
            $fileBaseName = [System.IO.Path]::GetFileNameWithoutExtension($destinationFilePath)
			Write-Host "fileBaseName: $fileBaseName"
            $fileExtension = [System.IO.Path]::GetExtension($destinationFilePath)
			Write-Host "fileExtension: $fileExtension"
			$newFileName = "${fileBaseName}_${dateString}${fileExtension}"
			Write-Host "newFileName: $newFileName"
            $newFilePath = Join-Path -Path $targetPath -ChildPath $newFileName
            Rename-Item -Path $destinationFilePath -NewName $newFilePath
            Write-Host "已重命名原文件为: $newFilePath"
        }
        
        # 复制新文件
        Copy-Item -Path $sourceFilePath -Destination $destinationFilePath -Force
        Write-Host "已复制 $file 到 $targetPath"
    } else {
        Write-Host "源文件 $sourceFilePath 不存在"
    }
}