language: cpp

matrix:
  include:
    - os: linux
      compiler: gcc
      dist: trusty
      addons:
        apt:
          sources:
            - ubuntu-toolchain-r-test
          packages:
            - g++-5
      env:
        - CC_COMPILER=gcc-5 CXX_COMPILER=g++-5

    - os: linux
      compiler: gcc
      dist: trusty
      addons:
        apt:
          sources:
            - ubuntu-toolchain-r-test
          packages:
            - g++-5
      env:
        - CC_COMPILER=gcc-5 CXX_COMPILER=g++-5 PERL_BINDING=1

    - os: linux
      compiler: gcc
      dist: trusty
      addons:
        apt:
          sources:
            - ubuntu-toolchain-r-test
          packages:
            - g++-5
      env:
        - CC_COMPILER=gcc-5 CXX_COMPILER=g++-5 PYTHON_BINDING=1 PYTHON=3.5

    - os: linux
      compiler: gcc
      dist: bionic
      addons:
        apt:
          sources:
            - ubuntu-toolchain-r-test
          packages:
            - g++-5
      env:
        - CC_COMPILER=gcc-5 CXX_COMPILER=g++-5 JAVA_BINDING=1

    - os: osx
      compiler: clang

    - os: osx
      compiler: clang
      env:
        - PERL_BINDING=1

    - os: osx
      compiler: clang
      env:
        - PYTHON_BINDING=1 PYTHON=3.6

    - os: osx
      compiler: clang
      osx_image: xcode11.2
      env:
        - JAVA_BINDING=1

before_install:
  - |
    if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then
      HOMEBREW_NO_AUTO_UPDATE=1 brew install swig
      brew link --force swig

      if [[ "$PERL_BINDING" == "1" ]]; then
        HOMEBREW_NO_AUTO_UPDATE=1 brew install perl
        perl --version
      fi

    elif [[ "$TRAVIS_OS_NAME" == "linux" ]]; then
      sudo apt-get update
      sudo apt-get install -qq swig

      if [[ "$PERL_BINDING" == "1" ]]; then
        sudo apt-get install perl
        alias perl="/usr/bin/perl"
        perl --version
      fi

      # this is only working for python3.5
      if [[ "$PYTHON_BINDING" == "1" ]]; then
        sudo apt-get install python$PYTHON
        sudo apt-get install python$PYTHON-dev
        alias python="/usr/bin/python$PYTHON"
        python --version
      fi

      if [[ "$JAVA_BINDING" == "1" ]]; then
        sudo apt-get install openjdk-8-jdk
        javac -version
      fi
    fi

install:
  - if [[ "${CXX_COMPILER}" != "" ]]; then export CXX=${CXX_COMPILER}; export CXX_FOR_BUILD=${CXX_COMPILER}; fi
  - if [[ "${CC_COMPILER}" != "" ]]; then export CC=${CC_COMPILER}; export CC_FOR_BUILD=${CC_COMPILER}; fi

script:
  - |
    mkdir build
    cd build
    cmake -DBUILD_BINDINGS_PERL=$PERL_BINDING -DBUILD_BINDINGS_PYTHON=$PYTHON_BINDING -DPYTHON_VERSION=$PYTHON -DBUILD_BINDINGS_JAVA=$JAVA_BINDING ..
    make
    ./core/test_core

before_deploy:
  - |
    cd $TRAVIS_BUILD_DIR

    VERSION=$(head -1 version.txt)
    VERSION=${VERSION//./_}

    if [[ "$PERL_BINDING" == "1" ]]; then
      BUNDLE_NAME=sourcetraildb_perl_${VERSION}-${TRAVIS_OS_NAME}-64bit-${TRAVIS_COMPILER}
      
      mkdir -p $BUNDLE_NAME
      cp build/bindings_perl/sourcetraildb.* $BUNDLE_NAME
      
    elif [[ "$PYTHON_BINDING" == "1" ]]; then
      PYTHON_VERSION=${PYTHON//.}
      PYTHON_VERSION=${PYTHON_VERSION:0:2}
      BUNDLE_NAME=sourcetraildb_python${PYTHON_VERSION}_${VERSION}-${TRAVIS_OS_NAME}-64bit-${TRAVIS_COMPILER}
      
      mkdir -p $BUNDLE_NAME
      cp build/bindings_python/sourcetraildb.py $BUNDLE_NAME
      cp build/bindings_python/_sourcetraildb* $BUNDLE_NAME/_sourcetraildb.so
      
    elif [[ "$JAVA_BINDING" == "1" ]]; then
      BUNDLE_NAME=sourcetraildb_java_${VERSION}-${TRAVIS_OS_NAME}-64bit-${TRAVIS_COMPILER}
      
      mkdir -p $BUNDLE_NAME
      cp build/bindings_java/SourcetrailDB.jar $BUNDLE_NAME
      cp build/bindings_java/lib_sourcetraildb* $BUNDLE_NAME
      
    else
      BUNDLE_NAME=sourcetraildb_core_${VERSION}-${TRAVIS_OS_NAME}-64bit-${TRAVIS_COMPILER}
      
      mkdir -p $BUNDLE_NAME/include
      mkdir -p $BUNDLE_NAME/lib
      cp build/core/*.a $BUNDLE_NAME/lib
      cp core/include/* $BUNDLE_NAME/include
      cp build/core/include/* $BUNDLE_NAME/include
      
    fi

    mkdir -p $BUNDLE_NAME/license
    cp LICENSE.txt $BUNDLE_NAME/license/license_sourcetraildb.txt
    cp external/catch/license_catch.txt $BUNDLE_NAME/license/
    cp external/cpp_sqlite/license_cpp_sqlite.txt $BUNDLE_NAME/license/
    cp external/json/license_json.txt $BUNDLE_NAME/license/

    zip -r $BUNDLE_NAME $BUNDLE_NAME

deploy:
  provider: releases
  api_key:
    secure: UwuiE6SrjorpBr1CzTRadX1zq83hwbnur+MvzBueqEHM7Yyu5Y0xMkwchRlX41HzMuSkC0azM0S8GfqJT1E7pIrqgJGvFbXR3t9ZG8HtvrcM4PvTS8b0G0d8aWHfTP2Zv+PAK3MMnYtxmOFPs4IaqO1m+imjQWtpvCRG5tvc4DNIPAMh0S8LCjxUdz35BYAM4pwM7WB9Qt+YhbyP1h3oSQ7qtr1fZYCC0H4G1cjVBLeP9g66/tfg0MIQVf1wxU8KF3H6esilPhFDCnsBYcnYrKyJN9OkMXZvLoxsjqQvTlsSNO2wIQ32dW8fk+1VFebWjN3RnNUV2Zb3wdz2y6e6A08J8T/+QrJ003eUJAGassQEgsuQ85ldcbylbKCVdi+iA+W6pKSJoHIlyr2KQG77OZ+q3obvY09IMNX2qVnUJKG8g9vZHKk8TlvUtIDODUxqJ/XUVwG5aKiUPuE5UMeLnZlO7cWpABb28BjXGehLCDtqg2eu39n+N9hyEjmsJLcOuS03+C55YrjJHW09qZomorMyIa//4jxSAOU9Iah7cRzPGUl/eSwQ2kf0OGWfySd2PreOThWepH0ymvG9htlTUSMM4wRuKnNAmG7NY6TykttJY2KsTLpki+P4MFBlSvL0VvoHQintK69udSVeUrJaGx508w8V59AdZwJjMxhjOXw=
  file_glob: true
  file: $TRAVIS_BUILD_DIR/*.zip
  overwrite: true
  skip_cleanup: true
  on:
    tags: true
    repo: CoatiSoftware/SourcetrailDB
