# Much of this is borrowed from node-sqlite3 and node-icu-bidi
sudo: false

language: cpp

git:
  depth: 10

addons:
  apt:
    sources:
    - ubuntu-toolchain-r-test
    - llvm-toolchain-precise-3.5
    packages:
    - clang-3.5
    - gcc-5-multilib
    - g++-5-multilib
    - linux-libc-dev:i386
    - libxml2-dev:i386
    - zlib1g-dev:i386

## don't re-build for tags so that [publish binary] is not re-run
## https://github.com/travis-ci/travis-ci/issues/1532
#branches:
#  except:
#    - /^[0-9]+[.][0-9]+[.][0-9]+/


matrix:
  include:
     # Linux
     - os: linux
       compiler: clang
       env: NODE_VERSION="5.0.0" PUBLISHABLE=false
     - os: linux
       env: NODE_VERSION="5.0.0" BUILD_X86=true # node abi 47
     # Test in release mode, not just debug mode.
     - os: linux
       compiler: clang
       env: NODE_VERSION="5.0.0" PUBLISHABLE=false RELEASE=true
#     - os: linux
#       compiler: clang
#       env: NODE_VERSION="4.2.1" PUBLISHABLE=false
     - os: linux
       env: NODE_VERSION="4.2.1" BUILD_X86=true # node abi 46
#     - os: linux
#       compiler: clang
#       env: NODE_VERSION="iojs-v3.3.1" PUBLISHABLE=false
     - os: linux
       env: NODE_VERSION="iojs-v3.3.1" BUILD_X86=true # node abi 45
#     - os: linux
#       compiler: clang
#       env: NODE_VERSION="iojs-v2.4.0" PUBLISHABLE=false
     - os: linux
       env: NODE_VERSION="iojs-v2.4.0" BUILD_X86=true # node abi 44
# disabled because libphp5-embed package is not on whitelist yet
# https://github.com/travis-ci/apt-package-whitelist#package-approval-process
#     # test building against external libphp5
#     - os: linux
#       env: NODE_VERSION="4.2.1" EXTERNAL_LIBPHP=true PUBLISHABLE=false
#       addons:
#         apt:
#            sources: [ 'libphp5-embed:i386', 'php5-dev:i386' ]
#            packages: [ 'libphp5-embed', 'php5-dev']
     # OS X
     - os: osx
       compiler: clang
       env: NODE_VERSION="5.0.0" # node abi 47
     # Test in release mode, not just debug mode.
     - os: osx
       compiler: clang
       env: NODE_VERSION="5.0.0" PUBLISHABLE=false RELEASE=true
     - os: osx
       compiler: clang
       env: NODE_VERSION="4.2.1" # node abi 46
     - os: osx
       compiler: clang
       env: NODE_VERSION="iojs-v3.3.1" # node abi 45
     - os: osx
       compiler: clang
       env: NODE_VERSION="iojs-v2.4.0" # node abi 44

before_install:
- export PUBLISHABLE=${PUBLISHABLE:-true}
- export RELEASE=${RELEASE:-false}
# ensure that published builds are not built in debug mode
- if test "$PUBLISHABLE" = "true" ; then if scripts/validate_tag.sh --check ; then RELEASE=true ; fi ; fi
- if test "$TRAVIS_OS_NAME $CC" = "linux gcc" ; then
    export CXX="g++-5" ;
    export CC="gcc-5" ;
  fi
- if test "$TRAVIS_OS_NAME $CC" = "linux clang" ; then
    export CXX="clang++-3.5" ;
    export CC="clang-3.5" ;
  fi
- if test "$TRAVIS_OS_NAME $CC" = "osx clang" ; then
    export MACOSX_DEPLOYMENT_TARGET=10.7 ;
  fi
# we need to install libicu on os x
- |
  if test "$TRAVIS_OS_NAME" = "osx"; then
    brew update | head ;
    brew install icu4c ;
  fi
# check if tag exists and matches package.json
- scripts/validate_tag.sh
- echo $NODE_VERSION
- rm -rf ~/.nvm/ && git clone --depth 1 https://github.com/creationix/nvm.git ~/.nvm
- source ~/.nvm/nvm.sh
- nvm install $NODE_VERSION
- nvm use $NODE_VERSION
- if test "$NODE_VERSION" = "0.8.28" ; then npm install -g npm@1.4.28 ; fi
# Work around ECONNRESET issues on travis:
# https://github.com/npm/npm/issues/7558#issuecomment-97528256
# https://github.com/npm/npm/issues/7699#issuecomment-93091111
- npm install -g npm@latest
- npm config set spin=false
- npm config set loglevel=http
- node --version
- npm --version

install:
# put node-pre-gyp on path
- export PATH=./node_modules/.bin/:$PATH

before_script:
# get commit message
- export COMMIT_MESSAGE=$(git show -s --format=%B $TRAVIS_COMMIT | tr -d '\n')

script:
- node --version
- npm --version
- DEBUG=''
- if test "$RELEASE" = "false"; then DEBUG='--debug --libicu=external' ; fi
- if false ; then export V=1 ; fi # Useful when debugging build/link failures.
- if test -z "$EXTERNAL_LIBPHP" ; then npm install --build-from-source $DEBUG; else npm install --build-from-source --libphp5=external $DEBUG; fi
- node-pre-gyp package testpackage
# OSX malloc debugging
- |
  if test "$TRAVIS_OS_NAME" = "osx"; then
    echo DISABLED export MallocStackLogging=1
    echo DISABLED export MallocStackLoggingNoCompact=1
    export MallocScribble=1
    export MallocPreScribble=1
    export MallocGuardEdges=1
    export MallocCheckHeapStart=1
    export MallocCheckHeapEach=1
    export DYLD_INSERT_LIBRARIES=/usr/lib/libgmalloc.dylib
  fi
- |
  if npm test ; then
    echo "Test successful.  Publishable=$PUBLISHABLE"
  else
    echo "Test failed, refusing to publish."
    PUBLISHABLE=false
    false
  fi
- |
  if test "$TRAVIS_OS_NAME" = "osx"; then
    unset MallocStackLogging
    unset MallocStackLoggingNoCompact
    unset MallocScribble
    unset MallocPreScribble
    unset MallocGuardEdges
    unset MallocCheckHeapStart
    unset MallocCheckHeapEach
    unset DYLD_INSERT_LIBRARIES
  fi
- export PUBLISH=false
# Normal case: tagged builds trigger 32-bit and 64-bit x86 builds
- if scripts/validate_tag.sh --check ; then PUBLISH=true; fi
# Workaround to trigger a manual x86 build
- if test "$TRAVIS_BRANCH" == "x86" -a  "${COMMIT_MESSAGE#*'[publish binary]'}" != "$COMMIT_MESSAGE"; then PUBLISH=true; fi
- if test "$PUBLISH,$PUBLISHABLE" = "true,true"; then npm install core-js@1.1.4 && npm install request@2.64.0 && npm run gh-publish && if test -n "$BUILD_X86"; then scripts/publish_x86.sh ; fi ; fi
