parameters:
  name: 'BuildWindows'
  vmImage: 'windows-2019'
  arch: '64' # or '32'

jobs:
  - job: ${{ parameters.name }}
    pool:
      vmImage: ${{ parameters.vmImage }}
    variables:
      OPAMYES: 1
      ADD_REVISION: 1
      CYG_MIRROR: http://mirrors.kernel.org/sourceware/cygwin/
      ${{ if eq(parameters.arch, '64') }}:
        ARCH: 64
        MINGW_ARCH: x86_64
        CYGWIN_SETUP: https://cygwin.com/setup-x86_64.exe
        CYG_ROOT: C:/cygwin64
      ${{ if eq(parameters.arch, '32') }}:
        ARCH: 32
        MINGW_ARCH: i686
        CYGWIN_SETUP: https://cygwin.com/setup-x86.exe
        CYG_ROOT: C:/cygwin
    steps:
      - checkout: self
        submodules: recursive
      - powershell: |
          Set-PSDebug -Trace 1
          choco install --no-progress nsis.portable --version 3.02 -y
          choco install --no-progress curl wget 7zip.portable -y
        displayName: Install dependencies
      - template: install-neko-snapshot.yaml
        parameters:
          platform: windows
      - powershell: |
          Set-PSDebug -Trace 1
          curl.exe -fsSL -o cygwin-setup.exe --retry 3 $(CYGWIN_SETUP)
          Start-Process -FilePath "cygwin-setup.exe" -ArgumentList "-B -q -R $(CYG_ROOT) -l C:/tmp -s $(CYG_MIRROR) -P default -P make -P git -P zlib-devel -P rsync -P patch -P diffutils -P curl -P unzip -P tar -P m4 -P perl -P libpcre-devel -P mingw64-$(MINGW_ARCH)-zlib -P mingw64-$(MINGW_ARCH)-gcc-core -P mingw64-$(MINGW_ARCH)-pcre" -Wait
          curl.exe -fsSL -o "opam.tar.xz" --retry 3 https://github.com/fdopen/opam-repository-mingw/releases/download/0.0.0.2/opam$(ARCH).tar.xz
          & "$(CYG_ROOT)/bin/bash.exe" @('-lc', 'echo "$OLDPWD"')
          & "$(CYG_ROOT)/bin/bash.exe" @('-lc', 'cd "$OLDPWD" && tar -xf opam.tar.xz')
          & "$(CYG_ROOT)/bin/bash.exe" @('-lc', 'cd "$OLDPWD" && bash opam$(ARCH)/install.sh')
          & "$(CYG_ROOT)/bin/bash.exe" @('-lc', 'opam init mingw "https://github.com/fdopen/opam-repository-mingw.git#opam2" --comp 4.07.0+mingw$(ARCH)c --switch 4.07.0+mingw$(ARCH)c --auto-setup --yes 2>&1')
          & "$(CYG_ROOT)/bin/bash.exe" @('-lc', 'opam update --yes 2>&1')
          & "$(CYG_ROOT)/bin/bash.exe" @('-lc', 'cd "$OLDPWD" && opam pin add haxe . --kind=path --no-action --yes 2>&1')
          & "$(CYG_ROOT)/bin/bash.exe" @('-lc', 'opam install haxe --deps-only --yes 2>&1')
          & "$(CYG_ROOT)/bin/bash.exe" @('-lc', 'opam list')
          & "$(CYG_ROOT)/bin/bash.exe" @('-lc', 'ocamlopt -v')
        displayName: Install OCaml and OCaml libraries
      - powershell: Write-Host "##vso[task.prependpath]${env:CYG_ROOT}/usr/$(MINGW_ARCH)-w64-mingw32/sys-root/mingw/bin"
        displayName: Expose mingw dll files
      - powershell: |
          Set-PSDebug -Trace 1
          & "$(CYG_ROOT)/bin/bash.exe" @('-lc', 'cd "$OLDPWD" && opam config exec -- make -s -f Makefile.win libs prebuild 2>&1')
          & "$(CYG_ROOT)/bin/bash.exe" @('-lc', 'cd "$OLDPWD" && opam config exec -- make -s -f Makefile.win -j`nproc` haxe 2>&1')
          & "$(CYG_ROOT)/bin/bash.exe" @('-lc', 'cd "$OLDPWD" && opam config exec -- make -s -f Makefile.win haxelib 2>&1')
          & "$(CYG_ROOT)/bin/bash.exe" @('-lc', 'cd "$OLDPWD" && opam config exec -- make -f Makefile.win echo_package_files package_bin package_installer_win package_choco 2>&1')
          dir out
          & "$(CYG_ROOT)/bin/bash.exe" @('-lc', 'cd "$OLDPWD" && cygcheck ./haxe.exe')
          & "$(CYG_ROOT)/bin/bash.exe" @('-lc', 'cd "$OLDPWD" && cygcheck ./haxelib.exe')
        displayName: Build Haxe
      - task: PublishPipelineArtifact@0
        inputs:
          artifactName: 'win$(ARCH)Binaries'
          targetPath: out