Install-Chocolatey.ps1


function Install-Chocolatey() {
    [cmdletbinding()]
    Param(
        [string] $Path = $null,
        [string] $ToolsDirectory = $null,
        [switch] $Force = $false,
        [HashTable] $Feeds,
        [String] $ConfigPath
    )

    
    $setupFile = $null; 

    if(![string]::IsNullOrWhiteSpace($ConfigPath)) {
        $setupFile = $ConfigPath
    } else {
        if(Test-Path "${Env:USERPROFILE}/OneDrive/.choco-bear/choco-bear-setup.json") {
            $setupFile = "${Env:USERPROFILE}/OneDrive/.choco-bear/choco-bear-setup.json"
        }
        if(Test-Path "${Env:USERPROFILE}/.choco-bear/choco-bear-setup.json") {
            $setupFile = "${Env:USERPROFILE}/.choco-bear/choco-bear-setup.json"
        }
    }

    if($setupFile -ne $null) {
        $json = [System.IO.File]::ReadAllText($setupFile);
        $config = ConvertFrom-Json $json 
        $Path = $config.Path;
        $ToolsDirectory = $config.ToolsDirectory;
        $Feeds = @{}
        $config.Feeds.PSObject.Properties | ForEach-Object {
           $name = $_.name
           $feed = $_.value 
           $Feeds.Add($name, $feed);
        }
    }
    
    $hasAllUsersProfile = Test-Path Env:\ALLUSERSPROFILE
    if([String]::IsNullOrWhiteSpace($Path) -or $Force.ToBool()) {

        Write-Debug "Path was empty, falling back to default location";

        if($hasAllUsersProfile) {
            $Path = "${Env:ALLUSERSPROFILE}"
        } elseif(Test-Path Env:\HOME) {
            $Path = "$Env:HOME"
        } else {
            Write-Error "Default location not available, please specify a -Path parameter"; 
            return;
        }
    }

    if([string]::IsNullOrWhiteSpace($ToolsDirectory) -or $Force.ToBool()) {
        if($hasAllUsersProfile -and $Path -eq $Env:ALLUSERSPROFILE) {
            $ToolsDirectory = "C:\tools";
        } else {
            $ToolsDirectory = $Path;
        }
    }

    if(-not (Test-Path $Path)) {
        mkdir $Path | Write-Debug;
    }

    
    if($Env:OS -eq "Windows_NT") { 
        $isAdmin = [bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-32-544")

        if(-not (Test-Path Env:\ChocolateyInstall)) {
            $Env:ChocolateyInstall = "$Path\chocolatey";
            if($isAdmin) {
                setx ChocolateyInstall "${Env:ChocolateyInstall}" /M 
            } else {
                setx ChocolateyInstall "${Env:ChocolateyInstall}"
            }
        }

        if(-not (Test-Path Env:\ChocolateyToolsLocation)) {
            $Env:ChocolateyToolsLocation = "$ToolsDirectory";
            if($isAdmin) {
                setx ChocolateyToolsLocation "$Env:ChocolateyToolsLocation" /M 
            } else {
                setx ChocolateyToolsLocation "$Env:ChocolateyToolsLocation"
            }
        }
    }

    # No point in reinstalling chocolatey if it already exists unless forced to do so.
    if(-not (Test-Path "$Env:ChocolateyInstall\choco.exe") -or $Force.ToBool()) {
        iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex
    }

    if($Feeds -ne $null -and $Feeds.Count -gt 0) {

        if(-not $Feeds.Contains("chocolatey")) {
            $Feeds.Add("chocolatey", "http://chocolatey.org/api/v2");
        }
        
        # chocolatey won't throw an error if the feed does not already exist
        foreach($name in $Feeds.Keys) {
             choco source remove -n="$name"
        }

        # add feeds back in specific order
        foreach($name in $Feeds.Keys) {
             $feed = $Feeds[$name];

             if([string]::IsNullOrWhiteSpace($feed)) {
                 Write-Warning "feed for $name was empty"
                 continue 
             }

             if($feed -contains '|') {
                 $index = $feed.IndexOf('|')
                 $url = $feed.Substring(0, $index)
                 $parameters = $feed.Substring($index + 1)

                 & "$Env:ChocolateyInstall\choco.exe" source add -n="$name" -s="$url" $parameters
                 continue
             }

             & "$Env:ChocolateyInstall\choco.exe" source add -n="$name" -s="$feed"
        }
    }

    choco upgrade chocolatey -yf 
}