### Esegue setup di BeamWall da repo Artifacts ###

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

#Definisco path truenas di origine
$Artifacts = '\\truenas\Artifacts'

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

#Chiedo all'utente che branch desidera installare
function Show-MenuA
{
    param (
        [string]$Title = 'Seleziona Branch BeamWall'
    )
    Clear-Host
    Write-Host ""
    Write-Host "============= $Title ============="
    Write-Host ""
    Write-Host "1: Premi '1' per branch Develop"
    Write-Host "2: Premi '2' per branch Master"
    Write-Host "q: Premi 'q' tornare al menù principale"
    Write-Host ""
}

Show-MenuA

#Salvo la selezione dell'utente 
$MachineSelect = Read-Host 'Seleziona Branch BeamWall'
switch ($MachineSelect)
{
    '1' {
         $SelBranch = 'develop'
    }
    '2' {
         $SelBranch = 'master'
    }
    'q' {
        Powershell.exe -executionpolicy remotesigned -File  .\CLI\EgtScriptsManager.ps1
    }
}

#Chiedo all'utente se vuole 32 bit o 64 bit
function Show-MenuB
{
    param (
        [string]$Title = 'Seleziona architettura BeamWall'
    )
    Clear-Host
    Write-Host ""
    Write-Host "============= $Title ============="
    Write-Host ""
    Write-Host "1: Premi '1' per x86"
    Write-Host "2: Premi '2' per x64"
    Write-Host "q: Premi 'q' tornare al menù principale"
    Write-Host ""
}

Show-MenuB

#Salvo la selezione dell'utente 32 bit o 64 bit
$BitSelect = Read-Host "Seleziona architettura BeamWall"
switch ($BitSelect)
{
    '1' {
         $SelArch = 'x86'
    }
    '2' {
        $SelArch = 'x64'
    }
    'q' {
        Powershell.exe -executionpolicy remotesigned -File  .\CLI\EgtScriptsManager.ps1
    }
}

#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'
} 

Write-Output ""
Write-Output "Installing Test EgtBeamWall. Branch: $SelBranch Architecture: $ChosenBit bit"
Write-Output ""
Start-Sleep -seconds 5

#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  .\CLI\EgtScriptsManager.ps1