resources:
  repositories:
    - repository: radialgm
      type: github
      name: enigma-dev/RadialGM
      endpoint: github.com_EnigmaBot
      # ref: refs/pull/138/merge # Use this for testing a co-dependent rgm pr but comment this before merging
      
pr:  # Trigger a build on every PR.
  branches:
    include:
      - '*'  # Must quote since "*" is a YAML reserved character
variables:
- template: azure-vars.yml@radialgm
- group: Test Harness Auth

jobs:
- template: azure-jobs.yml@radialgm
  parameters:
    repo: radialgm
    enigmaref: $(Build.SourceBranch)

- job: TestHarness
  displayName: 'Test Harness'
  variables:
    TEST_HARNESS: "true"
    TRAVIS_OS_NAME: "linux"
    TRAVIS: "true"
    TRAVIS_PULL_REQUEST: $(System.PullRequest.PullRequestNumber)
    TRAVIS_PULL_REQUEST_SHA: $(Build.SourceVersion)
  pool:
    vmImage: 'ubuntu-20.04'
  timeoutInMinutes: 360

  steps:
    - checkout: self
      persistCredentials: true
      
    - script: |
        sudo dpkg --add-architecture i386
        ./CI/install_emake_deps.sh
        ./CI/split_jobs.sh install
        sudo cp ./CI/asound.conf /etc/asound.conf
      displayName: Install Dependencies
    - script: |
        make -j4
        make -j4 emake
      displayName: Build emake
      
    - script: |
        make emake-tests
        ./emake-tests
      displayName: Test emake
   
    - script: |
        ./CI/build_and_run_game.sh
      env:
        _IMGUR_CLIENT_ID: $(imgur_client_id)
        _BOT_COMMENT_TOKEN: $(bot_comment_token)
        _BOT_PUSH_ON_GREEN_TOKEN: $(bot_push_on_green_token)
        _CODECOV_UPLOAD_TOKEN: $(codecov_upload_token)
      displayName: Run Test Harness
      
- job: CrossCompile
  displayName: 'Cross Compile'
  pool:
    vmImage: 'ubuntu-20.04'

  steps:
    - checkout: self
      persistCredentials: true
      path: enigma-dev
    
    - bash: |
        set -e
        cd $(Build.BinariesDirectory)
        ARCH_URL=`curl -s https://mirrors.acm.wpi.edu/archlinux/iso/latest/ | egrep -o 'archlinux-bootstrap-([0-9._]+)-x86_64.tar.gz' | head -n1`
        echo https://mirrors.acm.wpi.edu/archlinux/iso/latest/$ARCH_URL
        curl https://mirrors.acm.wpi.edu/archlinux/iso/latest/$ARCH_URL -o arch.tar.gz
        sudo tar xzf arch.tar.gz
        sudo mount --bind ./root.x86_64/ ./root.x86_64/
        sudo cp -R $(Agent.BuildDirectory)/enigma-dev ./root.x86_64/
        cat << EOF | sudo ./root.x86_64/bin/arch-chroot ./root.x86_64/
        set -e
        echo 'Server = https://mirrors.kernel.org/archlinux/\$repo/os/\$arch' >> /etc/pacman.d/mirrorlist
        pacman-key --init 
        pacman-key --populate archlinux
        pacman -Syu --noconfirm base base-devel sudo go git protobuf grpc yaml-cpp pugixml rapidjson boost xorg-server-xvfb
        echo "%wheel ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
        mkdir -p /home/aur/packages/
        useradd -G wheel aur
        chown -R aur:aur /home/aur
        EOF
      displayName: 'Bootstrap Archlinux'
    
    - bash: |
        set -e
        cd $(Build.BinariesDirectory)
        cat << EOF | sudo ./root.x86_64/bin/arch-chroot ./root.x86_64/
        set -e
        cd /home/aur/packages
        sudo -u aur git clone https://aur.archlinux.org/yay-git.git
        cd yay-git
        sudo -u aur makepkg
        pacman -U --noconfirm  yay-*.tar.*
        sudo -u aur yay -S --noconfirm --builddir /home/aur/packages lcov mingw-w64-gcc mingw-w64-binutils mingw-w64-crt mingw-w64-headers mingw-w64-winpthreads
        EOF
      displayName: 'Build & Install AUR Packages'
    
    - bash: |
        set -e
        cd $(Build.BinariesDirectory)
        cat << EOF | sudo ./root.x86_64/bin/arch-chroot ./root.x86_64/
        set -e
        cp enigma-dev/CI/mirrorlist* /etc/pacman.d/
        cp enigma-dev/CI/asound.conf /etc/asound.conf
        echo "[multilib]
              Include = /etc/pacman.d/mirrorlist
              [mingw32]
              Include = /etc/pacman.d/mirrorlist.mingw32
              [mingw64]
              Include = /etc/pacman.d/mirrorlist.mingw64
              [msys]
              Include = /etc/pacman.d/mirrorlist.msys"  >> /etc/pacman.conf
        curl https://raw.githubusercontent.com/msys2/MSYS2-keyring/master/msys2.gpg -o /usr/share/pacman/keyrings/msys2.gpg
        curl https://raw.githubusercontent.com/msys2/MSYS2-keyring/master/msys2-trusted -o /usr/share/pacman/keyrings/msys2-trusted
        pacman-key --populate msys2
        pacman -Sy --noconfirm wine pulseaudio lib32-libpulse mingw-w64-x86_64-openal mingw-w64-x86_64-dumb mingw-w64-x86_64-libvorbis mingw-w64-x86_64-libogg mingw-w64-x86_64-flac mingw-w64-x86_64-mpg123 mingw-w64-x86_64-libsndfile mingw-w64-x86_64-zlib mingw-w64-x86_64-libffi mingw-w64-x86_64-box2d mingw-w64-x86_64-glew mingw-w64-x86_64-glm mingw-w64-x86_64-alure mingw-w64-x86_64-SDL2 mingw-w64-x86_64-pkg-config
        pacman -S --noconfirm mingw-w64-i686-openal mingw-w64-i686-dumb mingw-w64-i686-libvorbis mingw-w64-i686-libogg mingw-w64-i686-flac mingw-w64-i686-mpg123 mingw-w64-i686-libsndfile mingw-w64-i686-zlib mingw-w64-i686-libffi mingw-w64-i686-box2d mingw-w64-i686-glew mingw-w64-i686-glm mingw-w64-i686-alure mingw-w64-i686-SDL2 mingw-w64-i686-pkg-config
        EOF
      displayName: 'Install MinGW Engine Dependencies'
      
    - bash: |
        set -e
        cd $(Build.BinariesDirectory)
        cat << EOF | sudo ./root.x86_64/bin/arch-chroot ./root.x86_64/
        set -e
        cd enigma-dev
        make emake
        EOF
      displayName: 'Build emake'
      
    - bash: |
        set -e
        cd $(Build.BinariesDirectory)
        cat << EOF | sudo ./root.x86_64/bin/arch-chroot ./root.x86_64/
        set -e
        cd enigma-dev
        ./emake -x MinGW32 -p Win32 -w Win32 -a DirectSound -e DirectShow,WindowsTouch,XInput,MediaControlInterface,FileDropper,ExternalFuncs -o /tmp/test32.exe CommandLine/testing/SimpleTests/clean_exit.sog/
        pulseaudio --start --verbose
        LIBGL_ALWAYS_SOFTWARE=1 WINEDLLOVERRIDES="mscoree,mshtml=" WINEPREFIX=/tmp/.wine32 WINEARCH=win32 xvfb-run wine /tmp/test32.exe
        EOF
      displayName: 'Engine MinGW32'
      
    - bash: |
        set -e
        cd $(Build.BinariesDirectory)
        cat << EOF | sudo ./root.x86_64/bin/arch-chroot ./root.x86_64/
        set -e
        cd enigma-dev
        ./emake -x MinGW64 -p Win32 -w Win32 -a DirectSound -e DirectShow,WindowsTouch,XInput,MediaControlInterface,FileDropper,ExternalFuncs -o /tmp/test64.exe CommandLine/testing/SimpleTests/clean_exit.sog/
        LIBGL_ALWAYS_SOFTWARE=1 WINEDLLOVERRIDES="mscoree,mshtml=" WINEPREFIX=/tmp/.wine64 WINEARCH=win64 xvfb-run wine /tmp/test64.exe
        EOF
      displayName: 'Engine MinGW64'
      
- job: Android
  displayName: 'Android'
  variables:
    TEST_HARNESS: "true"
    TRAVIS_OS_NAME: "linux"
    TRAVIS: "true"
    TRAVIS_PULL_REQUEST: $(System.PullRequest.PullRequestNumber)
    TRAVIS_PULL_REQUEST_SHA: $(Build.SourceVersion)
    COMPILER: Android 
    GRAPHICS: OpenGLES2 
    PLATFORM: SDL
    MODE: Compile
    AUDIO: None
    COLLISION: None 
    NETWORK: None 
    WIDGETS: None 
    EXTENSIONS: "None"
    OUTPUT: /tmp/test-android-game.apk
    ANDROID_API_LEVEL: 29 
    ANDROID_BUILD_TOOLS_VERSION: 29.0.3
  pool:
    vmImage: 'ubuntu-20.04'

  steps:
    - checkout: self
      persistCredentials: true
      path: enigma-dev
      
    - script: echo "y" | $ANDROID_HOME/tools/bin/sdkmanager --channel=1 --install "tools" "platform-tools" "extras;google;google_play_services" "extras;google;m2repository" "extras;android;m2repository" "build-tools;29.0.3" "platforms;android-29" "ndk;22.0.6917172"
      displayName: 'Install Android Dependencies'
      
    - script: git clone --depth 1 https://github.com/enigma-dev/enigma-android.git android
      displayName: 'Clone Android Repo'
      
    - script: ./CI/install_emake_deps.sh
      displayName: 'Install Emake Dependencies'
      
    - script: make -j4 emake
      displayName: 'Build Emake'

    - script: ./ci-build.sh
      displayName: 'Build Game'
