#!/bin/bash
. `dirname $0`/common.sh

# Set debug mode
set -x
set -v

# build_package
# -------------
#
# Build the package using the last Eigen release (3.2) which is not
# available as a Debian package on Ubuntu 12.04.
build_package()
{
    echo "--> Building package..."

    cd "$build_dir"

    if [[ ";${DO_COVERAGE_ON_BRANCH};" == *";${CI_BRANCH};"* ]]; then
      cmake "$root_dir" -DCMAKE_INSTALL_PREFIX="$install_dir"	\
        -DCMAKE_CXX_FLAGS="--coverage"				\
        -DCMAKE_EXE_LINKER_FLAGS="--coverage"			\
        -DCMAKE_MODULE_LINKER_FLAGS="--coverage"		\
        ${CMAKE_ADDITIONAL_OPTIONS}
    else
      cmake "$root_dir" -DCMAKE_INSTALL_PREFIX="$install_dir"	\
        ${CMAKE_ADDITIONAL_OPTIONS}
    fi

    ${MAKE_PREFIX} make
    make install

    ALLOW_TESTSUITE_FAILURE=${ALLOW_TESTSUITE_FAILURE:-false}
    make test || ${ALLOW_TESTSUITE_FAILURE}

    if [[ ";${DO_CPPCHECK_ON_BRANCH};" == *";${CI_BRANCH};"* ]]; then
      cppcheck --quiet --enable=all \
        -I $root_dir/src -I $root_dir/tests -I $root_dir/include \
        -I $root_dir/tests/shared-tests \
        -I $build_dir/include -I $install_dir/include \
        -i $build_dir/CMakeFiles \
        $root_dir || true
    fi
}

# debian_build_package
# --------------------
#
# Use git-buildpackage and pbuilder to build the package in a sid
# sandbox.
debian_build_package()
{
    export GNUPGHOME="$root_dir/.travis/.gnupg"
    export NAME="Thomas Moulard (Travis Automatic Builds)"
    export DEBEMAIL="thomas.moulard+travis@gmail.com"

    echo "--> Building Debian package..."
    cd "$root_dir"

    buildNumber=$(git rev-list \
      $(git describe --tags --match "debian/*" --abbrev=0)..HEAD | wc -l) \
      || buildNumber=1
    dch --force-distribution --distribution ${DIST} \
        --local ppa$buildNumber+$DIST "Travis automatic build"

    echo "debian/changelog first line:"
    head -n 1 debian/changelog

    git add debian/changelog
    git commit -m "Travis automatic commit"

    ${SUDO_CMD} chmod -R 777 /var/cache/pbuilder/ccache

    # If orig tarball exists, delete it.
    rm -f "$build_dir/export/*_*.orig.tar*"
    git-buildpackage					\
      --git-submodules				\
      --git-no-pristine-tar				\
      --git-ignore-branch				\
      --git-debian-branch=HEAD			\
      --git-export-dir="$build_dir/export"		\
      --git-tag					\
      --git-upstream-branch=master			\
      --git-dist=${DIST}				\
      --git-pbuilder					\
      --git-force-create				\
      --git-ignore-new				\
      --git-retag					\
      -p\"gpg\\ --passphrase\\ ${GNUPG_PASSPHRASE}\" \
      -k${DEBSIGN_KEYID} || exit 1


    git-buildpackage			\
      --git-submodules		\
      --git-no-pristine-tar		\
      --git-debian-branch=HEAD	\
      --git-ignore-branch		\
      --git-export-dir="$build_dir/export" \
      --git-tag			\
      --git-upstream-branch=master	\
      --git-dist=${DIST}		\
      --git-ignore-new		\
      --git-retag			\
      -p\"gpg --passphrase ${GNUPG_PASSPHRASE}\" \
      -k${DEBSIGN_KEYID} \
      -S -sa || exit 1
}


# setup_ros_build_environment
# ---------------------------
#
# Source ROS setup scripts if they exist
setup_ros_build_environment()
{
  if [ -e /opt/ros/${ROS_DISTRO}/setup.sh ]; then
    . /opt/ros/${ROS_DISTRO}/setup.sh
  fi
  CATKIN_DEP_WORKSPACE=/tmp/_ci/catkin_dep_ws
  if [ -e ${CATKIN_DEP_WORKSPACE}/devel/setup.sh ]; then
    . ${CATKIN_DEP_WORKSPACE}/devel/setup.sh
  fi
  # Limit the number of parallel jobs when running catkin_make
  PARALLEL_JOBS=${PARALLEL_JOBS:-1}
  export ROS_PARALLEL_JOBS="-j ${PARALLEL_JOBS}"
}

# build_catkin_package
# --------------------
#
# build all the packages using catkin_make.
# Also check the installation (catkin_make install)
# and check whether the catkin package is well written (catkin_lint)
build_catkin_package()
{
    # Main package workspace
    CATKIN_WORKSPACE=$build_dir/..
    ln -s $root_dir/.. $CATKIN_WORKSPACE/src
    cd $CATKIN_WORKSPACE/src
    catkin_init_workspace

    cd $CATKIN_WORKSPACE
    catkin_make
    for pack in `ls -d ./src/*/ ./src/*/*/`; do
    if test -f $pack/package.xml; then
      rosdoc_lite $pack
    fi
    done
    catkin_make install

    # run catkin_lint on every directory.
    ALLOW_CATKINLINT_FAILURE=${ALLOW_CATKINLINT_FAILURE:-false}
    catkin_lint `ls -d ./src/*/ ./src/*/*/`  || ${ALLOW_CATKINLINT_FAILURE}
}

setup_ros_build_environment
# Realize a normal build in all branches except the one containing a
# debian/ folder.
if [ -d debian ]; then
    if `test x${DIST} = x`; then
      echo "distribution is not set, skipping this build"
      exit 0
    fi
    echo "Target distribution: ${DIST}"
    debian_build_package
else
    if [ ! x${DIST} = x ]; then
      echo "skipping this build"
      exit 0
    fi
    # checking if it is a ros folder. Taking appropriate measure.
    #The current repository is a package
    if  [[ -n $(find .  -maxdepth 2 -name package.xml) ]]; then
      build_catkin_package
    else
      build_package
    fi
fi

# End debug mode
set +v
set +x
