#!/bin/bash

# This script defines various utility functions which are useful in the context
# of a Jenkins 'Shell' build task.


# Checks for the presence of the hot_deploy marker in the git repo inside
# the Jenkins workspace. Returns 0 if the marker is present, otherwise 1.
function hot_deploy_marker_present_in_workspace {
  # For some reason, WORKSPACE is set to a relative path, although the Jenkins
  # docs specify it should be absolute. Play it safe by getting the absolute
  # path manually and doing checks relative to our own variable.
  workspace_abs=`cd ~/$WORKSPACE; pwd`
  hot_deploy_file="${workspace_abs}/.openshift/markers/hot_deploy"

  if [ -f $hot_deploy_file ]; then
    return 0
  else
    return 1
  fi
}

# Wraps the remote call to the app start script with a check for the hot
# deployment marker. If the marker is present, the start will be skipped.
#
# Usage: jenkins_start_app <uuid@host>
function jenkins_start_app {
  UPSTREAM_SSH=$1

  if hot_deploy_marker_present_in_workspace; then
    echo "Skipping application start due to presence of hot_deploy marker"
  else
    $GIT_SSH $UPSTREAM_SSH 'ctl_all start'
  fi
}

# Wraps the remote call to the app stop script with a check for the hot
# deployment marker. If the marker is present, the stop script will be
# skipped.
#
# Usage: jenkins_stop_app <uuid@host>
function jenkins_stop_app {
  UPSTREAM_SSH=$1

  if hot_deploy_marker_present_in_workspace; then
    echo "Skipping application stop due to presence of hot_deploy marker"
  else
    $GIT_SSH $UPSTREAM_SSH 'ctl_all stop'
  fi
}

# Executes rsync consistently with options which are known to work well
# in the context of a Jenkins build for an OpenShift application.
#
# All arguments to the function are passed through to rsync.
function jenkins_rsync {
  rsync --delete-after -az -e "$GIT_SSH" $@
}

# Performs three standard rsync commands which are necessary for a stock
# JBoss application to be updated properly (supporting Maven, hot deployment,
# and OpenShift metadata).
#
# Usage: jenkins_sync_jboss <uuid@host>
function jenkins_sync_jboss {
  UPSTREAM_SSH=$1

  jenkins_rsync ~/.m2/ $UPSTREAM_SSH:~/.m2/
  
  cd ~
  jboss_version=`find . -maxdepth 1 -type d -name "jboss*"`

  jenkins_rsync --exclude='*.deployed' --exclude='*.deploying' --exclude='*.isundeploying' \
    ~/$WORKSPACE/deployments/. $UPSTREAM_SSH:~/$jboss_version/$jboss_version/standalone/deployments/
    
  jenkins_rsync --exclude='*.deployed' --exclude='*.deploying' --exclude='*.isundeploying' \
    ~/$WORKSPACE/deployments/. $UPSTREAM_SSH:~/app-root/repo/deployments/

  jenkins_rsync ~/$WORKSPACE/.openshift/ $UPSTREAM_SSH:~/app-root/repo/.openshift/
}

function jenkins_sync_jbossews {
  UPSTREAM_SSH=$1

  jenkins_rsync ~/.m2/ $UPSTREAM_SSH:~/.m2/

  jenkins_rsync ~/$WORKSPACE/webapps/. $UPSTREAM_SSH:~/app-root/repo/webapps/

  jenkins_rsync ~/$WORKSPACE/.openshift/ $UPSTREAM_SSH:~/app-root/repo/.openshift/
}

# If the previous and current commits didn't upload .bundle sync previous bundled gems.
# Note: Adding .openshift/markers/force_clean_build at the root of the repo will trigger a clean rebundle
function restore_cached_ruby_bundles {
  UPSTREAM_SSH=$1

  if [ ! -f .openshift/markers/force_clean_build ] && ! [ -d .bundle ] && ! git show master~1:.bundle > /dev/null 2>&1
  then
    jenkins_rsync --include='.bundle/' --include='.bundle/***' --include='vendor/' --include='vendor/***' --exclude='*' $UPSTREAM_SSH:~/app-root/runtime/repo/ ~/$WORKSPACE
  fi
}
