os: Visual Studio 2013
version: "{build}"
build: off
skip_tags: true

environment:
  matrix:
    - arch: x86_64
      bits: 64
      nodejs_version: 7
    - arch: i686
      bits: 32
      nodejs_version: 7

platform:
  - x64
  # - x86

install:
  - ps: Install-Product node $env:nodejs_version $env:platform
  - npm -g i npm@latest
  - set PATH=%APPDATA%\npm;%APPVEYOR_BUILD_FOLDER%\node_modules\.bin;%PATH%
  - npm i --ignore-scripts
  - for /f %%i in ('node -v') do set exact_nodejs_version=%%i
  - git submodule update --init --recursive
  - ps: |
      [Environment]::SetEnvironmentVariable("MSYS2_PATH_TYPE", "inherit", "Machine")
      $arch = $env:Arch
      $bits = $env:Bits
      $msys = "msys$bits"
      $mingw = "mingw$bits"
      $mingwUpper = $mingw.ToUpper()
      $buildFolder = $env:APPVEYOR_BUILD_FOLDER
      $posixBuildFolder = $buildFolder -Replace '\\', '/'
      $env:PATH="C:\$msys\$mingw\bin;C:\$msys\usr\bin;$env:PATH"
      function bash($command, $dieOnError = $true) {
        ""
        Write-Host $command
        & "C:\$msys\usr\bin\bash" --login -c "ARCH=$arch MSYSTEM=$mingwUpper . /etc/profile && cd $posixBuildFolder && $command"
        if ($LASTEXITCODE -eq 0) {
          Write-Host "'$command' succeeded. While its output might be red, it exited with '0'." -ForegroundColor Green
        } else {
          Write-Host "'$command' failed with exit code $LASTEXITCODE! " -ForegroundColor Red -NoNewline
          if ($dieOnError) {
            Write-Host "Exiting." -ForegroundColor Red
            exit $LASTEXITCODE
          } else {
            "Continuing."
          }
        }
      }
      # 32-bit MSYS2 is not offered on AppVeyor yet, so we need to install it.
      if ($arch -eq "i686") {
        Write-Host "Installing 32-bit MSYS2..." -ForegroundColor Cyan
        # download installer
        $zipPath = "$($env:USERPROFILE)\msys2-i686-latest.tar.xz"
        $tarPath = "$($env:USERPROFILE)\msys2-i686-latest.tar"
        "Downloading MSYS2 installation package..."
        (New-Object Net.WebClient).DownloadFile('http://repo.msys2.org/distrib/msys2-i686-latest.tar.xz', $zipPath)
        Write-Host "Unzipping $zipPath..."
        7z x $zipPath -y -o"$env:USERPROFILE" | Out-Null
        Write-Host "Untaring $tarPath to C:\msys32\..."
        7z x $tarPath -y -oC:\ | Out-Null
        del $zipPath
        del $tarPath
        Write-Host "32-bit MSYS2 installed" -ForegroundColor Green
        bash "pacman --sync --noconfirm --needed pacman pacman-mirrors"
        bash "pacman --sync --noconfirm --needed VCS"
        bash "pacman --sync --noconfirm --needed base-devel"
        bash "pacman --sync --noconfirm --needed msys2-devel"
        bash "pacman --sync --noconfirm --needed mingw-w64-$arch-toolchain"
      }
      bash "cd libuiohook && ./bootstrap.sh && ./configure"
  - node build %exact_nodejs_version%

on_success:
 - for %%i in (prebuilds\*) do appveyor PushArtifact %%i
