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('Lifecycle.one', $notebook, [ref]$section, 0)
11 $deadline = [DateTime]::UtcNow.AddSeconds(30)
12 do {
13 $hierarchy = ''
14 $app.GetHierarchy($section, 4, [ref]$hierarchy, 1)
15 [xml]$tree = $hierarchy
16 $pages = @($tree.SelectNodes('//*[local-name()="Page"]'))
17 if ($pages.Count -eq 9) { break }
18 Start-Sleep -Milliseconds 200
19 } while ([DateTime]::UtcNow -lt $deadline)
20 $targets = @($pages | Where-Object { $_.GetAttribute('name') -eq 'Same title' })
21 if ($pages.Count -ne 9 -or $targets.Count -ne 1) { throw 'Expected nine pages and one moved parent.' }
22 $pageId = [Security.SecurityElement]::Escape($targets[0].GetAttribute('ID'))
23 $title = 'Native moved ' + [char]::ConvertFromUtf32(0x1F98B) + ' e' + [char]0x301
24 $title = [Security.SecurityElement]::Escape($title)
25 $xml = "<one:Page xmlns:one='http://schemas.microsoft.com/office/onenote/2010/onenote' ID='$pageId'><one:Title><one:OE><one:T>$title</one:T></one:OE></one:Title><one:Outline><one:Position x='72' y='144'/><one:OEChildren><one:OE><one:T>Native body after a Rust page move.</one:T></one:OE></one:OEChildren></one:Outline></one:Page>"
26 [IO.File]::WriteAllText((Join-Path $Root 'native-edit.xml'), $xml, [Text.Encoding]::UTF8)
27 $app.UpdatePageContent($xml, [DateTime]::MinValue, 1, $false)
28 $app.SyncHierarchy($notebook)
29} finally {
30 [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($app)
31 [GC]::Collect()
32 [GC]::WaitForPendingFinalizers()
33}