@echo off


:: We modify the file using powershell (scripting Chet does not seem to do anythong)

cd Delphi

echo == Stripping Invalid C-Style UA_StatusCode Casts ==
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Command "(Gc 'open62541.pas') -replace '\(\(UA_StatusCode\)\s*', '(' | Out-File 'open62541.pas' -Encoding utf8"

echo == Remapping CRITICAL_SECTION for Delphi Win64 Compatibility ==
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Command "(Gc 'open62541.pas') -replace '\bCRITICAL_SECTION\b', '_RTL_CRITICAL_SECTION' | Out-File 'open62541.pas' -Encoding utf8"

echo == Removing UA_LOGLEVEL Line ==
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Command "(Gc 'open62541.pas') -notmatch '^\s*UA_LOGLEVEL\s*=\s*100;' | Out-File 'open62541.pas' -Encoding utf8"

echo == Renaming all UA_ functions to _UA_ ==
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Command "(Gc 'open62541.pas') -replace 'function\s+(UA_[A-Za-z0-9_]+)\(', 'function _${1}(' | Out-File 'open62541.pas' -Encoding utf8"

echo == Renaming all UA_ procedures to _UA_ ==
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Command "(Gc 'open62541.pas') -replace 'procedure\s+(UA_[A-Za-z0-9_]+)\(', 'procedure _${1}(' | Out-File 'open62541.pas' -Encoding utf8"

echo == Inserting Generated Banner Comment ==
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Command "$lines = Gc 'open62541.pas'; $insert = '{ Additional automated changes added }'; $lines[0..2] + $insert + $lines[3..($lines.Count-1)] | Out-File 'open62541.pas' -Encoding utf8"

echo == Injecting Native Delphi Pointer Hooks and Init Logic ==
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Command "$content = Gc 'open62541.pas' -Raw; $vars = 'type' + [char]13 + [char]10 + '  PUaTypesArray = ^TUaTypesArray;' + [char]13 + [char]10 + '  TUaTypesArray = array[0..UA_TYPES_COUNT-1] of UA_DataType;' + [char]13 + [char]10 + [char]13 + [char]10 + 'var' + [char]13 + [char]10 + '  _UA_TYPES    : PUATypesArray = nil;' + [char]13 + [char]10 + '  _UA_TRANSPORT: PUATypesArray = nil;' + [char]13 + [char]10 + [char]13 + [char]10 + 'implementation'; $content = $content -replace '(?mi)^implementation\b', $vars; $init = 'initialization' + [char]13 + [char]10 + '  _UA_TYPES     := Winapi.Windows.GetProcAddress(Winapi.Windows.GetModuleHandle(''libopen62541.dll''), ''UA_TYPES'');' + [char]13 + [char]10 + '  _UA_TRANSPORT := Winapi.Windows.GetProcAddress(Winapi.Windows.GetModuleHandle(''libopen62541.dll''), ''UA_TRANSPORT'');' + [char]13 + [char]10 + [char]13 + [char]10 + 'end.'; $content = $content -replace '(?mi)^end\.\s*$', $init; $content | Out-File 'open62541.pas' -Encoding utf8"

echo == Inserting Alignment and Packing Directives ==
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Command "$content = Get-Content 'open62541.pas' -Raw; $content = $content -replace '\{\$MINENUMSIZE 4\}', ('{$MINENUMSIZE 4}' + [char]13 + [char]10 + '{$ALIGN 8}'); Set-Content 'open62541.pas' $content -Encoding utf8"



cd ..

