#!/bin/bash -e

source $OPENSHIFT_CARTRIDGE_SDK_BASH

cartridge_type="jenkins"

function isrunning() {
    # Check for running app
    pid=`pgrep -f ".*java.*-jar.*jenkins.war.*--httpListenAddress=${OPENSHIFT_JENKINS_IP}.*" 2> /dev/null`
    if [ -n "$pid" ]
    then
        return 0
    fi
    # not running
    return 1
}

# Check if the server is all the way up
function is_up() {
    jenkins_url="http://${OPENSHIFT_JENKINS_IP}:8080/"

    let count=0
    while [ ${count} -lt 25 ]
    do
        url="curl -s -k -X GET --user \"${JENKINS_USERNAME}:${JENKINS_PASSWORD}\" ${jenkins_url}"
        result=`$url`
        if [ -z "$result" ]; then
                echo "Waiting ..."
        else
                if [[ "$result" == *"Please wait while Jenkins is getting ready to work"* ]]; then
                        echo "Still waiting ..."
                else
                        return 0
                fi
        fi
        let count=${count}+1
        sleep 2
    done
    return 1
}

function start() {
 	echo "Starting $cartridge_type cart"

  if isrunning
  then
    echo "Application is already running!" 1>&2
    exit 0
  fi
  JENKINS_CMD="/etc/alternatives/jre/bin/java \
      -Xmx168m \
      -XX:MaxPermSize=100m \
      -Dcom.sun.akuma.Daemon=daemonized \
      -Djava.awt.headless=true"

  if [ -f "${OPENSHIFT_REPO_DIR}/.openshift/markers/enable_debugging" ]; then
      JENKINS_CMD="${JENKINS_CMD} -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=${OPENSHIFT_JENKINS_IP}:7600,suspend=n"
  fi
  
  if [ -z $JENKINS_WAR_PATH ]; then
	 JENKINS_WAR_PATH=/usr/lib/jenkins/jenkins.war
  fi

  JENKINS_CMD="${JENKINS_CMD} ${JENKINS_OPTS} -DJENKINS_HOME=$OPENSHIFT_DATA_DIR \
      -Dhudson.slaves.NodeProvisioner.recurrencePeriod=500 \
      -Dhudson.slaves.NodeProvisioner.initialDelay=100 \
      -Dhudson.slaves.NodeProvisioner.MARGIN=100 \
      -Dhudson.model.UpdateCenter.never=true \
      -Dhudson.DNSMultiCast.disabled=true \
      -jar ${JENKINS_WAR_PATH} \
      --ajp13Port=-1 \
      --controlPort=-1 \
      --logfile=$OPENSHIFT_JENKINS_DIR/logs/jenkins.log \
      --daemon \
      --httpPort=8080 \
      --debug=5 \
      --handlerCountMax=45 \
      --handlerCountMaxIdle=20 \
      --httpListenAddress=$OPENSHIFT_JENKINS_IP"

  $JENKINS_CMD &
  
  if ! is_up; then
      echo "Timed out waiting for Jenkins to fully start"
      exit 1
  fi
}

function stop() {
 	echo "Stopping $cartridge_type cart"
 	
  if isrunning
  then
    kill -TERM $pid > /dev/null 2>&1
    wait_for_stop $pid
  else
    echo "Application is already stopped!" 1>&2
  fi
}

function restart() {
  echo "Restarting $cartridge_type cart"

  stop
  start
}

function status() {
   if isrunning
   then
      client_result "Application is running"
   else
      client_result "Application is either stopped or inaccessible"
   fi
}

function reload() {
    echo "Reloading $cartridge_type cart"
    restart
}

# Clean up any log files
function tidy() {
  client_message "Emptying log dir: $OPENSHIFT_JENKINS_LOG_DIR"
  shopt -s dotglob
  rm -rf $OPENSHIFT_JENKINS_LOG_DIR/*
}


case "$1" in
  start)     	start ;;
  stop)      	stop ;;
  restart)   	restart ;;
  status)    	status ;;
  reload)    	reload ;;
  tidy)      	tidy ;;
  *)         	exit 0
esac

