param (
	[parameter(Mandatory=$true)]
	[string]
	$templateFileName,
    
	[parameter(Mandatory=$false)]
	[string]
	$outputFileName,

	[parameter(Mandatory=$false)]
	[string]
	$registryHive = 'HKEY_LOCAL_MACHINE',

	[parameter(Mandatory=$false)]
	[string]
	$comServerDirectory,

	[parameter(Mandatory=$false)]
	[string]
	$comServerFileName
)

if (!$outputFileName) {
    if ($templateFileName.EndsWith('.template') {
        $outputFileName = $templateFileName.Substring(0, $templateFileName.Length - 9)
    } else {
        $outputFileName = 'out.reg'
    }
}


function DoRegAction {
	param(
		# [parameter(Mandatory=$true)]
		# [String]
		# $regExe,

		# [parameter(Mandatory=$true)]
		# [String]
		# $action,

		[parameter(Mandatory=$true)]
		[String]
		$path
    )

    $regExe32 = "$($env:SystemRoot)\system32\reg.exe"
    $regExe64 = "$($env:SystemRoot)\syswow64\reg.exe"
    if (Test-Path $regExe64) {
        $command = $regExe64
    } else {
        $command = $regExe32
    }

    #$arguments = @($action, $path)
    $arguments = New-Object System.Collections.Generic.List[System.Object]
    $arguments.Add($action)
    $arguments.Add($path)
    switch ($action) {
        # 'add' {
        # }
        'delete' {
            $arguments.Add('/f')
        }
        default { # query
            $arguments.Add('/s')
            $errorMsg = "Missing key: $path"
        }
    }

    if ($errorMsg) {
        try {
            & "$command" $arguments
        } catch {
            Write-Error $errorMsg
        }
    } else {
        & "$command" $arguments
    }
}

# Write-Host "=-=-=-=-= DEBUG =-=-=-=-= type: $type"
# Write-Host "=-=-=-=-= DEBUG =-=-=-=-= action: $action"
# Write-Host "=-=-=-=-= DEBUG =-=-=-=-= registryHive: $registryHive"
# Write-Host "=-=-=-=-= DEBUG =-=-=-=-= clsid: $clsid"
# Write-Host "=-=-=-=-= DEBUG =-=-=-=-= interfaceId: $interfaceId"
# Write-Host "=-=-=-=-= DEBUG =-=-=-=-= eventInterfaceId: $eventInterfaceId"
# Write-Host "=-=-=-=-= DEBUG =-=-=-=-= libId: $libId"
# Write-Host "=-=-=-=-= DEBUG =-=-=-=-= className: $className"
DoRegAction "$registryHive\Software\Classes\CLSID\{$clsid}"
DoRegAction "$registryHive\Software\Classes\Interface\{$interfaceId}"
DoRegAction "$registryHive\Software\Classes\Interface\{$eventInterfaceId}"
DoRegAction "$registryHive\Software\Classes\$ClassName"
DoRegAction "$registryHive\Software\Classes\$ClassName.1"
DoRegAction "$registryHive\Software\Classes\TypeLib\{$libId}"
