install.ps1

[CmdletBinding()]
Param (
    [switch]$Force
)

#Requires -Version 3

Push-Location -Path $PSScriptRoot
$Manifest = Invoke-Expression -Command ( Get-Content -Path *.psd1 | Out-String )
$ModuleName = $Manifest.RootModule -replace '\.psm1$',''

if ( Get-Module $ModuleName -ListAvailable ) {
    $InstalledVersion = Get-Module $ModuleName -ListAvailable | Select -ExpandProperty Version
} Else {
    $InstalledVersion = 0
}

$ShouldUpdate = ( $Force -or $Manifest.ModuleVersion -gt $InstalledVersion )

If ( -not $ShouldUpdate ) { Pop-Location ; Return 'The {0} module is already installed' -f $ModuleName }

If ( Get-Module -Name $ModuleName ) { Remove-Module -Name $ModuleName }

$ProfilePath = Split-Path -Path $profile
$ModulesPath = Join-Path -Path $ProfilePath -ChildPath 'Modules'
$InstallPath = Join-Path -Path $ModulesPath -ChildPath $ModuleName

If ( Test-Path -Path $InstallPath ) { Remove-Item -Path $InstallPath -Recurse }

Try {
    If ( $InstallPath -ne $PSScriptRoot ) {
        $null = New-Item -Path $InstallPath -ItemType Directory
        foreach ( $file in $Manifest.FileList ) {
            $FullFilePath =  Join-Path -Path $PSScriptRoot -ChildPath $file
            Copy-Item -Path $FullFilePath -Destination $InstallPath
        }
        Out-Default -InputObject ( 'The {0} module has been installed.' -f $ModuleName )
    }
}
Catch {
    Out-Default -InputObject 'There has been a problem installing the {0} module.' -f $ModuleName
    Pop-Location
}

Pop-Location