#!/bin/bash

set -e

script/bootstrap

echo Building LibSass $MAKE_OPTS

# export this path right here (was in script/spec before)
export SASS_LIBSASS_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )"/../ && pwd )"

# use some defaults if not running under travis ci
if [ "x$CONTINUOUS_INTEGRATION" == "x" ]; then export CONTINUOUS_INTEGRATION=true; fi
if [ "x$TRAVIS_BUILD_DIR" == "x" ]; then export TRAVIS_BUILD_DIR=$(pwd); fi
if [ "x$SASS_SASSC_PATH" == "x" ]; then export SASS_SASSC_PATH=$(pwd)/sassc; fi
if [ "x$SASS_SPEC_PATH" == "x" ]; then export SASS_SPEC_PATH=$(pwd)/sass-spec; fi

# try to get the os name from uname (and filter via perl - probably not the most portable way?)
if [ "x$TRAVIS_OS_NAME" == "x" ]; then export TRAVIS_OS_NAME=`uname -s | perl -ne 'print lc \$1 if\(/^([a-zA-Z]+)/'\)`; fi

if [ "x$COVERAGE" == "xyes" ]; then
  COVERAGE_OPT="--enable-coverage"
  export EXTRA_CFLAGS="-fprofile-arcs -ftest-coverage"
  export EXTRA_CXXFLAGS="-fprofile-arcs -ftest-coverage"
  if [ "$TRAVIS_OS_NAME" == "osx" ]; then
    # osx doesn't seem to know gcov lib?
    export EXTRA_LDFLAGS="--coverage"
  else
    export EXTRA_LDFLAGS="-lgcov --coverage"
  fi
else
  COVERAGE_OPT="--disable-coverage"
fi

if [ "x$BUILD" == "xstatic" ]; then
  SHARED_OPT="--disable-shared --enable-static"
  MAKE_TARGET="static"
else
  # Makefile of sassc wants to link to static
  SHARED_OPT="--enable-shared --enable-static"
  MAKE_TARGET="shared"
fi

if [ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" ]; then
  MAKE_OPTS="$MAKE_OPTS -j1 V=1"
else
  MAKE_OPTS="$MAKE_OPTS -j5 V=1"
fi

if [ "x$PREFIX" == "x" ]; then
  if [ "x$TRAVIS_BUILD_DIR" == "x" ]; then
    PREFIX=$SASS_LIBSASS_PATH/build
  else
    PREFIX=$TRAVIS_BUILD_DIR/build
  fi
fi

# enable address sanitation
# https://en.wikipedia.org/wiki/AddressSanitizer
if [ "x$CC" == "xclang" ]; then
  if [ "x$COVERAGE" != "xyes" ]; then
    if [ "$TRAVIS_OS_NAME" == "linux" ]; then
      export EXTRA_CFLAGS="$EXTRA_CFLAGS -fsanitize=address"
      export EXTRA_CXXFLAGS="$EXTRA_CXXFLAGS -fsanitize=address"
      export EXTRA_LDFLAGS="$EXTRA_LDFLAGS -fsanitize=address"
    fi
  fi
fi

echo SASS_LIBSASS_PATH: $SASS_LIBSASS_PATH
echo TRAVIS_BUILD_DIR: $TRAVIS_BUILD_DIR
echo SASS_SASSC_PATH: $SASS_SASSC_PATH
echo SASS_SPEC_PATH: $SASS_SPEC_PATH
echo INSTALL_LOCATION: $PREFIX

if [ "x$AUTOTOOLS" == "xyes" ]; then

  echo -en 'travis_fold:start:configure\r'
  autoreconf --force --install
  ./configure --enable-tests $COVERAGE_OPT \
    --disable-silent-rules \
    --with-sassc-dir=$SASS_SASSC_PATH \
    --with-sass-spec-dir=$SASS_SPEC_PATH \
    --prefix=$PREFIX \
    ${SHARED_OPT}
  echo -en 'travis_fold:end:configure\r'

  make $MAKE_OPTS clean

else

  make $MAKE_OPTS clean

  # Run C++ unit tests
  make $MAKE_OPTS -C test clean
  make $MAKE_OPTS -C test test

fi

# install to prefix directory
PREFIX="$PREFIX" make $MAKE_OPTS install

ls -la $PREFIX/*

echo successfully compiled libsass
echo AUTOTOOLS=$AUTOTOOLS COVERAGE=$COVERAGE BUILD=$BUILD

if [ "$CONTINUOUS_INTEGRATION" == "true" ] && [ "$TRAVIS_PULL_REQUEST" != "false" ] && [ "x$TRAVIS_PULL_REQUEST" != "x" ] &&
   ([ "$TRAVIS_OS_NAME" == "linux" ] || [ "$TRAVIS_OS_NAME" == "osx" ] || [ "$TRAVIS_OS_NAME" == "cygwin" ]);
then

  echo "Fetching PR $TRAVIS_PULL_REQUEST"

  JSON=$(curl -L -sS https://api.github.com/repos/sass/libsass/pulls/$TRAVIS_PULL_REQUEST)

  if [[ $JSON =~ "API rate limit exceeded" ]];
  then
    echo "Travis rate limit on github exceeded"
    echo "Retrying via 'special purpose proxy'"
    JSON=$(curl -L -sS https://github-api-reverse-proxy.herokuapp.com/repos/sass/libsass/pulls/$TRAVIS_PULL_REQUEST)
  fi

  RE_SPEC_PR="sass\/sass-spec(#|\/pull\/)([0-9]+)"

  if [[ $JSON =~ $RE_SPEC_PR ]];
  then
    SPEC_PR="${BASH_REMATCH[2]}"
    echo "Fetching Sass Spec PR $SPEC_PR"
    git -C sass-spec fetch -u origin pull/$SPEC_PR/head:ci-spec-pr-$SPEC_PR
    git -C sass-spec checkout --force ci-spec-pr-$SPEC_PR
    make $MAKE_OPTS test_probe
  else
    make $MAKE_OPTS test_probe
  fi
else
  make $MAKE_OPTS test_probe
fi