IntelliSearch.psm1

#requires -version 4
#requires -RunAsAdministrator
#requires -Modules powershell-yaml

function ModuleRoot { Split-Path $MyInvocation.MyCommand.Path -Parent }

# Removes the Zone Identifier from the files.
# This is to help with importing the .dll files, .ps1 files and other executables
Get-Item -Path ".\*" -Stream Zone.Identifier -ErrorAction:SilentlyContinue | Remove-Item -ErrorAction:SilentlyContinue

$Public  = @( Get-ChildItem -Path $PSScriptRoot\Public\*.ps1 -ErrorAction SilentlyContinue )
$Private = @( Get-ChildItem -Path $PSScriptRoot\Private\*.ps1 -ErrorAction SilentlyContinue )

foreach($import in @($Public + $Private))
{
    try
    {   
        Write-Verbose "Importing cmdlet $($import.fullname)"
        . $import.fullname
    }
    catch
    {
        Write-Error -Message "Failed to import function $($import.fullname): $_"
    }
}

Write-Verbose "Exporting cmdlets"
Export-ModuleMember -Function $Public.BaseName

#region Find NuGet path
Write-Verbose "Finding NuGet.exe path"
$LIB_DIR = Join-Path (ModuleRoot) "Lib"
$NUGET_EXE = Join-Path $LIB_DIR "nuget.exe"
$NUGET_URL = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"

# Ensure Lib folder exists
if ((Test-Path ModuleRoot) -and !(Test-Path $LIB_DIR -PathType Container))
{
    Write-Verbose -Message "Creating tools directory..."
    New-Item -Path $LIB_DIR -Type Directory -Force | Out-Null
}

# Try find NuGet.exe in path if not exists
if (!(Test-Path $NUGET_EXE))
{
    Write-Verbose -Message "Trying to find nuget.exe in PATH..."
    $existingPaths = $Env:Path -Split ';' | Where-Object { (![string]::IsNullOrEmpty($_)) -and (Test-Path $_ -PathType Container) }
    $NUGET_EXE_IN_PATH = Get-ChildItem -Path $existingPaths -Filter "nuget.exe" | Select-Object -First 1
    
    if ($NUGET_EXE_IN_PATH -ne $null -and (Test-Path $NUGET_EXE_IN_PATH.FullName))
    {
        Write-Verbose -Message "Found in PATH at $($NUGET_EXE_IN_PATH.FullName)."
        $NUGET_EXE = $NUGET_EXE_IN_PATH.FullName
    }
}

if (!(Test-Path $NUGET_EXE))
{
    Write-Verbose -Message "Attempting to download NuGet.exe..."
    $wc = New-Object System.Net.WebClient
    $wc.Headers["User-Agent"] = "ISPSModuleInitiator"
    $wc.DownloadFile($NUGET_URL, $NUGET_EXE)
    Unblock-File -Path $NUGET_EXE -ErrorAction:SilentlyContinue
}

# Attempt to export the NuGet.exe path
Write-Verbose "Exporting NuGet.exe path as variable NUGET_EXE"
if (Test-Path $NUGET_EXE)
{
    Export-ModuleMember -Variable "NUGET_EXE"
}
else
{
    Throw "Could not find any path to NuGet.exe."
}
#endregion Find NuGet path

#region Load config file
$IS_SettingsPath = Join-Path $Env:ProgramData "\IntelliSearch\.IntelliShell\settings.config"
if ( -not (Test-Path $IS_SettingsPath))
{
    $IS_SettingsFile = New-Item -Path $IS_SettingsPath -ItemType File -Force
    $IS_SettingsFile.Directory.Attributes = $IS_SettingsFile.Directory.Attributes -bor [io.fileattributes]::Hidden
}

$IS_Settings = ConvertFrom-Yaml -Yaml (Get-Content $IS_SettingsPath -Raw)

if ($IS_Settings -eq $null)
{
    $IS_Settings = New-Object -TypeName System.Collections.Hashtable
}

$IS_Settings.InstanceStore = if ($IS_Settings.InstanceStore) {$IS_Settings.InstanceStore} else {(Join-Path $env:ProgramData "\IntelliSearch")}
$IS_Settings.ComponentStore = if ($IS_Settings.ComponentStore) {$IS_Settings.ComponentStore} else {(Join-Path $env:ProgramFiles "\IntelliSearch")}
$IS_Settings.InstanceCreationPluginStore = if ($IS_Settings.InstanceCreationPluginStore) {$IS_Settings.InstanceCreationPluginStore} else {@((Join-Path (ModuleRoot) "\InstanceCreationPlugins"))}

Write-Verbose "Loaded settings file with following key-values:"
foreach ($Key in $IS_Settings.GetEnumerator())
{
    Write-Verbose ("${Key}: $($IS_Settings.$Key)")
}
Export-ModuleMember -Variable "IS_Settings"
#endregion Load config file