Install-PaperinikDB.ps1

#Requires -Version 5.1

<#
.SYNOPSIS
    Installs the PaperinikDB module to the user's PowerShell modules directory.
 
.DESCRIPTION
    This script copies the PaperinikDB module files to the appropriate location
    in the user's PowerShell modules directory, allowing the module to be imported
    and used in PowerShell sessions.
 
.EXAMPLE
    .\Install-PaperinikDB.ps1
 
    Installs the module to $env:USERPROFILE\Documents\PowerShell\Modules\PaperinikDB\<version>
 
.NOTES
    Author: OpusTecnica
    Version: 1.0.0
    Date: 2025-12-23
#>


[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')]
param()

begin {
    Write-Verbose 'Starting PaperinikDB module installation'

    # Get the script directory
    $scriptDirectory = $PSScriptRoot

    # Path to the module manifest
    $manifestPath = Join-Path $scriptDirectory 'PaperinikDB.psd1'

    # Check if manifest exists
    if (-not (Test-Path $manifestPath)) {
        throw "Module manifest not found at $manifestPath"
    }

    # Import the manifest data to get version
    try {
        $manifestData = Import-PowerShellDataFile $manifestPath
        $moduleVersion = $manifestData.ModuleVersion
        $moduleName = 'PaperinikDB'
    } catch {
        throw "Failed to read module manifest: $_"
    }

    Write-Verbose "Module version: $moduleVersion"

    # Determine the target installation path
    $userModulesPath = Join-Path $env:USERPROFILE 'Documents\PowerShell\Modules'
    $targetPath = Join-Path (Join-Path $userModulesPath $moduleName) $moduleVersion

    Write-Verbose "Target installation path: $targetPath"
}

process {
    # Create the target directory if it doesn't exist
    if (-not (Test-Path $targetPath)) {
        if ($PSCmdlet.ShouldProcess($targetPath, 'Create directory')) {
            try {
                New-Item -ItemType Directory -Path $targetPath -Force | Out-Null
                Write-Verbose "Created directory: $targetPath"
            } catch {
                throw "Failed to create target directory: $_"
            }
        }
    } else {
        Write-Verbose "Target directory already exists: $targetPath"
    }

    # Copy the module files
    if ($PSCmdlet.ShouldProcess($scriptDirectory, "Copy module files to $targetPath")) {
        try {
            Copy-Item -Path (Join-Path $scriptDirectory '*') -Destination $targetPath -Recurse -Force
            Write-Verbose 'Module files copied successfully'
        } catch {
            throw "Failed to copy module files: $_"
        }
    }

    Write-Host "PaperinikDB module version $moduleVersion installed successfully to $targetPath"
}

end {
    Write-Verbose 'PaperinikDB module installation completed'
}