# -----------------------------------------------------------------------------
#                    S C R I P T   S P E C I F I C A T I O N
#                  Copyright 2019-2020 Motorola Solutions, Inc.
#                              All Rights Reserved.
#                   Motorola Solutions Confidential Restricted
# -----------------------------------------------------------------------------

<#
.SYNOPSIS
    Name: RunCustomSteps.ps1 run scripts that are defined in the registry
.DESCRIPTION
    Script will run every script which can be found in path that was declared in registry HKLM:\SOFTWARE\Motorola\MotoPatch[ProductName]\[InstalationPhase]Scripts (ex: HKLM:\SOFTWARE\Motorola\MotoPatchOpenJDK\PostInstallConfigScripts)
    Example of registry structure:
    HKLM:\SOFTWARE\Motorola\MotoPatchOpenJDK
    |
    *---*-----------------------------------------------------------------------------*
        *      Name      |                          Data                              *
        *-----------------------------------------------------------------------------*
        * 2.FixSomething | powershell.exe C:\fixsomething.ps1 -argument1 -argument2 valueForArgument *
        *-----------------------------------------------------------------------------*
        * 1.MyBatScript  | C:\anotherSc.bat                                           *
        *-----------------------------------------------------------------------------*
.EXAMPLE
    RunCustomSteps.ps1 -ProductName OpenJDK -InstallationPhase PostInstallConfig -LogFile "C:\Windows\debug\Motorola OpenJDK.log"
#>

# MODIFICATION HISTORY
# Date         Change
# -----------------------------------------------------------------------------
# 01 Dec 19    Initial creation
# 12 May 20    AWP-1465 Improvement of Motopatch custom scripts
# -----------------------------------------------------------------------------
[CmdletBinding(SupportsShouldProcess = $true)]
param (
    #Name of the product for which you want run a custom scripts
    [Parameter(Mandatory = $true)] [String] $ProductName,
    #Name of the phase for which you want run a custom scripts
    [Parameter(Mandatory = $true)] [ValidateSet("PostInstallConfig", "PostUpgrade", "PreCheck", "PreInstallConfig", "PreUpgrade", "Terminate")] [String] $InstallationPhase
)

Set-Variable -name "SUCCESS" -Value 0 -Option ReadOnly
Set-Variable -name "FAILURE" -Value 1 -Option ReadOnly

$scriptPath = Split-Path -Path $MyInvocation.MyCommand.Path -Parent
$scriptName = $MyInvocation.MyCommand.Name

. "$scriptPath\scripts\LoggingFunctions.ps1"

if ($ProductName -match "x64") {
    $logName = $ProductName -ireplace "\s*x64$", " x64"
}
else {
    $logName = $ProductName
}
$logFile = "C:\Windows\debug\Motorola $($logName).log"

$motorolaCustomScriptsRegistryKey = "HKLM:\SOFTWARE\Motorola\MotoPatch$ProductName\$($InstallationPhase)Scripts\"

if ( -not (Test-Path $motorolaCustomScriptsRegistryKey)) {
    Write-Log "There are no scripts to run. Registry path $motorolaCustomScriptsRegistryKey doesn't exist. Skiping"
    exit $SUCCESS
}
Write-Log "Getting information from $motorolaCustomScriptsRegistryKey"

try {
    $customScriptsRegistryObject = Get-Item -Path $motorolaCustomScriptsRegistryKey
    if ( $customScriptsRegistryObject.ValueCount -eq 0 ) {
        Write-Log "There are no scripts to run. Registry key '$($customScriptsRegistryObject)' is empty"
        exit $SUCCESS
    }
}
catch {
    Write-Log "$_"
    exit $FAILURE
}

$customScriptsRegistryObject.Property | Foreach-Object {
    $currentRegistryKeyProperty = $_
    try {
        $currentProcess = Start-Process "powershell.exe" -ArgumentList "& '$scriptPath\RunScriptFromRegistry.ps1' -RegistryKeyPath '$($customScriptsRegistryObject.PSPath)\$currentRegistryKeyProperty' -LogFile '$logFile'" -NoNewWindow -Wait -PassThru
    }
    catch {
        Write-Log "$_" -type $ERROR_C
        exit $FAILURE
    }
    if ($currentProcess.ExitCode -ne 0) {
        exit $currentProcess.ExitCode
    }
}