#!/bin/bash

set -e

script/bootstrap

# 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$TRAVIS_BUILD_DIR" == "x" ]; then export TRAVIS_BUILD_DIR=$(pwd)/build; 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

if [ "x$COVERAGE" == "xyes" ]; then
  COVERAGE_OPT="--enable-coverage"
  export EXTRA_CFLAGS="-O0 --coverage"
  export EXTRA_CXXFLAGS="-O0 --coverage"
  export EXTRA_LDFLAGS="-O0 --coverage"
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

MAKE_OPTS="$MAKE_OPTS -j3 V=1"

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=$TRAVIS_BUILD_DIR \
    ${SHARED_OPT}
  echo -en 'travis_fold:end:configure\r'

  make $MAKE_OPTS install

else

  make clean

  # does what $BUILD says
  make $MAKE_OPTS

  # sassc has static as dep
  make $MAKE_OPTS $SASS_SASSC_PATH/bin/sassc

fi

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

if [ "x$PREFIX" == "x" ]; then
	if [ "x$TRAVIS_BUILD_DIR" == "x" ]; then
		PREFIX=/usr/local
	else
		PREFIX=$TRAVIS_BUILD_DIR
	fi
fi

LD_LIBRARY_PATH="$PREFIX/lib/" script/spec
