version: 3.0.1.{build}
image: Visual Studio 2019
clone_folder: C:\ProxSpace\pm3\proxmark
environment:
  proxspace_url: https://github.com/Gator96100/ProxSpace/archive/master.zip
  proxspace_zip_file: \proxspace.zip
  proxspace_zip_folder_name: ProxSpace-*
  proxspace_path: \ProxSpace
  proxspace_home_path: \ProxSpace\pm3

init:
- ps: >-
    $psversiontable

    #Get-ChildItem Env:

    $releasename=""

    $env:APPVEYOR_REPO_COMMIT_SHORT = $env:APPVEYOR_REPO_COMMIT.Substring(0, 8)

    if ($env:appveyor_repo_tag -match "true"){
      $releasename=$env:APPVEYOR_REPO_TAG_NAME + "/"
    }

    $releasename+=$env:APPVEYOR_BUILD_VERSION + " [" + $env:APPVEYOR_REPO_COMMIT_SHORT + "]"


    Write-Host "repository: $env:appveyor_repo_name branch:$env:APPVEYOR_REPO_BRANCH release: $releasename" -ForegroundColor Yellow

    Add-AppveyorMessage -Message "[$env:APPVEYOR_REPO_COMMIT_SHORT]$env:appveyor_repo_name($env:APPVEYOR_REPO_BRANCH)" -Category Information -Details "repository: $env:appveyor_repo_name branch: $env:APPVEYOR_REPO_BRANCH release: $releasename"

    # iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/appveyor/ci/master/scripts/enable-rdp.ps1'))
clone_script:
- ps: >-
    
    Function ExecUpdate($Text, $firstStart) {
        Write-Host "$Text" -NoNewLine 
        Start-Process "cmd.exe"  "/c ""cd /D $env:proxspace_path && call msys2\ps\setup.cmd && msys2\msys2_shell.cmd -mingw64 -defterm -no-start -c ""exit"""""
        $StartTime=[System.Environment]::TickCount
        Start-Sleep -s 10
        while($true) {
            $cmdprocess = Get-Process "cmd" -ErrorAction SilentlyContinue
            
            if (!$cmdprocess -Or $cmdprocess.HasExited) {
                Write-Host "[ OK ]" -ForegroundColor Green
                break
            }
            
            if ($firstStart -And (Test-Path "$env:proxspace_path\msys2\etc\pacman.conf.pacnew")) {
                Start-Sleep -s 5
                $tmp = $cmdprocess.CloseMainWindow()
                Start-Sleep -s 5
                Stop-Process -Name "cmd" -Force -ErrorAction SilentlyContinue
                Write-Host "Exit by pacman.conf" -ForegroundColor Green
                break           
            }
            
            if ([System.Environment]::TickCount-$StartTime -gt 1000000) {
                Write-host "Exit by timeout" -ForegroundColor Yellow
                break
            }
            
            Start-Sleep -s 1
        }
    }
    
    Write-Host "Removing ProxSpace..." -NoNewLine
    
    $CloneTime=[System.Environment]::TickCount
    
    cd \

    Remove-Item -Recurse -Force -Path $env:proxspace_path

    Write-Host "[ OK ]" -ForegroundColor Green

    Write-Host "Download ProxSpace..." -NoNewLine
    
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    
    Invoke-WebRequest "$env:proxspace_url" -outfile "$env:proxspace_zip_file"
    
    Write-Host "[ OK ]" -ForegroundColor Green
    
    Write-Host "Extracting ProxSpace..." -NoNewLine
    
    Expand-Archive -LiteralPath "$env:proxspace_zip_file" -DestinationPath "\"
    
    Remove-Item "$env:proxspace_zip_file"
    
    Write-Host "[ OK ]" -ForegroundColor Green
    
    Write-Host "Renaming ProxSpace folder..." -NoNewLine
    
    Get-ChildItem -Path "\$env:proxspace_zip_folder_name" | Rename-Item -NewName (Split-Path $env:proxspace_path -Leaf)
    
    Write-Host "[ OK ]" -ForegroundColor Gree 

    ExecUpdate "Initial msys2 startup..." $true
    
    ExecUpdate "Installing required packages..." $false
    
    
    $psversion = (Select-String -Pattern 'PSVERSION=' -SimpleMatch -Path "$env:proxspace_path\msys2\ps\09-proxspace_setup.post").Line.Split("""")[1]
    
    Write-Host "ProxSpace version: $psversion" -ForegroundColor Yellow
    
    Write-Host "Cloning repository <$env:appveyor_repo_name> to $env:appveyor_build_folder ..." -NoNewLine

    if(-not $env:appveyor_pull_request_number) {
        git clone -q --branch=$env:appveyor_repo_branch https://github.com/$env:appveyor_repo_name.git $env:appveyor_build_folder
        cd $env:appveyor_build_folder
        git checkout -qf $env:appveyor_repo_commit
    } else {
        git clone -q https://github.com/$env:appveyor_repo_name.git $env:appveyor_build_folder
        cd $env:appveyor_build_folder
        git fetch -q origin +refs/pull/$env:appveyor_pull_request_number/merge:
        git checkout -qf FETCH_HEAD
    }
    
    Write-Host "[ OK ]" -ForegroundColor Green
    
    Add-AppveyorMessage -Message "ProxSpace download and update took $(([System.Environment]::TickCount-$CloneTime) / 1000) sec" -Category Information
    

install:
build_script:
- ps: >-

    $pmfolder = Split-Path $env:appveyor_build_folder -Leaf
    
    Function ExecMinGWCmd($Cmd) {
        cd $env:proxspace_path
        msys2\msys2_shell.cmd -mingw64 -defterm -no-start -c "cd $pmfolder && $Cmd"
    }

    
    Write-Host "---------- make ----------" -ForegroundColor Yellow
    
    $TestTime=[System.Environment]::TickCount  
    
    #make
    
    cd $env:proxspace_path
    
    msys2\ps\setup.cmd
    
    ExecMinGWCmd "make clean;make V=1"

    #some checks

    if(!(Test-Path "$env:proxspace_home_path\$pmfolder\client\proxmark3.exe")){

    throw "Main file proxmark3.exe not exists."

    }

    ExecMinGWCmd 'make check'

    $testspass = ($LASTEXITCODE -eq 0)
    
    $global:TestsPassed=$testspass

    if ($testspass) {
      Add-AppveyorTest -Name "make Tests" -Framework NUnit -Filename "make check" -Outcome Passed -Duration "$([System.Environment]::TickCount-$TestTime)"
      Write-Host "make Tests [ OK ]" -ForegroundColor Green
    } else {
      Add-AppveyorTest -Name "make Tests" -Framework NUnit -Filename "make check" -Outcome Failed -Duration "$([System.Environment]::TickCount-$TestTime)"
      Write-Host "make Tests [ ERROR ]" -ForegroundColor Red
      throw "Tests error."
    }
    
    Write-Host "---------- btaddon ----------" -ForegroundColor Yellow
    
    $TestTime=[System.Environment]::TickCount

    ExecMinGWCmd 'make clean;make V=1 PLATFORM_EXTRAS=BTADDON'

    ExecMinGWCmd 'make check'

    $testspass = ($LASTEXITCODE -eq 0)

    $global:TestsPassed=(($global:TestsPassed) -and ($testspass))
    
    if ($testspass) {
      Add-AppveyorTest -Name "BTaddon Tests" -Framework NUnit -Filename "make check" -Outcome Passed -Duration "$([System.Environment]::TickCount-$TestTime)"
      Write-Host "BTaddon Tests [ OK ]" -ForegroundColor Green
    } else {
      Add-AppveyorTest -Name "BTaddon Tests" -Framework NUnit -Filename "make check" -Outcome Failed -Duration "$([System.Environment]::TickCount-$TestTime)"
      Write-Host "BTaddon Tests [ ERROR ]" -ForegroundColor Red
    }
    
    Write-Host "---------- make clean ----------" -ForegroundColor Yellow
    
    ExecMinGWCmd 'make clean'
    
    Write-Host "---------- cmake ----------" -ForegroundColor Yellow
    
    $TestTime=[System.Environment]::TickCount
    
    ExecMinGWCmd 'mkdir -p client/build; cd client/build; cmake -G""MSYS Makefiles"" ..; make VERBOSE=1;'
    
    Write-Host "---------- cmake tests ----------" -ForegroundColor Yellow

    ExecMinGWCmd './tools/pm3_tests.sh --clientbin client/build/proxmark3.exe client'
    
    $testspass = ($LASTEXITCODE -eq 0)

    $global:TestsPassed=(($global:TestsPassed) -and ($testspass))
    
    if ($testspass) {
      Add-AppveyorTest -Name "cmake Tests" -Framework NUnit -Filename "make client/check" -Outcome Passed -Duration "$([System.Environment]::TickCount-$TestTime)"
      Write-Host "cmake Tests [ OK ]" -ForegroundColor Green
    } else {
      Add-AppveyorTest -Name "cmake Tests" -Framework NUnit -Filename "make client/check" -Outcome Failed -Duration "$([System.Environment]::TickCount-$TestTime)"
      Write-Host "cmake Tests [ ERROR ]" -ForegroundColor Red
    }
    
test_script:
- ps: >-
    if ($global:TestsPassed) {
      Write-Host "Tests [ OK ]" -ForegroundColor Green
    } else {
      Write-Host "Tests [ ERROR ]" -ForegroundColor Red
      throw "Tests error."
    }
on_success:
- ps: Write-Host "Build success..." -ForegroundColor Green
on_failure:
- ps: Write-Host "Build error." -ForegroundColor Red
on_finish:
- ps: # $blockRdp = $true; iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/appveyor/ci/master/scripts/enable-rdp.ps1'))
