Dictionaries/Dict.OS/Includes/Bootloader/Includes/Bootloader.Syslinux.psm1

<#
.SYNOPSIS
Search for a syslinux configuration
 
.DESCRIPTION
Search into local partition for a syslinux.cfg file.
Default search for a "syslinux.cfg" file. If config name is overriden, use -Filename to search for custom filename.
 
.EXAMPLE
Find-OSBootloadersyslinuxCfg -Path "/boot"
 
.EXAMPLE
Find-OSBootloadersyslinuxCfg -Path "/boot" -Filename "mysyslinux.cfg"
 
.NOTES
General notes
#>

function Find-OSBootloadersyslinuxCfg {
    [CmdletBinding()]
    [OutputType([String])]
    Param (
        # Path to start search
        [Parameter(Mandatory = $true, ValueFromPipeLine = $true)][string]$Path,
        # Custom filename to search for
        [Parameter(Mandatory = $false, ValueFromPipeLine = $false)][string]$Filename = "syslinux.cfg",
        # Search as far as MaxDepth deep
        [Parameter(Mandatory = $false, ValueFromPipeLine = $false)][uint32]$MaxDepth = 3
    )
    Begin {
        Write-PwShFwOSEnterFunction
    }

    Process {
        $syslinuxcfg = Get-ChildItem -Path $Path $Filename -Recurse -Depth $MaxDepth -ErrorAction SilentlyContinue
        return ${syslinuxcfg}?.fullname
    }

    End {
        Write-PwShFwOSLeaveFunction
    }
}

function Get-OSBootloaderSyslinuxDefaultBoot {
    [CmdletBinding()]
    [OutputType([String])]
    Param (
        [Alias('ConfigFile')]
        [Parameter(Mandatory = $false, ValueFromPipeLine = $false)][string]$SyslinuxCfg = (Find-OSBootloadersyslinuxCfg -Path "/boot")
    )
    Begin {
        Write-PwShFwOSEnterFunction
    }

    Process {
        $rc = (Get-Content $SyslinuxCfg | select-string "^LABEL") | ForEach-Object { $_ -match "^LABEL\s*(?<label>\w+)" }
        if ($rc) {
            return $Matches.label
        } else {
            return $null
        }
    }

    End {
        Write-PwShFwOSLeaveFunction
    }
}

function Set-OSBootloaderSyslinuxDefaultBoot {
    [CmdletBinding()]
    [OutputType([System.Boolean])]
    Param (
        [Alias('ConfigFile')]
        [Parameter(Mandatory = $false, ValueFromPipeLine = $false)][string]$SyslinuxCfg = (Find-OSBootloadersyslinuxCfg -Path "/boot"),
        [Parameter(Mandatory = $true, ValueFromPipeLine = $false)][string]$label
    )
    Begin {
        Write-PwShFwOSEnterFunction
    }

    Process {
        $syslinux = (Get-Content $SyslinuxCfg -Raw) -replace "^DEFAULT .*", "DEFAULT $label"
        $syslinux | Out-File $SyslinuxCfg
        return $?
    }

    End {
        Write-PwShFwOSLeaveFunction
    }
}