#Requires -Version 5.1
<#
.SYNOPSIS
  Create a Desktop shortcut to CCS PDF Editor (Windows WebView2 shell).

.DESCRIPTION
  Package layout:
    <package>\CcsPdfEditor.exe
    <package>\tools\Create Desktop Shortcut.bat   (caller)
    <package>\tools\scripts\create-desktop-shortcut.ps1  (this file)

  Resolves package root as two levels up from this script.
#>
[CmdletBinding()]
param(
    [string]$ExePath = ""
)

$ErrorActionPreference = "Stop"

# tools\scripts -> tools -> package root
$PackageRoot = (Resolve-Path (Join-Path $PSScriptRoot "..\..")).Path

if (-not $ExePath) {
    $ExePath = Join-Path $PackageRoot "CcsPdfEditor.exe"
}
if (-not (Test-Path -LiteralPath $ExePath)) {
    throw "CcsPdfEditor.exe not found. Unzip the full Windows package first.`nLooked for: $ExePath"
}
$ExePath = (Resolve-Path -LiteralPath $ExePath).Path
$WorkDir = Split-Path -Parent $ExePath

$desktop = [Environment]::GetFolderPath("Desktop")
if (-not $desktop -or -not (Test-Path -LiteralPath $desktop)) {
    throw "Could not resolve Desktop folder for this user."
}

$lnkName = "CCS PDF Editor.lnk"
$lnkPath = Join-Path $desktop $lnkName

$shell = New-Object -ComObject WScript.Shell
$sc = $shell.CreateShortcut($lnkPath)
$sc.TargetPath = $ExePath
$sc.Arguments = ""
$sc.WorkingDirectory = $WorkDir
$sc.WindowStyle = 1
$sc.Description = "CCS PDF Editor (Windows desktop shell)"
$sc.IconLocation = "$ExePath,0"
$sc.Save()

$localLnk = Join-Path $WorkDir $lnkName
Copy-Item -LiteralPath $lnkPath -Destination $localLnk -Force

Write-Host ""
Write-Host "Desktop shortcut created:" -ForegroundColor Green
Write-Host "  $lnkPath"
Write-Host "Also saved in package folder:"
Write-Host "  $localLnk"
Write-Host ""
Write-Host "Tip: Run tools\Register File Types.bat if you also want .ccspdf double-click / PDF Open with."
