1param([Parameter(Mandatory=$true)][string]$Root, [Parameter(Mandatory=$true)][string]$CloneHost)
2Set-StrictMode -Version Latest
3$ErrorActionPreference = 'Stop'
4& "$PSScriptRoot\cold-current.ps1" -Root $Root -CloneHost $CloneHost
5$app = New-Object -ComObject OneNote.Application
6$notebook = ''
7try {
8 $app.OpenHierarchy((Join-Path $Root 'notebook'), '', [ref]$notebook, 0)
9 $section = ''
10 $app.OpenHierarchy('synthetic.one', $notebook, [ref]$section, 0)
11 $deadline = [DateTime]::UtcNow.AddSeconds(60)
12 $matches = @()
13 $content = ''
14 do {
15 $hierarchy = ''
16 $app.GetHierarchy($section, 4, [ref]$hierarchy, 1)
17 [xml]$tree = $hierarchy
18 $pages = @($tree.SelectNodes('//*[local-name()="Page"]') | Where-Object { $_.GetAttribute('name') -eq 'Move leaf down' })
19 if ($pages.Count -eq 1) {
20 $app.GetPageContent($pages[0].GetAttribute('ID'), [ref]$content, 3, 1)
21 [xml]$page = $content
22 $matches = @($page.SelectNodes('//*[local-name()="T"]') | Where-Object { $_.InnerText -like '*>Rust *' -or $_.InnerText -like 'Rust *' })
23 if ($matches.Count -eq 1) { break }
24 }
25 Start-Sleep -Milliseconds 250
26 } while ([DateTime]::UtcNow -lt $deadline)
27 if ($matches.Count -ne 1) { throw 'Expected exactly one Rust-written paragraph on the Move leaf down page.' }
28 [IO.File]::WriteAllText("$Root\before.xml", $content, [Text.Encoding]::UTF8)
29 $text = 'Native after Rust ' + [char]::ConvertFromUtf32(0x1F98B) + ' e' + [char]0x0301
30 $matches[0].InnerText = [Security.SecurityElement]::Escape($text)
31 $app.UpdatePageContent($page.OuterXml, [DateTime]::MinValue, 1, $false)
32 $app.SyncHierarchy($notebook)
33 [IO.File]::WriteAllText("$Root\native-text.txt", $text, [Text.Encoding]::UTF8)
34} finally {
35 try { if ($notebook) { $app.CloseNotebook($notebook, $false) } }
36 finally {
37 [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($app)
38 $app = $null
39 [GC]::Collect()
40 [GC]::WaitForPendingFinalizers()
41 Get-Process ONENOTE -ErrorAction SilentlyContinue | Wait-Process -Timeout 120 -ErrorAction SilentlyContinue
42 }
43}