#!/bin/bash

# Copyright (c) 2013 Cloudera, Inc. All rights reserved.

# Time marker for both stderr and stdout
date; date 1>&2

cloudera_config=`dirname $0`
cloudera_config=`cd "$cloudera_config/../common"; pwd`
. ${cloudera_config}/cloudera-config.sh

# attempt to find java
locate_java_home

# For JVM-based processes, search-replace {{PID}} in heap dump path,
if [ -n "$CSD_JAVA_OPTS" ]; then
  export CSD_JAVA_OPTS=$(replace_pid $CSD_JAVA_OPTS)
  echo "Using $CSD_JAVA_OPTS as CSD_JAVA_OPTS"
fi

# Load the parcel environment
source_parcel_environment

echo "Using $CONF_DIR as conf dir"
echo "Using $CM_CSD_SCRIPT as process script"

# Search-replace {{CMF_CONF_DIR}} in files
replace_conf_dir

# mark common scripts as executable
make_scripts_executable

RUN_DIR=$CONF_DIR
if [ "$RUN_COMMON_CM_SCRIPT" == "true" ]; then
  RUN_DIR=$CMF_PACKAGE_DIR
else
  # Make sure that the custom script is executable
  chmod u+x $RUN_DIR/$CM_CSD_SCRIPT
fi

# Allow CSDs to use the common functions
export COMMON_SCRIPT=${cloudera_config}/cloudera-config.sh

exec $RUN_DIR/$CM_CSD_SCRIPT "$@"
