parameters:
  name: ''
  vmImage: ''
  arch: '' # hxcpp's target arch (HXCPP_Mxx), can be '32' or '64'
  platform: '' # can be linux64, mac, windows, or windows64
  listPlatform32: ['linux32', 'windows']
  listPlatform64: ['linux64', 'mac', 'windows64']

jobs:
  - job: ${{ parameters.name }}
    pool:
      vmImage: ${{ parameters.vmImage }}
    variables:
      ${{ if startsWith(parameters.vmImage, 'ubuntu-') }}:
        libExt: .dso
        sep: '/'
      ${{ if contains(parameters.vmImage, 'mac') }}:
        libExt: .dylib
        sep: '/'
      ${{ if contains(parameters.vmImage, 'win') }}:
        libExt: .dll
        sep: '\'
    steps:
      - ${{ if startsWith(parameters.vmImage, 'ubuntu-') }}:
        - script: |
            set -ex
            sudo rm -R /var/lib/mysql/
            # mariadb
            curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup > mariadb_repo_setup
            curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup.sha256 > mariadb_repo_setup.sha256
            sha256sum --check mariadb_repo_setup.sha256
            sudo bash mariadb_repo_setup
            sudo apt-get update -qqy
            sudo apt-get remove -qqy mysql-common
            sudo apt-get autoremove -qqy
            sudo apt-get install -qqy mariadb-server
            # remaining packages
            sudo apt-get install -qqy gcc-multilib g++-multilib
          displayName: Install dependencies
        - script: |
            set -ex
            sudo systemctl start mysql
            sudo mysql -u root -e "create database hxcpp; grant all privileges on hxcpp.* to hxcpp@localhost identified by 'hxcpp'; flush privileges;"
          displayName: Configure MariaDB
      - template: install-neko-snapshot.yaml
        parameters:
          platform: ${{ parameters.platform }}
      - template: install-haxe-snapshot.yaml
        parameters:
          platform: ${{ parameters.platform }}
      - script: |
          set -ex
          mkdir -p ~/haxelib
          haxelib setup ~/haxelib
          haxelib install utest
          haxelib dev hxcpp $(Build.SourcesDirectory)
          haxelib list
        displayName: Install Haxe libraries
      - script: haxe compile.hxml
        workingDirectory: tools/run
        displayName: Build run.n
      - script: haxe compile.hxml
        workingDirectory: tools/hxcpp
        displayName: Build hxcpp
      - script: haxe compile-cppia.hxml
        workingDirectory: project
        displayName: Build cppia

      # cffi
      - script: haxelib run hxcpp build.xml -debug -DHXCPP_M${{parameters.arch}}
        workingDirectory: test/cffi/project
        displayName: Build cffi project
      - bash: |
          set -ex
          haxe compile.hxml      -debug -D HXCPP_M${{parameters.arch}}
          haxe compile-utf8.hxml -debug -D HXCPP_M${{parameters.arch}}
          haxe compile-neko.hxml -debug -D HXCPP_M${{parameters.arch}}
        workingDirectory: test/cffi
        displayName: cffi test - Compile
      - script: bin$(sep)cpp$(sep)TestMain-debug
        workingDirectory: test/cffi
        displayName: cffi test - Run
      - script: bin$(sep)cpp-utf8$(sep)TestMain-debug
        workingDirectory: test/cffi
        displayName: cffi-utf8 test - Run
      - ${{ if or(and(containsValue(parameters.listPlatform32, parameters.platform), eq(parameters.arch, '32')), and(containsValue(parameters.listPlatform64, parameters.platform), eq(parameters.arch, '64'))) }}:
        - bash: cp test/cffi/project/ndll/*/prime$(libExt) test/cffi/bin/neko/prime.ndll
          displayName: Copy prime.ndll
        - script: neko TestMain.n
          workingDirectory: test/cffi/bin/neko
          displayName: cffi-neko test - Run

      # haxe
      - script: haxe compile.hxml -debug -D HXCPP_M${{parameters.arch}}
        workingDirectory: test/haxe
        displayName: haxe test - Compile
      - script: bin$(sep)TestMain-debug
        workingDirectory: test/haxe
        displayName: haxe test - Run

      # telemetry, should work in debug and non-debug modes
      - script: haxe compile.hxml -debug -D HXCPP_M${{parameters.arch}}
        workingDirectory: test/telemetry
        displayName: telemetry test (debug) - Compile
      - script: bin$(sep)TestMain-debug
        workingDirectory: test/telemetry
        displayName: telemetry test (debug) - Run
      - script: haxe compile.hxml -D HXCPP_M${{parameters.arch}}
        workingDirectory: test/telemetry
        displayName: telemetry test - Compile
      - script: bin$(sep)TestMain
        workingDirectory: test/telemetry
        displayName: telemetry test - Run

      # std
      - script: haxe compile${{parameters.arch}}.hxml
        workingDirectory: test/std
        displayName: std test - Compile
      - script: cpp${{parameters.arch}}$(sep)Test
        workingDirectory: test/std
        displayName: std test - Run

      # debugger
      - script: haxe compile.hxml -D HXCPP_M${{parameters.arch}}
        workingDirectory: test/debugger
        displayName: debugger test - Compile
      - script: bin$(sep)App-debug
        workingDirectory: test/debugger
        displayName: debugger test - Run

      # native
      - script: haxe compile.hxml -D HXCPP_M${{parameters.arch}}
        workingDirectory: test/native
        displayName: native test - Compile
      - script: bin$(sep)Native
        workingDirectory: test/native
        displayName: native test - Run

      # haxe unit test
      - bash: |
          set -ex
          HAXE_VERSION=`haxe -version`
          HAXE_HASH=`[[ "$HAXE_VERSION" =~ ^.+\+(.+) ]] && echo ${BASH_REMATCH[1]}`
          git clone -q https://github.com/HaxeFoundation/haxe.git
          pushd haxe
          git reset --hard "$HAXE_HASH"
          git clean -fdx
          popd
        displayName: haxe unit test - Clone haxe repo
      - script: haxelib install compile-cpp.hxml --always
        workingDirectory: haxe/tests/unit
        displayName: haxe unit test - Install haxelibs
      - script: haxe compile-cpp.hxml -D HXCPP_M${{parameters.arch}} -D no_http
        workingDirectory: haxe/tests/unit
        displayName: haxe unit test - Compile
      - script: bin$(sep)cpp$(sep)TestMain-debug
        workingDirectory: haxe/tests/unit
        displayName: haxe unit test - Run