# Creates gold "Bayan GlassCaption" shortcuts in THREE places: Desktop,
# Start Menu, and the app folder itself. Called by "INSTALL - CLICK ME
# FIRST.bat". Safe to re-run (overwrites). Lives in resources\; app root
# is one level up. Shortcuts launch pythonw.exe DIRECTLY - no .vbs, no
# WScript, nothing for SmartScreen to distrust.
$ErrorActionPreference = 'Stop'
$res  = $PSScriptRoot
$base = Split-Path -Parent $res

$pyw = Join-Path $res 'python\pythonw.exe'
if (-not (Test-Path $pyw)) { $pyw = Join-Path $res 'venv\Scripts\pythonw.exe' }
if (-not (Test-Path $pyw)) { throw 'pythonw.exe not found under resources\' }
$mainPy = Join-Path $res 'app\main.py'
$icon   = Join-Path $res 'branding\icon.ico'

$ws = New-Object -ComObject WScript.Shell
$made = @()
$paths = @()
$spots = @(
  @{ dir = [Environment]::GetFolderPath('Desktop'); label = 'Desktop' },
  @{ dir = (Join-Path $env:APPDATA 'Microsoft\Windows\Start Menu\Programs'); label = 'Start Menu' },
  @{ dir = $base; label = 'this folder' }
)
foreach ($spot in $spots) {
  if (-not (Test-Path $spot.dir)) { continue }
  $lnk = Join-Path $spot.dir 'Bayan GlassCaption.lnk'
  $s = $ws.CreateShortcut($lnk)
  $s.TargetPath = $pyw
  $s.Arguments = '"' + $mainPy + '"'
  $s.WorkingDirectory = $base
  if (Test-Path $icon) { $s.IconLocation = "$icon,0" }
  $s.Description = 'Bayan GlassCaption - live translated captions'
  $s.Save()
  $made += $spot.label
  $paths += $lnk
}
if ($made.Count -eq 0) { throw 'no shortcut location was writable' }

# Stamp each shortcut with the app's taskbar identity (AppUserModelID).
# The app sets the same ID at startup, so a PINNED shortcut and the RUNNING
# window merge into ONE taskbar button showing the gold icon - without this,
# pinning shows the generic Python icon (David's session-6 finding).
# Best-effort: if this fails, shortcuts still work, just without merging.
try {
  Add-Type -TypeDefinition @'
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
namespace BGC {
  [StructLayout(LayoutKind.Sequential, Pack = 4)]
  public struct PROPERTYKEY { public Guid fmtid; public uint pid; }
  [StructLayout(LayoutKind.Explicit)]
  public struct PROPVARIANT {
    [FieldOffset(0)] public ushort vt;
    [FieldOffset(8)] public IntPtr pv;
  }
  [ComImport, Guid("886D8EEB-8CF2-4446-8D02-CDBA1DBDCF99"),
   InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  public interface IPropertyStore {
    int GetCount(out uint cProps);
    int GetAt(uint iProp, out PROPERTYKEY pkey);
    int GetValue(ref PROPERTYKEY key, out PROPVARIANT pv);
    int SetValue(ref PROPERTYKEY key, ref PROPVARIANT pv);
    int Commit();
  }
  [ComImport, Guid("00021401-0000-0000-C000-000000000046")]
  public class CShellLink { }
  public static class LnkAumid {
    [DllImport("Ole32.dll")] static extern int PropVariantClear(ref PROPVARIANT pvar);
    public static void Set(string lnkPath, string aumid) {
      var link = (IPersistFile)new CShellLink();
      link.Load(lnkPath, 0x2 /* STGM_READWRITE */);
      var store = (IPropertyStore)(object)link;
      var key = new PROPERTYKEY { fmtid = new Guid("9F4C2855-9F79-4B39-A8D0-E1D42DE1D5F3"), pid = 5 };
      var pv = new PROPVARIANT { vt = 31, pv = Marshal.StringToCoTaskMemUni(aumid) };
      int hr = store.SetValue(ref key, ref pv);
      if (hr == 0) { store.Commit(); link.Save(lnkPath, true); }
      PropVariantClear(ref pv);
    }
  }
}
'@
  foreach ($p in $paths) { [BGC.LnkAumid]::Set($p, 'AVLabsPro.BayanGlassCaption') }
} catch { }

Write-Output ('                 Gold icon created: ' + ($made -join ', ') + '.')
