$ErrorActionPreference = 'SilentlyContinue'
$SID = 'S-1-12-1-4150293861-1139320743-1956584882-216650436'  # kristinsteen
$wp2x64 = 'C:\Program Files\Datto\Workplace2\SmartBadge\DattoSmartBadgeShim_x64.dll'
$wp2x86 = 'C:\Program Files\Datto\Workplace2\SmartBadge\DattoSmartBadgeShim_x86.dll'

# --- Guard: Excel must be closed (it rewrites per-user add-in state on exit) ---
$xl = Get-Process EXCEL -ErrorAction SilentlyContinue
if ($xl) { Write-Output "[ABORT] EXCEL.EXE is running (pid $($xl.Id)). Close Excel before running the per-user fix."; exit 2 }

Write-Output "=== MACHINE-WIDE: remove v8 leftovers (match EVO-X1) ==="
# Drop the Workplace Desktop CLSID {2B96EDC1} (x64 + WOW64)
foreach ($k in @(
  'HKLM:\Software\Classes\CLSID\{2B96EDC1-FDF3-47E1-B177-F205E7B98DF4}',
  'HKLM:\Software\WOW6432Node\Classes\CLSID\{2B96EDC1-FDF3-47E1-B177-F205E7B98DF4}')) {
  if (Test-Path $k) { Remove-Item $k -Recurse -Force; Write-Output "  removed $k" } else { Write-Output "  absent  $k" }
}
# Drop the non-_CC Datto.SmartBadgeShim add-in keys (EVO-X1 only has _CC)
foreach ($k in @(
  'HKLM:\Software\Microsoft\Office\Excel\Addins\Datto.SmartBadgeShim',
  'HKLM:\Software\WOW6432Node\Microsoft\Office\Excel\Addins\Datto.SmartBadgeShim',
  'HKLM:\Software\Microsoft\Office\Word\Addins\Datto.SmartBadgeShim',
  'HKLM:\Software\Microsoft\Office\PowerPoint\Addins\Datto.SmartBadgeShim')) {
  if (Test-Path $k) { Remove-Item $k -Recurse -Force; Write-Output "  removed $k" } else { Write-Output "  absent  $k" }
}

Write-Output ""
Write-Output "=== MACHINE-WIDE: verify _CC CLSID -> Workplace2 DLLs ==="
$cc = '{3C639243-95A2-400D-B4B4-4384DA7F61D3}'
foreach ($pair in @(@("HKLM:\Software\Classes\CLSID\$cc\InprocServer32",$wp2x64), @("HKLM:\Software\WOW6432Node\Classes\CLSID\$cc\InprocServer32",$wp2x86))) {
  $path = $pair[0]; $want = $pair[1]
  $cur = (Get-Item $path -ErrorAction SilentlyContinue)
  $val = if ($cur) { $cur.GetValue('') } else { $null }
  if ($val -ne $want) {
    if (-not (Test-Path $path)) { New-Item $path -Force | Out-Null }
    Set-ItemProperty $path -Name '(default)' -Value $want
    Set-ItemProperty $path -Name 'ThreadingModel' -Value 'Apartment'
    Write-Output "  set $path -> $want"
  } else { Write-Output "  ok  $path -> $val" }
}

Write-Output ""
Write-Output "=== PER-USER ($SID): clear stuck disabled state ==="
$xlAddins = "Registry::HKEY_USERS\$SID\Software\Microsoft\Office\Excel\Addins"
# Remove non-_CC leftover in user hive (match EVO-X1 = no per-user Datto entries)
if (Test-Path "$xlAddins\Datto.SmartBadgeShim") { Remove-Item "$xlAddins\Datto.SmartBadgeShim" -Recurse -Force; Write-Output "  removed HKCU Datto.SmartBadgeShim" }
# Reset _CC per-user override to 3 (Excel had set it to 2 = disabled)
if (Test-Path "$xlAddins\Datto.SmartBadgeShim_CC") {
  Set-ItemProperty "$xlAddins\Datto.SmartBadgeShim_CC" -Name 'LoadBehavior' -Value 3 -Type DWord
  Write-Output "  set HKCU Datto.SmartBadgeShim_CC LoadBehavior=3"
}
# Clear Excel Resiliency DisabledItems + crash records
$res = "Registry::HKEY_USERS\$SID\Software\Microsoft\Office\16.0\Excel\Resiliency"
foreach ($sub in @('DisabledItems','CrashingAddinList','DocumentRecovery')) {
  if (Test-Path "$res\$sub") {
    $i = Get-Item "$res\$sub"
    $i.GetValueNames() | ForEach-Object { Remove-ItemProperty "$res\$sub" -Name $_ -Force }
    Write-Output "  cleared values under Resiliency\$sub"
  }
}
# Keep add-in protected from auto-disable
$dnd = "$res\DoNotDisableAddinList"
if (-not (Test-Path $dnd)) { New-Item $dnd -Force | Out-Null }
Set-ItemProperty $dnd -Name 'Datto.SmartBadgeShim_CC' -Value 1 -Type DWord
Write-Output "  ensured DoNotDisableAddinList Datto.SmartBadgeShim_CC=1"
Write-Output "=== FIX COMPLETE ==="
