# Esegue setup di BeamWall da repo Artifacts

Write-Output ""
Write-Output "---------------------------------"
Write-Output "| EgtBEAMWALL Last Release Test |"
Write-Output "---------------------------------"
Write-Output ""

#inizializzo variabili vuote
$SelBranch = ""
$SelArch = ""
$ChosenBit = ""
$ChosenDll = ""
$ChosenDllD = ""

#Definisco path truenas di origine
$Artifacts = '\\truenas\Artifacts'
$BWSourceFolder = '\\truenas\Artifacts\EgtProg\EgtBEAMWALL'

#Definisco path test sottocartelle EgtBEAMWALL
$TestProgBWFolder = 'c:\EgtTestProg\EgtBEAMWALL'
$TestDataBWFolder = 'c:\EgtTestData\EgtBEAMWALL'

#Cerco le cartelle dei branch in \\truenas\Artifacts\EgtProg\EgtBEAMWALL
Write-Output ""
$BranchSelection = $false
$Branches = Get-ChildItem -Path $BWSourceFolder -Directory
$BranchCount = $Branches.Count

#Chiedo all'utente che branch desidera installare
do{
    $idx = 1
    Write-Output "-----------------------------"
    foreach($item in $Branches)
    {
        Write-Output("$idx - $item")
        $idx++
    }
    Write-Output "-----------------------------"
    Write-Output ""
    Write-Output ""
    $SelBranch = @(Get-ChildItem $BWSourceFolder -Directory | Out-GridView -Title 'Scegli il branch EgtBeamWall da testare' -PassThru)
    $BranchSelection = ($numSel -le $BranchCount)
} while(!$BranchSelection)

#Chiedo all'utente che architettura desidera installare
Write-Output ""
$ArchSelection = $false
$Architectures = 'x86','x64'
$ArchCount = $Architectures.Count

do{
    $idx = 1
    Write-Output "-----------------------------"
    foreach($item in $Architectures)
    {
        Write-Output("$idx - $item")
        $idx++
    }
    Write-Output "-----------------------------"
    Write-Output ""
    Write-Output ""
    $SelArch = @($Architectures | Out-GridView -Title 'Scegli architettura EgtBeamWall da testare' -PassThru)
    $ArchSelection = ($numSel -le $ArchCount)
    Write-Output $SelArch
} while(!$ArchSelection)

#Switch fra 32 bit o 64 bit in base alla richiesta dell'utente
if($SelArch -eq 'x86'){
    $ChosenBit = '32'
    $ChosenDll = 'Dll32'
    $ChosenDllD = 'DllD32'
}
else {
    $ChosenBit = '64'
    $ChosenDll = 'Dll64'
    $ChosenDllD = 'DllD64'
} 

#Pulisco cartella EgtTestProg\EgtBEAMWALL se già esistente, poi la creo
if (Test-Path -Path $TestProgBWFolder) {
    "Prog esiste!"
    Remove-Item -Recurse $TestProgBWFolder\*.*
} else {
    new-item $TestProgBWFolder -itemtype directory
}

#Pulisco cartella EgtTestData\EgtBEAMWALL se già esistente, poi la creo
if (Test-Path -Path $TestDataBWFolder) {
    "Data esiste!"
    Remove-Item -Recurse $TestDataBWFolder\*.*
} else {
    new-item $TestDataBWFolder -itemtype directory
}

#Pulisco cartella EgtTestData\EgtBEAMWALL\Temp se già esistente, poi la creo
if (Test-Path -Path $TestDataBWFolder\Temp) {
    "Temp esiste!"
    Remove-Item -Recurse $TestDataBWFolder\Temp\*.*
} else {
    new-item $TestDataBWFolder\Temp -itemtype directory
}

#Pulisco cartella EgtTestData\EgtBEAMWALL\Macro se già esistente, poi la creo
if (Test-Path -Path $TestDataBWFolder\Macro) {
    "Macro esiste!"
    Remove-Item -Recurse $TestDataBWFolder\Macro\*.*
} else {
    new-item $TestDataBWFolder\Macro -itemtype directory
}

#Pulisco cartella EgtTestData\EgtBEAMWALL\Config se già esistente, poi la creo
if (Test-Path -Path $TestDataBWFolder\Config) {
    "Config esiste!"
    Remove-Item -Recurse $TestDataBWFolder\Config\*.*
} else {
    new-item $TestDataBWFolder\Config -itemtype directory
}

#Copio la cartella Config
ROBOCOPY /E $Artifacts\EgtProg\EgtBEAMWALL_Config\Config\ $TestDataBWFolder\Config

#Copio la cartella Messages
ROBOCOPY /E $Artifacts\EGT_SRV\EgtTech\EgtData\Messages $TestDataBWFolder\Config

#Copio la cartella Fonts
ROBOCOPY /E $Artifacts\EGT_SRV\EgtTech\EgtProg\Fonts\ $TestDataBWFolder\Fonts

#Copio DataRoot.ini custom in c:\EgtTestProg\EgtBEAMWALL
ROBOCOPY /E $Artifacts\EgtProg\EgtBEAMWALL_Config\ $TestProgBWFolder DataRoot.ini

#Copio il file Medium.ini in \Warehouse 
ROBOCOPY /E $Artifacts\EGT_SRV\EgtTech\EgtData\EgtBEAMWALL\Warehouse\ $TestDataBWFolder\Warehouse Medium.ini

#Copio Resources
ROBOCOPY /E $Artifacts\EGT_SRV\EgtTech\EgtData\EgtBEAMWALL\Resources\ $TestDataBWFolder\Resources

#Copio la sottocartella 32 o 64 bit di Lualibs a seconda della richiesta
ROBOCOPY /E $Artifacts\EGT_SRV\EgtTech\EgtProg\LuaLibs\$ChosenBit $TestDataBWFolder\Lualibs

#Copio la cartella Dll32 o Dll64 a seconda della richiesta
ROBOCOPY /E $Artifacts\EGT_SRV\EgtTech\EgtProg\$ChosenDll $TestProgBWFolder

#Copio la cartella DllD32 o DllD64 a seconda della richiesta
ROBOCOPY /E $Artifacts\EGT_SRV\EgtTech\EgtProg\$ChosenDllD $TestProgBWFolder

#Copio Versione + Architettura di BEAMWALL richieste
ROBOCOPY /E $Artifacts\EgtProg\EgtBEAMWALL\$SelBranch\$SelArch $TestProgBWFolder

#Cancello tutti i .exe.config e .dll.config perchè problematici in area Test
Remove-Item -Recurse $TestProgBWFolder\*.exe.config
Remove-Item -Recurse $TestProgBWFolder\*.dll.config

#Chiudo lanciando lo Script Manager per altre operazioni
Powershell.exe -executionpolicy remotesigned -File  .\EgtScriptsManager.ps1