#!/bin/bash

set -e
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# 'target' is the output directory
rm -rf $DIR/target
mkdir $DIR/target
mkdir $DIR/target/deb
 
#rm -rf vendor/bundle -- let jenkins config wipe workspace, or not

rm -rf tmp/capybara
#rm -f Gemfile.lock # if we don't want versions to float, pin it in the Gemfile, not count on Gemfile.lock
# put all dependencies into vendor/bundle

# default build number to 1 if not found
if [ -z "$BUILD_NUMBER" ]; then
    BUILD_NUMBER="1"
fi

GEM_VERSION="0.1.${BUILD_NUMBER}"


# by putting these gems in vendor/cache, bundle will see them when running 'bundle install'
mkdir -p vendor/cache
#cp ../db/target/ruby_package/jam_db-${GEM_VERSION}.gem vendor/cache/ || { echo "unable to copy jam-db gem"; exit 1; }
cp ../pb/target/ruby/jampb/jampb-${GEM_VERSION}.gem vendor/cache/ || { echo "unable to copy jam-pb gem";  exit 1; }
cp ../ruby/jam_ruby-${GEM_VERSION}.gem vendor/cache/ || { echo "unable to copy jam-ruby gem"; exit 1; }
cp ../websocket-gateway/jam_websockets-${GEM_VERSION}.gem vendor/cache/ || { echo "unable to copy websocket-gateway gem"; exit 1; }

echo "updating dependencies"

# for some reason, this command fails on Ubuntu 14.04/Ruby 2.2.2.  But the next bundle install succeeds.
set +e
bundle install
set -e
bundle install --path vendor/bundle
#bundle update

set +e
echo "cleaning assets from last build"
# clean assets, because they may be lingering from last build
# bundle exec rake assets:clean
rm -rf $DIR/public/assets

if [ "$?" = "0" ]; then
    echo "success: updated dependencies"
else
    echo "could not update dependencies"
    exit 1
fi

if [ -z $SKIP_TESTS ]; then

    phantomjs --version
    if [ "$?" != "0" ]; then
        echo "phantomjs is required to run rspec tests; please install it"
        echo "emitting path to debug on build server $PATH"
        exit 1
    fi

	echo "running rspec tests"
	bundle exec rspec
	if [ "$?" = "0" ]; then
	    echo "success: ran rspec tests"
  elif [ "$?" = "20" ]; then
        echo "retrying once more"
        bundle exec rspec

        if [ "$?" = "0" ]; then
            echo "success: ran rspec tests"
        else
            echo "running rspec tests for the second time failed."
            exit 1
        fi
  else
	    echo "running rspec tests failed."
	    exit 1
	fi


    if [ -z $SKIP_KARMA ]; then
        echo "running karma tests"
        #http://shortforgilbert.com/blog/2011/03/25/headless-jasmine-ci
        # TODO starting Xvfb here because we don't do this on start of build server
        # If you run it once, it will background/nohup itself, so this is 'lazy'
        Xvfb :99 -screen 0 1440x900x16 > /dev/null 2>&1 & 
        # run jasmine using the virtual screen, and in the test environment to use the jam_web_test db
        DISPLAY=":99" karma start spec/javascripts/karma.ci.conf.js

        if [ "$?" = "0" ]; then
            echo "success: karma tests completed"
        else
            echo "running karma tests failed"
            exit 1
        fi
    fi

    if [ -z "$SKIP_CUCUMBER_TESTS" ]; then
        echo "running cucumber tests"
#        DISPLAY=":99" bundle exec cucumber
        if [ "$?" = "0" ]; then
            echo "success: cucumber tests completed"
        else
            echo "running cucumber tests failed"
            exit 1
        fi
    fi
fi

if [ -n "$PACKAGE" ]; then

    if [ -z "$BUILD_NUMBER" ]; then
       echo "BUILD NUMBER is not defined"
       exit 1
    fi

    cat > lib/jam_web/version.rb << EOF
module JamWeb
    VERSION = "0.1.$BUILD_NUMBER"
end
EOF

    echo "$BUILD_NUMBER" > BUILD_NUMBER

    type -P dpkg-architecture > /dev/null

   
    if [ "$?" = "0" ]; then
        ARCH=`dpkg-architecture -qDEB_HOST_ARCH`
    else
        echo "WARN: unable to determine architecture."
        ARCH=`all`
    fi


    set -e
    # cache all gems local, and tell bundle to use local gems only
    #bundle install --path vendor/bundle --local
    # prepare production acssets
    rm -rf $DIR/public/assets
    bundle exec rake assets:precompile RAILS_ENV=production
	
    # create debian using fpm 
    bundle exec fpm -s dir -t deb -p target/deb/jam-web_0.1.${BUILD_NUMBER}_${ARCH}.deb -n "jam-web" -v "0.1.$BUILD_NUMBER" --prefix /var/lib/jam-web --after-install $DIR/script/package/post-install.sh --before-install $DIR/script/package/pre-install.sh --before-remove $DIR/script/package/pre-uninstall.sh --after-remove $DIR/script/package/post-uninstall.sh Gemfile .bundle config Rakefile script config.ru lib public vendor app BUILD_NUMBER

fi

echo "build complete"

