variables:
 VERS_MAIN: '1.0'
 MACH_NAME: ''
 MACH_NAME_TEST: ''
 MANUFACTURER: ''
 NEW_REL: ''
 NET_SHARE_X: '\\10.74.82.201\EgwTech'
 NET_SHARE_Z: '\\10.74.82.201\Artifacts'
 NET_SHARE_S: '\\10.74.82.201\Scambio'
 NET_SHARE_R: '\\10.74.82.201\EgwTech'
 NET_SHARE_H: '\\10.74.82.252\team drives'
 NET_USERQ: 'steamw\egalware'
 NET_USER_SERVICES: 'steamw\services'
 MACH_VERS: '0.0.0.0'
 TEMP_PATH: 'C:\MachinesDeploy'
 MACHINES_PATH_R: 'R:\EgtData\Machines_GIT'
 DEST_MACHINES_PATH_H: 'H:'
 SRC_PATH: ''
 BASE_PATH: ''
 TEST_PATH: ''
 URL_VERSIONS_LOG: "https://liman.egalware.com/ELM.API/api/release/save"
 MACH_VERS_NUM: '0.0.0.0'
 CRITICAL_STRING: "#critical#"
 TOOLS_MACHINE_PATH: ""


#Note compilazione LUA:
# lua54 -o bin\$FileName -s $FileName: -o = output, -s = NON include i debug symbols

# helper copia script verso cartella di rete S:\ delle cartelle bin
.ReplicaS: &ReplicaS
   - |
      net use S: /delete
      SLEEP 2
      net use S: $env:NET_SHARE_S /u:$env:NET_USERQ $SDRIVE_PASSWD
      ROBOCOPY /MIR $env:BASE_PATH S:\LucaM\MachinesDeploy\$env:MACH_NAME\$env:MACH_VERS\ /XD "bin" /XD "$env:MACH_NAME_TEST"
      SLEEP 2
      net use S: /delete

# helper copia script verso cartella di rete R:\
.ReplicaR: &ReplicaR
   - |
      net use R: /delete
      SLEEP 2
      net use R: $env:NET_SHARE_R /u:$env:NET_USERQ $ZDRIVE_PASSWD
      echo "-----------------------"
      echo "   Copia macchina su R"
      echo "-----------------------"
      $customerPathR = $env:MACHINES_PATH_R + "\" + $env:MANUFACTURER
      ROBOCOPY /MIR "$env:SRC_PATH" "$customerPathR\$env:MACH_NAME\" /XD "bin"
      ROBOCOPY "$env:TEST_PATH" "$customerPathR\_TestMachines\$env:MACH_NAME_TEST\" /E /XD "bin"
      ROBOCOPY /MIR "$env:BASE_PATH" "$customerPathR\_Deploy\$env:MACH_NAME\$env:MACH_VERS\" /XD "bin" /XD "$env:MACH_NAME_TEST"
      SLEEP 2
      net use R: /delete

# helper copia script verso drive condiviso
.ReplicaStor01: &ReplicaStor01
   - |
      net use H: /delete
      SLEEP 2
      net use H: $env:NET_SHARE_H /u:$env:NET_USER_SERVICES $SRVCS_PASSWD
      echo "-----------------------"
      echo "   Copia macchina su stor01"
      echo "-----------------------"
      SLEEP 2
      $customerPathH = $env:DEST_MACHINES_PATH_H + "\" + $env:MANUFACTURER + "\test"
      $destinationPath = "$customerPathH\$env:MACH_NAME" + "\"
      ROBOCOPY "$env:BASE_PATH" "$destinationPath\" *.zip
      SLEEP 2
      net use H: /delete

# helper copia script verso cartella di rete R:\ per develop
.ReplicaR_DEV: &ReplicaR_DEV
   - |
      net use R: /delete
      SLEEP 2
      net use R: $env:NET_SHARE_R /u:$env:NET_USERQ $ZDRIVE_PASSWD
      echo "-----------------------"
      echo "   Copia macchina su R - DEV"
      echo "-----------------------"
      $customerPathR = $env:MACHINES_PATH_R + "\" + $env:MANUFACTURER
      ROBOCOPY /MIR "$env:BASE_PATH" "$customerPathR\_Deploy\$env:MACH_NAME\_DEV\$env:MACH_VERS\" /XD "bin" /XD "$env:MACH_NAME_TEST"
      SLEEP 2
      net use R: /delete

#helper copia sorgenti da R a percorso temporaneo
.CreateBaseFolder: &CreateBaseFolder
   - |   
      net use R: /delete
      SLEEP 2
      net use R: $env:NET_SHARE_R /u:$env:NET_USERQ $ZDRIVE_PASSWD
      echo "-----------------------"
      echo "   Recupero file non git da R e copia su macchina virtuale"
      echo "-----------------------"
      # test se la cartella con gli utensili esiste per questa macchina
      if (!(Test-Path -Path $env:TOOLS_MACHINE_PATH)) {
      echo "-----------------------"
      echo "CANNOT FIND TOOLS FOR THIS MACHINE!!"
      echo "-----------------------"
      Exit 1}
      ROBOCOPY "$env:TOOLS_MACHINE_PATH" "$env:SRC_PATH" /E /XF "*.bat" /XF "*.yml" /XF ".gitignore" /XD ".git" /XF "UpdateLog.txt" /XD ".vscode"
      ROBOCOPY "$env:TOOLS_MACHINE_PATH" "$env:TEST_PATH" /E /XF "*.bat" /XF "*.yml" /XF ".gitignore" /XD ".git" /XF "UpdateLog.txt" /XD ".vscode"
      ROBOCOPY "$env:TOOLS_MACHINE_PATH" "$env:SRC_PATH\bin\$env:MACH_NAME\" /E /XF "*.bat" /XF "*.yml" /XF ".gitignore" /XD ".git" /XF "UpdateLog.txt" /XD ".vscode"
      SLEEP 2
      net use R: /delete

# helper compilazione LUA a 32 e 64 bit
.LuaCompile: &LuaCompile
  - |
     Set-Alias lua54  C:\Tools\Lua32\luac54
     echo "-----------------------"
     echo "   Copia file su macchina virtuale"
     echo "-----------------------"
     ROBOCOPY . "$env:SRC_PATH" /E /XF "*.bat" /XF "*.yml" /XF ".gitignore" /XD ".git" /XF "UpdateLog.txt" /XD ".vscode"
     ROBOCOPY . "$env:TEST_PATH" /E /XF "*.bat" /XF "*.yml" /XF ".gitignore" /XD ".git" /XF "UpdateLog.txt" /XD ".vscode"
     Rename-Item -Path "$env:TEST_PATH\$env:MACH_NAME.mlde" -NewName "$env:MACH_NAME_TEST.mlde"
     Rename-Item -Path "$env:TEST_PATH\$env:MACH_NAME.ini" -NewName "$env:MACH_NAME_TEST.ini"
     ROBOCOPY . "$env:SRC_PATH\bin\$env:MACH_NAME\" /E /XF "*.bat" /XF "*.yml" /XF ".gitignore" /XD ".git" /XF "UpdateLog.txt" /XD ".vscode"
     echo "-----------------------"
     echo "   Compilazione file *.lua 32/64bit"
     echo "-----------------------"
     $FileList = Get-ChildItem * -Include( "*.mlpe", "*.mlse")
     ForEach ($File in $FileList) {
       $FileName = Split-Path $File -leaf
       lua54 -o $env:SRC_PATH\bin\$env:MACH_NAME\$FileName -s $FileName
       echo "lua54 -o bin\$FileName -s $FileName"
     }

# helper recupero nome macchina e costruttore
.machName-fix: &machName-fix
   - |
      echo "-----------------------"
      echo "   Recupero nome macchina e costruttore"
      echo "-----------------------"
      $fileName = dir *.mlde
      $env:MACH_NAME = $fileName.BaseName
      $env:MACH_NAME_TEST = $env:MACH_NAME + ".TEST"
      $comp = $fileName.BaseName -split "-"
      $env:MANUFACTURER = $comp[0]
      Write-Output $env:MANUFACTURER 

# helper calcolo versione
.version-fix: &version-fix
   - |
      echo "-----------------------"
      echo "   Calcolo versione"
      echo "-----------------------"

      # calcolo versione formato stringa
      $fPath = "$env:MACH_NAME.mlde";
      $vLine = Select-String -path $fPath -Pattern '^PP_VER';
      $comp = $vLine -split "=";
      $env:MACH_VERS = $comp[1].Replace("'","").Trim()
      # display versione formato stringa
      Write-Output $env:MACH_VERS

      # calcolo versione formato numerico
      $fPath = "$env:MACH_NAME.mlde";
      $vLine = Select-String -path $fPath -Pattern '^PP_NVER';
      $comp = $vLine -split "=";
      $env:MACH_VERS_NUM = $comp[1].Replace("'","").Trim()
      # display versione formato numerico
      Write-Output $env:MACH_VERS_NUM

# helper calcolo versione se develop
.version-fix_DEV: &version-fix_DEV
   - |
      echo "-----------------------"
      echo "   Calcolo versione - DEV"
      echo "-----------------------"
      $commitAuthor = $CI_COMMIT_AUTHOR -split " "
      $commitAuthor = $commitAuthor[0].Trim()
      $env:MACH_VERS = $commitAuthor
      # display versione
      Write-Output $env:MACH_VERS

# helper creazione folders 
.folder-fix: &folder-fix
   - |
      echo "-----------------------"
      echo "   Creazione cartelle su macchina virtuale"
      echo "-----------------------"
      $env:TOOLS_MACHINE_PATH = "$env:MACHINES_PATH_R\$env:MANUFACTURER\_DefaultMachineSetup\$env:MACH_NAME"
      Write-Output  $env:TOOLS_MACHINE_PATH
      $env:SRC_PATH = "$env:TEMP_PATH\$env:MACH_NAME\$env:MACH_VERS\$env:MACH_NAME";
      $env:BASE_PATH = "$env:TEMP_PATH\$env:MACH_NAME\$env:MACH_VERS";
      $env:TEST_PATH = "$env:TEMP_PATH\$env:MACH_NAME\$env:MACH_VERS\$env:MACH_NAME_TEST";
      Write-Output  $env:BASE_PATH;     
      if (Test-Path $env:BASE_PATH) { Remove-Item -Path "$env:BASE_PATH\*" -R -Force };
      Write-Output  $env:TEST_PATH;

# helper compressione e pulizia folders 
.ZipClean: &ZipClean
   - |
      "  Compressione file su macchina virtuale"
      $7zipPath = $env:ProgramFiles+"\7-Zip\7z.exe";
      if (-not (Test-Path -Path $7zipPath -PathType Leaf)) {
          throw "7 zip file '$7zipPath' not found"
      }
      Set-Alias 7zip $7zipPath
      $Target = "$env:BASE_PATH\$env:MACH_NAME.zip"
      cd "$env:SRC_PATH\bin\"
      $Source = "*"
      7zip a -tzip $Target $Source  
      Write-Output "called ZIP $Source --> $Target"

# helper invio notifica a log versioni online (LiMan)
.SendToVersionsLog: &SendToVersionsLog
   - |
      echo "-----------------------"
      echo "   Invio versione a log online"
      echo "-----------------------"
      $tags = ""
      $date = ""
      # se critico scrive tag
      if ( $CI_COMMIT_MESSAGE | Select-String -Pattern $env:CRITICAL_STRING) {
         $tags = "CRITICAL"
         $date = (Get-Date).AddDays(1).toString("yyyy-MM-ddTHH:mm:ss.fffZ")
      }
      else
      {
         $date = (Get-Date).AddDays(3).toString("yyyy-MM-ddTHH:mm:ss.fffZ")
      }
      $body =
      @{
            codInst = "EgalWare"
            codApp = $env:MACH_NAME
            uplAppId = "UpdateManager"
            masterKey = $LiMan_Key
            tipo = "Machine"
            versNum = $env:MACH_VERS_NUM
            versText = $env:MACH_VERS
            releaseDate = $date
            relTags = $tags
      }
      $jsonBody = ConvertTo-Json -InputObject $body
      echo $jsonBody
      Invoke-WebRequest -Method Post -URI $env:URL_VERSIONS_LOG -ContentType "application/json" -Body $jsonBody -UseBasicParsing


# helper esecuzione test 
.RunTest: &RunTest
   - |
      Write-Output "Test done!"      


stages:
 - build
#  - test
#  - deploy

LuaCompile:build:
 stage: build
 only:
   - main
   - master
 tags:
   - win
 before_script:
   - *machName-fix
   - *version-fix
   - *folder-fix
 script:
   - *CreateBaseFolder    
   - *LuaCompile
   - *ZipClean
   - *ReplicaR
   - *ReplicaStor01
   - *SendToVersionsLog

LuaCompileDev:build:
 stage: build
 only:
   - develop
 tags:
   - win
 before_script:
   - *machName-fix
   - *version-fix_DEV
   - *folder-fix
 script:
   - *CreateBaseFolder    
   - *LuaCompile
   - *ZipClean
   - *ReplicaR_DEV

# LuaCompile:test:
#  stage: test
#  needs: ["LuaCompile:build"]
#  only:
#    - main
#    - master
#    - develop
#  tags:
#    - win
#  before_script:
#    - *version-fix
#    - *folder-fix
#  script:    
#    - *LuaCompile
#    - *RunTest
   
# LuaCompile:deploy:
#  stage: deploy
#  needs: ["LuaCompile:test"]
#  only:
#    - main
#    - master
#  tags:
#    - win
#  before_script:
#    - *version-fix
#    - *folder-fix
#  script:    
#    - *LuaCompile
#    - *ZipClean
#    - *ReplicaR

