#!/bin/bash

# RUN_SLOW_TESTS, RUN_AWS_TESTS, SKIP_KARMA=1 SHOW_JS_ERRORS=1    PACKAGE=1
# WORKSPACE=/var/lib/jenkins/jobs/jam-web/workspace

set -e 

export BUNDLE_JOBS=1 # 6, which i want to use, makes the whole server crawl

echo ""

echo "BUILDING JAM-DB"
pushd db > /dev/null
 ./jenkins
popd > /dev/null

echo ""

echo "BUILDING JAM-PB"
pushd pb > /dev/null
bash -l ./jenkins
popd > /dev/null

echo ""

echo "BUILDING JAM-RUBY"
pushd ruby > /dev/null
rm -f *.gem
bash -l ./jenkins
popd > /dev/null

echo ""

echo "BUILDING WEBSOCKET GATEWAY"
pushd websocket-gateway > /dev/null
bash -l ./jenkins
popd > /dev/null

echo ""

echo "BUILDING JAM-WEB"
pushd web > /dev/null
echo "kill any stuck rspec tests from previous run.  need to debug how/why this happens on build server"
set +e
ps aux | grep -ie "jam-cloud.*rspec" | awk '{print $2}' | xargs kill -9
set -e

PACKAGE=1 bash ./jenkins

# we do this so that the build won't fail in jenkins if no capybara error screenshot isn't there
mkdir -p tmp/capybara
touch tmp/capybara/success.png
popd > /dev/null

echo ""

echo "BUILDING JAM-ADMIN"
pushd admin /dev/null
bash -l ./jenkins
popd > /dev/null


if [ ! -z "$PACKAGE" ]; then

DEB_SERVER=http://localhost:9010/apt-`uname -p`
GEM_SERVER=http://localhost:9000/gems

  # if still going, then push all debs up
  if [[ "$GIT_BRANCH" == *develop* || "$GIT_BRANCH" == *master* || "$GIT_BRANCH" == *release* || "$GIT_BRANCH" == *feature* || "$GIT_BRANCH" == *hotfix* ]]; then

      echo ""
      echo "PUSHING DB ARTIFACTS"
      pushd db > /dev/null
      echo "publishing ubuntu packages (.deb)"
      for f in `find target -name '*.deb'`; do
         DEBNAME=`basename $f`
         DEBPATH="$f"
         echo "publishing $DEBPATH to deb server"
         curl -f -T $DEBPATH $DEB_SERVER/$DEBNAME
         if [ "$?" != "0" ]; then
           echo "deb publish failed of $DEBPATH"
           exit 1
         fi
      done
      echo "done publishing debs"
      popd > /dev/null


      echo ""
      echo "PUSHING WEB"
      pushd web > /dev/null
      echo "publishing ubuntu package (.deb)"
      DEBPATH=`find target/deb -name *.deb`
      DEBNAME=`basename $DEBPATH`
      curl -f -T $DEBPATH $DEB_SERVER/$DEBNAME
      if [ "$?" != "0" ]; then
          echo "deb publish failed"
          exit 1
      fi
      echo "done publishing deb"
      popd > /dev/null

      echo ""
      echo "PUSHING WEBSOCKET-GATEWAY"
      pushd websocket-gateway > /dev/null
      echo "publishing ubuntu package (.deb)"
      DEBPATH=`find target/deb -name *.deb`
      DEBNAME=`basename $DEBPATH`
      curl -f -T $DEBPATH $DEB_SERVER/$DEBNAME
      if [ "$?" != "0" ]; then
          echo "deb publish failed"
          exit 1
      fi
      echo "done publishing deb"
      popd > /dev/null

      echo ""
      echo "PUSHING ADMIN"
      pushd admin > /dev/null
      echo "publishing ubuntu package (.deb)"
      DEBPATH=`find target/deb -name *.deb`
      DEBNAME=`basename $DEBPATH`
      curl -f -T $DEBPATH $DEB_SERVER/$DEBNAME
      if [ "$?" != "0" ]; then
          echo "deb publish failed"
          exit 1
      fi
      echo "done publishing deb"
      popd > /dev/null

  else
      echo "Skipping publish since branch is not master, develop, or release/*.  branch is $GIT_BRANCH"
  fi

fi
