PSUnifi.psm1

function Repair-UnifiPackage {
    param(
        [uri] $Url
    )
    $TemporaryPackageName = 'unifi-package.deb'
    $FinalPackageName = 'unifi-package-fixed.db'
    $TemporaryDirectory = Get-TemporaryDirectory # creates directory
    if ($null -ne $TemporaryDirectory) {
        $BinaryPath = [System.IO.Path]::Combine($TemporaryDirectory, $TemporaryPackageName)
        $Invoke = Invoke-WebRequest -Uri $Url -OutFile $BinaryPath
        if ($Invoke) {
            Write-Color 'Temporary directory ', $TemporaryDirectory, ' for storing ', $TemporaryPackageName -Color Yellow, White, Green
            $TemporaryUnpackDirectory = Get-TemporaryDirectory # creates directory
            if ($null -ne $TemporaryUnpackDirectory) {
                Write-Color 'Temporary directory ', $TemporaryUnpackDirectory, ' for unpacking ', $TemporaryPackageName -Color Yellow, White, Green
                dpkg-deb -R $BinaryPath $TemporaryUnpackDirectory
                $ControlFile = [System.IO.Path]::Combine($TemporaryUnpackDirectory, 'DEBIAN', 'control')

                $FileContent = Get-Content -LiteralPath $ControlFile
                $NewContent = foreach ($Line in $FileContent) {
                    if ($Line -like '*mongodb*') {
                        #Write-Color $File -Color Green
                    } else {
                        #Write-Color $File -Color Yellow
                        $Line
                    }
                }
                $LinesBefore = $FileContent.Count
                $LinesAfter = $NewContent.Count
                Write-Color 'Lines removed ', ($LinesAfter - $LinesAfter) -Color Yellow, Green
                $NewContent | Set-Content -LiteralPath $ControlFile
            
            }

            $BinaryPathFinal = [System.IO.Path]::Combine($TemporaryDirectory, $FinalPackageName)
            dpkg-deb -b $TemporaryUnpackDirectory $BinaryPathFinal            
            if (Test-Path -LiteralPath $BinaryPathFinal) {
                Write-Color 'File ', $FinalPackageName, ' was saved to location ', $BinaryPathFinal -Color Yellow, Green, Yellow, Green
            }
        }
    }
}


Export-ModuleMember `
    -Function @('Repair-UnifiPackage') `
    -Alias @()