param($headnode = $env:computername)

#update the HpcBroker.exe.config and HpcSession.exe.config files
$localhost = New-Object System.Text.RegularExpressions.Regex "localhost", SingleLine
copy $env:ccp_home\bin\HpcBroker.exe.config .\HpcBroker.exe.config.bak -force
copy $env:ccp_home\bin\HpcSession.exe.config .\HpcSession.exe.config.bak -force

Get-Item "*.exe.config.bak" | ForEach-Object {
    $output = [System.IO.File]::CreateText([System.IO.Path]::Combine((Get-Location).Path, $_.FullName + ".new"))
    $text = [System.IO.File]::ReadAllText($_.FullName)
    $text = $localhost.Replace($text, $headnode)
    
    $output.Write($text);
    
    $output.Close()
}

move HpcBroker.exe.config.bak.new $env:ccp_home\bin\HpcBroker.exe.config -force
move HpcSession.exe.config.bak.new $env:ccp_home\bin\HpcSession.exe.config -force

#create the certificates
cmd /c .\createcert.cmd $headnode

.\installcert.ps1 install

restart-service hpcsession
restart-service hpcbroker

#add to java trusts
Get-Item "C:\Program Files*\Java\jdk*\bin\javac.exe" | ForEach-Object {$javac =  $_.FullName}
Get-Item "C:\Program Files*\Java\jdk*\bin\java.exe" | ForEach-Object {$java = $_.FullName}
Get-Item "C:\Program Files*\Java\jdk*\jre\lib\security\cacerts" | ForEach-Object {$cacerts = $_.fullname}

cmd /c $javac InstallCert.java
cmd /c $java InstallCert $env:computername

copy $cacerts -force

copy jssecacerts $cacerts -force