#!/bin/bash

JENKINS_DIR=/opt/openshift
JENKINS_INSTALL_DIR=$(mktemp -d --suffix=jenkins)

INCREMENTAL_BUILD_ARTIFACTS_DIR=/tmp/artifacts

shopt -s dotglob

echo "---> Copying repository files ..."
cp -Rf /tmp/src/. ${JENKINS_INSTALL_DIR}

# Install jenkins plugins using plugin installer if user has "plugins.txt" file
# in repository
if [ -f ${JENKINS_INSTALL_DIR}/plugins.txt ]; then
  echo "---> Installing $(cat ${JENKINS_INSTALL_DIR}/plugins.txt|wc -l) Jenkins plugins using ${JENKINS_DIR}/plugins.txt ..."
  /usr/local/bin/install-plugins.sh ${JENKINS_INSTALL_DIR}/plugins.txt
  if [[ "$?" != "0" ]]; then
    echo "Failed to install plugins."
    exit 1
  fi
  /usr/local/bin/fix-permissions ${JENKINS_DIR}/plugins
fi

echo "Deleting $INCREMENTAL_BUILD_ARTIFACTS_DIR/plugins"
rm -rf "$INCREMENTAL_BUILD_ARTIFACTS_DIR/plugins"

if [ -d ${JENKINS_INSTALL_DIR}/plugins ]; then
  echo "---> Installing $(ls -l ${JENKINS_INSTALL_DIR}/plugins | grep ^- | wc -l) Jenkins plugins from plugins/ directory ..."
  cp -R ${JENKINS_INSTALL_DIR}/plugins/* ${JENKINS_DIR}/plugins/
  /usr/local/bin/fix-permissions ${JENKINS_DIR}/plugins
fi

if [ -d ${JENKINS_INSTALL_DIR}/configuration ]; then
  if [ -d ${JENKINS_INSTALL_DIR}/configuration/jobs ]; then
    echo "---> Removing sample Jenkins job ..."
    rm -rf ${JENKINS_DIR}/configuration/jobs
  fi
  echo "---> Installing new Jenkins configuration ..."
  /usr/local/bin/fix-permissions ${JENKINS_INSTALL_DIR}/configuration
  rsync -av ${JENKINS_INSTALL_DIR}/configuration/ ${JENKINS_DIR}/configuration
fi
