function Test-IsAdmin {
    if ($IsWindows) {
        try {
            $identity = [Security.Principal.WindowsIdentity]::GetCurrent()
            $principal = New-Object Security.Principal.WindowsPrincipal -ArgumentList $identity
            return $principal.IsInRole( [Security.Principal.WindowsBuiltInRole]::Administrator )
        } catch {
            throw "Failed to determine if the current user has elevated privileges. The error was: '{0}'." -f $_
        }
    } else {
        $(whoami) -eq "root"
    }


    <#
        .SYNOPSIS
            Checks if the current Powershell instance is running with elevated privileges or not.
        .EXAMPLE
            PS C:\> Test-IsAdmin
        .OUTPUTS
            System.Boolean
                True if the current Powershell is elevated, false if not.
    #>
}

function Pick-One {
    [CmdletBinding()]
    param(
        [String[]]$options
    );

    $rand = Get-Random -Minimum 0 -Maximum $options.Length;

    foreach ($option in $options) {
        if($option -eq $options[$rand]) {
            Write-Host -ForegroundColor Green '=> ' $option;
        } else {
            Write-Host '   ' $option -ForegroundColor DarkGray;
        }
    }
}

function Prepend-LineNumbers {
    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline=$true)]$input,
        [int]$pad = 4
    );

    $line = 0;
    $input | % {
        $line++;

        $line.ToString().PadLeft($pad, ' ') + " $_"
    }
}

function Fix-Colors {
    [System.Console]::ForegroundColor = [System.ConsoleColor]::White;
    [System.Console]::BackgroundColor = [System.ConsoleColor]::Black;
    $escape = [char]27
    
    echo "$escape[0m"
}

function Upload-File {
    param([string]$Path)

    sharex $(Resolve-Path $Path)
}

function Set-VsVars
{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true, HelpMessage="Enter VS version as 2010, 2012, 2013, 2015, 2017, 2017-BuildTools")]
        [ValidateSet(2010,2012,2013,2015,2017,'2017-BuildTools')]
        [string]$Version,

        [Parameter(Mandatory=$false, HelpMessage="Architecture either x64, x86 or arm (2017 only)")]
        [Alias('Arch')]
        [ValidateSet('x64', 'x86', 'arm')]
        [string]$Architecture
    )

    $VS_VERSION = @{ 
        2010 = " 10.0"; 
        2012 = " 11.0"; 
        2013 = " 12.0"; 
        2015 = " 14.0"; 
        2017 = "\2017\Community"; 
        '2017-BuildTools' = '\2017\BuildTools'
    }

    if($version -ge 2015)
    {
        $targetDir = "c:\Program Files (x86)\Microsoft Visual Studio$($VS_VERSION[$version])\Common7\Tools"
        $vcvars = "VsDevCmd.bat"

        if ($Architecture) {
            $params = ' -arch=' + $Architecture
        }
    }
    else
    {
        $targetDir = "c:\Program Files (x86)\Microsoft Visual Studio$($VS_VERSION[$version])\VC"
        $vcvars = "vcvarsall.bat"

        if($Architecture -eq 'x64') {
            $Architecture = 'x86_64';
        }

        if ($Architecture) {
            $params = ' ' + $Architecture
        }
    }
  
    if (!(Test-Path (Join-Path $targetDir $vcvars))) {
        "Error: Visual Studio $version not installed"
        return
    }

    $command = $vcvars + $params

    pushd $targetDir
    Write-Verbose "$command & set"

    cmd /c "$command & set" | % {
      if ($_ -match "(.*?)=(.*)") {
        Write-Verbose $_

        Set-Item -force -path "ENV:\$($matches[1])" -value "$($matches[2])"
      }
    }
    popd

    write-host "`nVisual Studio $version Command Prompt variables set." -ForegroundColor Yellow
}

function Import-VariablesFromCmd
{
    [CmdletBinding()]
    param([string]$path)
    cmd /c $path + "&set" | % {
      if ($_ -match "(.*?)=(.*)") {
        Write-Verbose $_

        Set-Item -force -path "ENV:\$($matches[1])" -value "$($matches[2])"
      }
    }
}

# not working, will get fixed eventually
function Out-PasteBin 
{
    [CmdletBinding()]
    param(
        [Parameter(
            Position=0,
            ValueFromPipeline=$true,
            parametersetname='nopipeline'
        )][string] $File,
        [Parameter(
            Position=1, 
            ValueFromPipelineByPropertyName=$true
        )][string] $Language,
        [Parameter(
            ValueFromPipelineByPropertyName=$true
        )][string] $Author,
        [Parameter(
            ValueFromPipelineByPropertyName=$true
        )][string] $Title,
        [Parameter(
            ValueFromPipelineByPropertyName=$true
        )][string] $Key = $global:PastebinKey
    )

    Begin {
        $pipeline = -not ($PSCmdlet.ParameterSetName -eq 'nopipeline');
    }

    Process {
        $Source += $_ + "`n";
    }
    
    End {
        $paste = if ($pipeline) { $Source } else { cat $File | Out-String }

        return (Invoke-WebRequest bin.kadet.net -Method Post -MaximumRedirection 0 -ErrorAction SilentlyContinue -Body @{ 
            author=$Author; 
            key=$Key; 
            title=$Title; 
            language=$Language; 
            paste=$paste;
        })
    }
}

function ~ {
    cd ~ @args;
}

function .. {
    cd .. @args;
}


function Run-Box {
    php '-dphar.readonly=0' "D:\Software\box.phar" @args;
}

function Run-Symfony {
    $path = if (Test-Path ./bin/console) { "./bin/console" } 
        elseif (Test-Path ./app/console) { "./app/console" }
        else { throw "oh die" };

    php $path @args;
}

function Run-Artisan {
    php artisan @args;
}
