# $data = @(
#     [pscustomobject]@{FirstName = 'Kevin'; LastName = 'Marquette' }
#     [pscustomobject]@{FirstName = 'John'; LastName = 'Doe' }
# )
$COMP = $env:COMPUTERNAME
Set-Clipboard $env:COMPUTERNAME
if ($COMP -eq 'ADMINISTRATOR') {
    $Global:TCMD_PATH = 'L:\progs\TCMDx64'
} else {
    $Global:TCMD_PATH = 'C:\job\TCMDx64'
}
$Global:REG_PATH_ROOT = 'HKCU'
# $Global:REG_PATH_ROOT = 'HKLM'
$Global:TCMD_REG_PATH = "$($Global:REG_PATH_ROOT):\Software\Ghisler\Total Commander"
$Global:TCMD_REG_PATH


$Global:ENVVARS = @(
    [pscustomobject]@{ NAME = 'Commander_Path'; VALUE = $Global:TCMD_PATH; REGKEY = 'InstallDir'; STATUS = 0; DESCR = 'Корневой каталог Total Commander' }
    [pscustomobject]@{ NAME = 'COMMANDER_INI'; VALUE = Join-Path -Path $Global:TCMD_PATH -ChildPath 'wincmd.ini'; REGKEY = 'IniFileName'; STATUS = 0; DESCR = 'ini файл настроек Total Commander' }
    [pscustomobject]@{ NAME = 'COMMANDER_FTP'; VALUE = Join-Path -Path $Global:TCMD_PATH -ChildPath 'wcx_ftp.ini'; REGKEY = 'FtpIniName'; STATUS = 0; DESCR = 'Файл настроек ftp Total Commander' }
)

$Global:TCMD_PATH


function EnvReg {
    [pscustomobject]$Var
	
	##Проверка
	# if ('ADMINISTRATOR' -eq $($env:COMPUTERNAME)) {
    # $TCMD_PATH = 'L:\progs\TCMDx64'
	# } else {
		# $TCMD_PATH = 'C:\job\TCMDx64'
	# }
	# $REG_PATH_ROOT = 'HKLM'
	# $ENV_REG_PATH = "$($REG_PATH_ROOT):\Software\Ghisler\Total Commander\InstallDir"
	# $ENV_REG_KEY = 'InstallDir'
	# $ENV_REG_VAL = $TCMD_PATH
	# if (!(Test-Path "$ENV_REG_PATH")) {
        # New-Item -Path "$ENV_REG_PATH" -Force | Out-Null
    # }
	# Set-ItemProperty -Path "$ENV_REG_PATH" -Name "$ENV_REG_KEY" -Value "$ENV_REG_VAL"
	# Get-ItemProperty -Path "$ENV_REG_PATH" -Name "$ENV_REG_KEY"	
	# $ENV_REG_KEY = 'IniFileName'
	# $ENV_REG_VAL = Join-Path -Path $TCMD_PATH -ChildPath 'wincmd.ini'
	Set-ItemProperty -Path "$ENV_REG_PATH" -Name "$ENV_REG_KEY" -Value "$ENV_REG_VAL"
	# Get-ItemProperty -Path "$ENV_REG_PATH" -Name "$ENV_REG_KEY"	
	
	
    $ENV_NAME = $Var.NAME        
    $ENV_REG_KEY = $Var.REGKEY
    $ENV_REG_PATH = $Global:TCMD_REG_PATH
    $ENV_REG_VAL = $Var.VALUE
    
    Write-Host "Устанавливаю значение ключа $ENV_REG_KEY = $ENV_REG_VAL переменной $ENV_NAME в реестре " -ForegroundColor Green
    if (!(Test-Path "$ENV_REG_PATH")) {
        New-Item -Path "$ENV_REG_PATH" -Force | Out-Null
    }
    Set-ItemProperty -Path "$ENV_REG_PATH" -Name "$ENV_REG_KEY" -Value "$ENV_REG_VAL"
    $Var.STATUS = 1
}

function EnvVar {
    param (
        [pscustomobject]$Var
    )
    $ENV_NAME = $Var.NAME
    # $ENV_VALUE = 'C:\job\TCMDx64'
    $ENV_VALUE = $Var.VALUE
    $ENV_DESCR = $Var.DESCR    
    $FENV = Get-Item -Path "Env:\$ENV_NAME" -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Value
    if ($FENV) {    
        Write-Host "Переменная $ENV_NAME существует. Eё значение '$FENV'" -ForegroundColor Green
        EnvReg -Var $Var
        return
    }
    Write-Host "Переменная $ENV_NAME не существует" -ForegroundColor Red
    $isValidPath = [System.IO.Path]::IsPathRooted($ENV_VALUE)
    if ($true -eq $isValidPath) {
        if (!(Test-Path "$ENV_VALUE")) {    
            Write-Host "Путь $ENV_VALUE не существует" -ForegroundColor Red            
            return
        }
    }       
    # New-Variable -Name $ENV_NAME -Visibility Public -Value $ENV_VALUE -Description $ENVDESCR -Scope Global
    Write-Host "Создаем переменную $ENV_NAME" -ForegroundColor Red
    [Environment]::SetEnvironmentVariable($ENV_NAME, $ENV_VALUE, 'User')
    EnvReg -Var $Var
    Get-Item -Path "Env:\$ENV_NAME" -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Value
}


function GetEnvVars {
    foreach ($V in $Global:ENVVARS) {
        Write-Host "Работаем с переменной $($V.NAME)" -ForegroundColor Cyan
        EnvVar -Var $V
        # Write-Host 'Ожидание 6 сек...' -ForegroundColor Cyan
        # Start-Sleep -Seconds 6
    }
    
}

function Main {
    Clear-Host
    Stop-Process -Name 'TOTALCMD64' -Force -ErrorAction SilentlyContinue
    GetEnvVars
    Write-Host 'Завершено. Ожидание 2 сек...' -ForegroundColor Cyan
    Start-Sleep -Seconds 2
    $TCMD_EXE_PATH = Join-Path -Path "$($Global:TCMD_PATH)" -ChildPath 'TOTALCMD64.EXE'
    $PINTOTASKBAR_PATH = Join-Path -Path "$($Global:TCMD_PATH)" -ChildPath 'program\PinToTaskbar\syspin.exe'
    if (Test-Path "$PINTOTASKBAR_PATH") {
        # 5386  : Pin to taskbar
        # 5387  : Unpin from taskbar
        # syspin 'PROGRAMFILES\Internet Explorer\iexplore.exe' 5386
        Start-Process -FilePath "$PINTOTASKBAR_PATH" -ArgumentList "$TCMD_EXE_PATH", '5386'
    }
    Start-Process -FilePath "$TCMD_EXE_PATH" -Verb RunAs
}

Main