$ErrorActionPreference = "Stop"

$installDirectory = Join-Path $env:LOCALAPPDATA "Programs\Index"
$sourceDirectory = $PSScriptRoot
$startMenuDirectory = Join-Path $env:APPDATA "Microsoft\Windows\Start Menu\Programs"
$desktopDirectory = [Environment]::GetFolderPath("Desktop")

New-Item -ItemType Directory -Path $installDirectory -Force | Out-Null
Get-ChildItem -LiteralPath $sourceDirectory -Force |
  Where-Object { $_.Name -notin @("Install Index.cmd", "Install-Index.ps1") } |
  Copy-Item -Destination $installDirectory -Recurse -Force

$executable = Join-Path $installDirectory "Index.exe"
$shell = New-Object -ComObject WScript.Shell
foreach ($shortcutPath in @(
  (Join-Path $startMenuDirectory "Index.lnk"),
  (Join-Path $desktopDirectory "Index.lnk")
)) {
  $shortcut = $shell.CreateShortcut($shortcutPath)
  $shortcut.TargetPath = $executable
  $shortcut.WorkingDirectory = $installDirectory
  $shortcut.IconLocation = "$executable,0"
  $shortcut.Description = "Save anything. Think with everything."
  $shortcut.Save()
}

Start-Process -FilePath $executable
Write-Host "Index was installed for this Windows account."
