1param([Parameter(Mandatory=$true)][string]$Root,
2 [Parameter(Mandatory=$true)][string]$SharedPath,
3 [Parameter(Mandatory=$true)][string]$CloneHost,
4 [switch]$ResumeCache,
5 [int]$StartSequence = 1)
6Set-StrictMode -Version Latest
7$ErrorActionPreference = 'Stop'
8if ($SharedPath -notmatch '^\\\\192\.168\.77\.1\\agent\\m6-[a-z0-9-]+$') { throw 'Choose a disposable Linux lab notebook.' }
9if ($env:COMPUTERNAME -ne $CloneHost) { throw 'The collaboration controller requires its owned clone.' }
10if (-not $ResumeCache) { & "$PSScriptRoot\cold-current.ps1" -Root $Root -CloneHost $CloneHost }
11$shared = $SharedPath
12if (-not (Test-Path "$shared\synthetic.one")) { throw 'Create the shared fixture first.' }
13New-Item -ItemType Directory "$Root\inbox", "$Root\outbox" -Force | Out-Null
14$user = 'HKCU:\Software\Microsoft\Office\Common\UserInfo'
15New-Item $user -Force | Out-Null
16New-ItemProperty $user -Name UserName -Value $CloneHost -PropertyType String -Force | Out-Null
17New-ItemProperty $user -Name UserInitials -Value $CloneHost.Substring($CloneHost.Length - 3) -PropertyType String -Force | Out-Null
18$app = New-Object -ComObject OneNote.Application
19$notebook = ''
20try {
21 $app.OpenHierarchy($shared, '', [ref]$notebook, 0)
22 $section = ''
23 $app.OpenHierarchy('synthetic.one', $notebook, [ref]$section, 0)
24 [IO.File]::WriteAllText("$Root\ready", 'ready')
25 $deadline = [DateTime]::UtcNow.AddMinutes(30)
26 $sequence = $StartSequence
27 while ([DateTime]::UtcNow -lt $deadline) {
28 $name = '{0:d4}.json' -f $sequence
29 $input = "$Root\inbox\$name"
30 if (-not (Test-Path $input)) { Start-Sleep -Milliseconds 100; continue }
31 $command = Get-Content -Raw -Encoding UTF8 $input | ConvertFrom-Json
32 $output = "$Root\outbox\$sequence"
33 New-Item -ItemType Directory $output | Out-Null
34 try {
35 if ($command.action -eq 'sync') { $app.SyncHierarchy($notebook) }
36 elseif ($command.action -eq 'kill-process') {
37 Get-Process ONENOTE | Stop-Process -Force
38 [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($app)
39 $app = New-Object -ComObject OneNote.Application
40 $app.OpenHierarchy($shared, '', [ref]$notebook, 0)
41 $app.OpenHierarchy('synthetic.one', $notebook, [ref]$section, 0)
42 }
43 elseif ($command.action -eq 'edit' -or $command.action -eq 'snapshot') {
44 $hierarchy = ''
45 $app.GetHierarchy($section, 4, [ref]$hierarchy, 1)
46 [IO.File]::WriteAllText("$output\hierarchy.xml", $hierarchy, [Text.Encoding]::UTF8)
47 [xml]$tree = $hierarchy
48 $matches = 0
49 $editedPage = $null
50 $i = 0
51 foreach ($node in $tree.SelectNodes('//*[local-name()="Page"]')) {
52 $content = ''
53 $app.GetPageContent($node.GetAttribute('ID'), [ref]$content, 1, 1)
54 [IO.File]::WriteAllText("$output\page-$i.xml", $content, [Text.Encoding]::UTF8)
55 [xml]$page = $content
56 if ($command.action -eq 'edit') {
57 $targets = @($page.SelectNodes('//*[local-name()="T"]') | Where-Object {
58 [Net.WebUtility]::HtmlDecode([regex]::Replace($_.InnerText, '<[^>]*>', '')) -eq $command.expected
59 })
60 if ($targets.Count -gt 1) { throw 'Expected one matching text object.' }
61 if ($targets.Count -eq 1) {
62 $matches++
63 if ($matches -gt 1) { throw 'The text occurs on multiple pages.' }
64 $targets[0].InnerText = [Security.SecurityElement]::Escape($command.text)
65 $container = $targets[0]
66 while ($container.ParentNode -ne $page.DocumentElement) { $container = $container.ParentNode }
67 foreach ($sibling in @($page.DocumentElement.ChildNodes)) {
68 if ($sibling -ne $container -and $sibling.LocalName -in @('Outline', 'Title', 'Image', 'InkDrawing')) {
69 [void]$page.DocumentElement.RemoveChild($sibling)
70 }
71 }
72 $editedPage = $page
73 }
74 }
75 $i++
76 }
77 if ($command.action -eq 'edit' -and $matches -ne 1) { throw 'Expected one matching text object.' }
78 if ($editedPage) { $app.UpdatePageContent($editedPage.OuterXml, [DateTime]::MinValue, 1, $false) }
79 } elseif ($command.action -ne 'close') { throw 'Unknown collaboration command.' }
80 [IO.File]::WriteAllText("$output\done", 'ok')
81 } catch {
82 [IO.File]::WriteAllText("$output\error", $_.Exception.ToString())
83 }
84 if ($command.action -eq 'close') { break }
85 $sequence++
86 }
87} finally {
88 try { if ($notebook) { $app.CloseNotebook($notebook, $false) } }
89 finally {
90 [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($app)
91 $app = $null
92 [GC]::Collect()
93 [GC]::WaitForPendingFinalizers()
94 Get-Process ONENOTE -ErrorAction SilentlyContinue | Wait-Process -Timeout 10
95 [IO.File]::WriteAllText("$Root\closed", 'closed')
96 }
97}