Start-JellyfinLibraryScan.psm1

function Start-JellyfinLibraryScan {
    [CmdletBinding()]
    param (
        [string]$server = 'homeservarr',    # Default server name in homeservarr project
        [string]$port = 2351,               # Default Jellyfin port in homeservarr project
        [string]$apiKey                     # API Key for authentication
    )
    
    $testConnection = Test-NetConnection -ComputerName $server -Port $port -WarningAction SilentlyContinue
    if (-not $testConnection.TcpTestSucceeded) {
        Write-Host "Cannot connect to $server on port $port. Please check the server name and port." -ForegroundColor Red
        return
    }
    else {
        $configFilePath = ($home + '\AppData\Roaming\Start-JellyfinLibraryScan\' + $server)
        $configFile = $configFilePath + '\config.txt'
        
        if (!(Test-Path -Path $configFilePath)) {
            New-Item -ItemType Directory -Path $configFilePath -Force
        }

        if (!(Test-Path -Path $configFile)) {            
            $apiKey | Set-Content -Path $configFile -Force
        } else {
            if ((Test-Path $configFile -PathType Leaf) -and ((Get-Item $configFile).Length -lt 3)) {
                $apiKey | Set-Content -Path $configFile -Force
            } else {
                [string]$apiKey = Get-Content -Path $configFile -Force
            }
        }
        
        $url = 'http://' + $server + ':' + $port + '/library/refresh?api_key=' + $apiKey
        try {
            $response = Invoke-RestMethod -Method Post -Uri $url -ErrorAction Stop

            Write-Host "Request Succeeded"
            Write-Host
            Write-Host "$server - Library Scan started successfully." -ForegroundColor Green
            Write-Host "Note: It may take a few minutes for the scan to reflect in the Jellyfin UI." -ForegroundColor Yellow
            Write-Host
            Write-Host
        }
        catch {
            Write-Host "Request Failed."
            Write-Host "Most likely you forgot to include the api key the first time OR"
            Write-Host "Jellyfin isn't running at the location you specified"
            Write-Host
            Write-Host
        }
    }
}
function Get-JellyfinLibraryScanConfig {
    [CmdletBinding()]
    param (
        #[string]$server = 'homeservarr'
    )
    
    $configFilePath = ($home + '\AppData\Roaming\Start-JellyfinLibraryScan\')
    $configs = Get-ChildItem -Path $configFilePath -Directory -Name -ErrorAction SilentlyContinue
    if ($null -eq $configs) {
        Write-Host "No configurations found for server $server." -ForegroundColor Yellow
        return
    } else {
        Write-Host "Here are the Jellyfin Servers you have saved APIs for configurations for server $server" -ForegroundColor Green
        Write-Host "Configurations:"
        Write-Host '--------------------------------'
        foreach ($config in $configs) {
            Write-Host "- $config"
        }
        Write-Host
        Write-Host '--------------------------------'
        Write-Host
        Write-Host "If there are no items listed it means no server API's have been saved yet." -ForegroundColor Yellow
        Write-Host 'To add a server API, run the "Start-JellyfinLibraryScan" function with all the parameters at least once.' -ForegroundColor Yellow
        Write-Host
        Write-Host
        Write-Host 'Use the "Start-JellyfinLibraryScanCleaner" function to remove specific server configurations.' -ForegroundColor Yellow
        Write-Host "Note: You can manually delete the config files if you wish to remove saved API keys." -ForegroundColor Yellow
        Write-Host
        Write-Host
        Write-Host
    }
}
function Start-JellyfinLibraryScanCleaner {
    [CmdletBinding()]
    param (
        [string]$server = 'homeservarr'
    )
    
    [string]$configFolderPath = ($home + '\AppData\Roaming\Start-JellyfinLibraryScan\' + $server)
    
    if (Test-Path -Path $configFolderPath) {
        Remove-Item -Path $configFolderPath -Recurse -Force
        Write-Host "Configuration folders have been removed." -ForegroundColor Green
    } else {
        Write-Host "No configuration file found for $server." -ForegroundColor Yellow
    }
}
Export-ModuleMember -Function Start-JellyfinLibraryScan, Start-JellyfinLibraryScanCleaner, Get-JellyfinLibraryScanConfig