#!/bin/bash

if [ "${SCRIPT_DEBUG}" = "true" ] ; then
    set -x
    echo "Script debugging is enabled, allowing bash commands and their arguments to be printed as they are executed"
fi

. $(dirname $0)/common.sh
source /usr/local/s2i/scl-enable-maven

LOCAL_SOURCE_DIR=/tmp/src

# Resulting WAR files will be deployed to /opt/eap/standalone/deployments
DEPLOY_DIR=$JBOSS_HOME/standalone/deployments

# JBoss AS data dir. Can be overridden.
DATA_DIR=${DATA_DIR:-$JBOSS_HOME/standalone/data}

# App data dir. Can be overridden.
APP_DATADIR=${APP_DATADIR:-data}

# the subdirectory within LOCAL_SOURCE_DIR from where we should copy build
# artifacts (*.war, *.jar)
ARTIFACT_DIR=${ARTIFACT_DIR:-target}

if [ -f $LOCAL_SOURCE_DIR/configuration/settings.xml ]; then
  echo "Copying maven config file from project..."
  mkdir -p $HOME/.m2
  mv $LOCAL_SOURCE_DIR/configuration/settings.xml $HOME/.m2
fi


configure_proxy
configure_mirrors

manage_incremental_build

# If a pom.xml is present, this is a normal build scenario
# so run maven.
if [ -f "$LOCAL_SOURCE_DIR/pom.xml" ]; then
  pushd $LOCAL_SOURCE_DIR &> /dev/null

  # Add JVM default options
  export MAVEN_OPTS="${MAVEN_OPTS:-$(/opt/run-java/java-default-options)}"
  MAVEN_ARGS=${MAVEN_ARGS--e -Popenshift -DskipTests -Dcom.redhat.xpaas.repo.redhatga package}

  # Use maven batch mode (CLOUD-579)
  # Always force IPv4 (CLOUD-188)
  # Append user-supplied arguments (CLOUD-412)
  MAVEN_ARGS="$MAVEN_ARGS --batch-mode -Djava.net.preferIPv4Stack=true ${MAVEN_ARGS_APPEND}"

  echo "Found pom.xml... attempting to build with 'mvn ${MAVEN_ARGS}'"
  echo "Using MAVEN_OPTS '${MAVEN_OPTS}'"

  echo "Using $(mvn --version)"

  # Execute the actual build
  mvn $MAVEN_ARGS

  ERR=$?
  if [ $ERR -ne 0 ]; then
    echo "Aborting due to error code $ERR from Maven build"
    exit $ERR
  fi

  # Expand each ARTIFACT_DIR entry to absolute path by prefixing it with $LOCAL_SOURCE_DIR
  IFS=',' read -a artifact_dir_entries <<< "$ARTIFACT_DIR"
  artifact_dir_entries=($(printf "$LOCAL_SOURCE_DIR/%s\n" "${artifact_dir_entries[@]}" | tr '\n' ','))

  # Copy built artifacts (if any!) from the target/ directory
  # (or $ARTIFACT_DIR if specified) to the
  # $JBOSS_HOME/standalone/deployments/ directory for later deployment
  # Use artifact directories in absolute path form when copying artifacts
  copy_artifacts "${artifact_dir_entries[@]}" war ear rar jar

  # optionally clear the local maven repository after the build
  clear_maven_repository

  popd &> /dev/null
else
  copy_artifacts "$LOCAL_SOURCE_DIR" war ear rar jar
fi

# copy app data, if specified
if [ -n "$APP_DATADIR" ] && [ -d "$LOCAL_SOURCE_DIR/$APP_DATADIR" ]; then
  echo "Copying app data from ${APP_DATADIR} to ${DATA_DIR}..."
  rsync -rl --out-format='%n' "$LOCAL_SOURCE_DIR/$APP_DATADIR/" "$DATA_DIR"
  chmod -R g+rwX $DATA_DIR
fi

# Copy (probably binary) artifacts from the deployments/
# directory to the $JBOSS_HOME/standalone/deployments/
# directory for later deployment
copy_artifacts "$LOCAL_SOURCE_DIR/deployments" war ear rar jar

if [ -d $LOCAL_SOURCE_DIR/configuration ]; then
  file_count=$(ls -1 $LOCAL_SOURCE_DIR/configuration | wc -l)
  if [ $file_count -gt 0 ]; then
    echo "Copying config files from project..."
    cp -v $LOCAL_SOURCE_DIR/configuration/* $JBOSS_HOME/standalone/configuration/
  fi
fi

if [ -d $LOCAL_SOURCE_DIR/modules ]; then
  echo "Copying modules from project..."
  cp -vr $LOCAL_SOURCE_DIR/modules/* $JBOSS_HOME/modules/
fi

function copy_injected {
  source_dir=$1
  target_dir=$2

  if [ -d "$source_dir" ]; then
    cp -rf ${source_dir}/* $target_dir
  fi
}

if [ -n $CUSTOM_INSTALL_DIRECTORIES ]; then
  IFS=',' read -a install_dir_entries <<< $CUSTOM_INSTALL_DIRECTORIES
  for install_dir_entry in "${install_dir_entries[@]}"; do
    for install_dir in $(find ${LOCAL_SOURCE_DIR}/$install_dir_entry -maxdepth 0 2>/dev/null); do
      chmod -R 755 ${install_dir}
      if [ -f ${install_dir}/install.sh ]; then
        ${install_dir}/install.sh ${install_dir}
      else
        copy_injected ${install_dir}/modules $JBOSS_HOME/modules

        copy_injected ${install_dir}/configuration $JBOSS_HOME/standalone/configuration

        copy_artifacts ${install_dir}/deployments war ear rar jar
      fi
    done
  done
fi

chmod -R g+rwX $HOME

# Remove java tmp perf data dir owned by 185
rm -rf /tmp/hsperfdata_jboss

exit 0
