PostInstall.ps1

# References:
# 1. Below are the list of predefined vars that can be used:
# - $PSScriptRoot [System defined] The folder path for current scipt file, NOT the caller script to call this function
function InjectCodeBlock
{
    param(
        [string] $Target,
        [string] $CodeId,
        [string] $CodeBlock
    )

    if (($Target -is [string]) -AND ($Target.Contains("<$CodeId>")))
    {
        $Target = ReplaceCodeBlock -Target $Target -CodeId $CodeId -CodeBlock $CodeBlock
    }
    else
    {
        $Target = $Target.TrimEnd()
        $Target += "`r`n`r`n# <$CodeId>`r`n"
        $Target += "$CodeBlock"
        $Target += "`r`n# </$CodeId>`r`n"
    }
    
    return $Target.Trim()
}

function ReplaceCodeBlock
{
    param(
        [string] $Target,
        [string] $CodeId,
        [string] $CodeBlock
    )

    $powerShellProfile -replace "(?ms)^(`r`n)*# <$CodeId>.*# </$CodeId>(`r`n)*","`r`n`# <$CodeId>`r`n$CodeBlock`r`n# </$CodeId>`r`n`r`n"
}

function RemoveCodeBlock
{
    param(
        [string] $Target,
        [string] $CodeId
    )

    $powerShellProfile -replace "(?ms)^(`r`n)*# <$CodeId>.*# </$CodeId>(`r`n)*","`r`n"
}

function InstallPsProfile
{
    param(
        [string] $CodeId,
        [string] $TargetScriptPath,
        [string] $Owner,
        [string] $Comment
    )

    if ([string]::IsNullOrWhiteSpace($TargetScriptPath)){return}

    $powerShellProfileDir = Join-Path $([Environment]::GetFolderPath("MyDocuments")) "WindowsPowerShell"
    $powerShellProfilePath = Join-Path $PowerShellProfileDir "Microsoft.PowerShell_profile.ps1"

    # Create a empty profile if it does not exists
    if (-not (Test-Path $powerShellProfilePath))
    {
        New-Item -Path $powerShellProfilePath -ItemType File -Force
    }

    $powerShellProfile = Get-Content $powerShellProfilePath -Raw
    
    if (-NOT [string]::IsNullOrWhiteSpace($Comment))
    {
        $codeBlock += "# $Comment`r`n"
    }
    
    $codeBlock += "if (Test-Path `"$TargetScriptPath`"){ . `"$TargetScriptPath`" }"
    InjectCodeBlock -Target $powerShellProfile -CodeId $CodeId -CodeBlock $codeBlock | Set-Content $powerShellProfilePath
}

. "$PSScriptRoot\Common.ps1"

# Install PSProfile.ps1, it will support the auto loading for PSProfile.ps1 for all of MyProfiles.
$psProfileScriptPath = Join-Path $PSScriptRoot "PSProfile.ps1" -Resolve
InstallPsProfile -CodeId "34418a32-466b-456e-bc36-e8f6f71f465d" -TargetScriptPath $psProfileScriptPath -Comment "Entrance for the PowerShell profile for MyProfile"

# Install environment vars: MyProfileBinPath, MyProfilePSModulePath
Add-EnvironmentListVariableValues -Name 'Path' -ValuesToAppend "%$MyProfileBinPathVarName%" -Target User

$currentUserPsModulesPath = Join-Path $([Environment]::GetFolderPath("MyDocuments")) "WindowsPowerShell\Modules"
Add-EnvironmentListVariableValues -Name 'PSModulePath' -ValuesToAppend @($currentUserPsModulesPath,"%$MyProfilePSModulesPathVarName%") -Target User

# Install SysProfile.ps1
$sysRunRegPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run"
$sysProfileTrigger = "PowerShell -Executionpolicy Unrestricted -WindowStyle Hidden -File `"$MyProfileModuleSysProfilePath`""
#New-ItemProperty -Path $sysRunRegPath -Name "MyProfile" -Value $sysProfileTrigger -PropertyType String -Force | Out-Null
Copy-Item $MyProfileModuleMyProfileShortcutPath -Destination $([Environment]::GetFolderPath("Startup")) -Force