#!/bin/bash
# Copyright (c) 2015 Cloudera, Inc. All rights reserved.

set -x

# 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

# Load the parcel environment
source_parcel_environment

find_path() {
  for candidate in $@; do
    if [ -e $candidate ]; then
      echo $candidate
      return 0
    fi
  done

  return 1
}

# Each platform uses a different name for HTTPD
HTTPD_CANDIDATES=(
  # CentOS path
  "/usr/sbin/httpd"

  # Debian path
  "/usr/sbin/apache2"

  # SLES path
  "/usr/sbin/httpd2"
)

# Each platform stores its HTTPD modules in a separate location.
HTTPD_MODULE_DIR_CANDIDATES=(
  # CentOS paths
  "/usr/lib64/httpd/modules"
  "/usr/lib/httpd/modules"

  # Debian Paths
  "/usr/lib64/apache2/modules"
  "/usr/lib/apache2/modules"

  # SLES Paths - Following SLES's pattern of using the preforked modules
  "/usr/lib64/apache2-prefork"
  "/usr/lib64/apache2"
)

if [ -z "$CLOUDERA_HTTPD_EXECUTABLE_PATH" ]; then
  CLOUDERA_HTTPD_EXECUTABLE_PATH=`find_path ${HTTPD_CANDIDATES[@]}`
  if [ "$?" != 0 ]; then
    echo "Failed to find the Apache HTTPD executable." 1>&2
    echo "Tried ${HTTPD_CANDIDATES[@]}" 1>&2
    echo "Please confirm that Apache HTTPD is properly installed on the Hue Load Balancer host." 1>&2
    echo "If Apache HTTPD is installed in a non-standard location, set the" 1>&2
    echo "'CLOUDERA_HTTPD_EXECUTABLE_PATH' environment variable in the Load Balancer" 1>&2
    echo "Environment Advanced Configuration Snippet (Safety Valve)" 1>&2
    exit 1
  fi
fi

if [ ! -x "$CLOUDERA_HTTPD_EXECUTABLE_PATH" ]; then
  echo "$CLOUDERA_HTTPD_EXECUTABLE_PATH executable does not exist or is not executable." 1>&2
  echo "Check the 'Apache HTTPD Executable Path' configuration property" 1>&2
  exit 1
fi

if [ -z "$CLOUDERA_HTTPD_MODULE_DIR" ]; then
  CLOUDERA_HTTPD_MODULE_DIR=`find_path ${HTTPD_MODULE_DIR_CANDIDATES[@]}`
  if [ "$?" != 0 ]; then
    echo "Failed to find the Apache HTTPD module paths." 1>&2
    echo "Tried ${HTTPD_MODULE_DIR_CANDIDATES[@]}" 1>&2
    echo "Please confirm that Apache HTTPD is properly installed on the Hue Load Balancer host." 1>&2
    echo "If Apache HTTPD is installed in a non-standard location, set the" 1>&2
    echo "'CLOUDERA_HTTPD_MODULE_DIR' environment variable in the Load Balancer" 1>&2
    echo "Environment Advanced Configuration Snippet (Safety Valve)" 1>&2
    exit 1
  fi
fi

if [ ! -d "$CLOUDERA_HTTPD_MODULE_DIR" ]; then
  echo "$CLOUDERA_HTTPD_MODULE_DIR does not exist, is not a directory, or is not readable." 1>&2
  echo "Check the 'Apache HTTPD Module Path' configuration property" 1>&2
  exit 1
fi

if [ -z "$CLOUDERA_HTTPD_LOG_DIR" ]; then
  echo "CLOUDERA_HTTPD_LOG_DIR environment variable is not defined." 1>&2
  echo "please check the 'Hue Load Balancer Log Directory' configuration variable." 1>&2
  exit 1
fi

if [ "$1" = "refresh" ]; then
  pid="$2"
  if [ "$pid" = "" ]; then
    echo "httpd process pid was not specified" 1>&2
    exit 1
  fi

  CONF_DIR=$(xargs -n 1 -0 < /proc/$pid/environ | sed -n 's/^CONF_DIR=\(.*\)/\1/p')
  if [ -n "$CONF_DIR" ]; then
    if [ ! -d "$CONF_DIR" ]; then
      echo "httpd process dir $CONF_DIR does not exist" 1>&2
      exit 1
    fi
  else
    echo "httpd process pid mismatch, $pid does not contain CONF_DIR variable in environment" 1>&2
    exit 1
  fi
fi

export HUE_HOME="$CDH_HUE_HOME"
export CLOUDERA_HTTPD_CONF_DIR="$CONF_DIR"

echo "HUE_HOME=$HUE_HOME"
echo "CLOUDERA_HTTPD_EXECUTABLE_PATH=$CLOUDERA_HTTPD_EXECUTABLE_PATH"
echo "CLOUDERA_HTTPD_MODULE_DIR=$CLOUDERA_HTTPD_MODULE_DIR"
echo "CLOUDERA_HTTPD_CONF_DIR=$CLOUDERA_HTTPD_CONF_DIR"
echo "CLOUDERA_HTTPD_LOG_DIR=$CLOUDERA_HTTPD_LOG_DIR"
echo "CLOUDERA_HTTPD_USE_SSL=$CLOUDERA_HTTPD_USE_SSL"

# local replacement of substitution variables.
# i.e. master doesn't know final resting place of configuration files
#   which may require absolute path (e.g. dfs_allows)
perl -pi -e "s#{{HUE_HOME}}#$HUE_HOME#g" $CLOUDERA_HTTPD_CONF_DIR/*.conf
perl -pi -e "s#{{CLOUDERA_HTTPD_CONF_DIR}}#$CLOUDERA_HTTPD_CONF_DIR#g" $CLOUDERA_HTTPD_CONF_DIR/*.conf
perl -pi -e "s#{{CLOUDERA_HTTPD_LOG_DIR}}#$CLOUDERA_HTTPD_LOG_DIR#g" $CLOUDERA_HTTPD_CONF_DIR/*.conf
perl -pi -e "s#{{CLOUDERA_HTTPD_MODULE_DIR}}#$CLOUDERA_HTTPD_MODULE_DIR#g" $CLOUDERA_HTTPD_CONF_DIR/*.conf

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

if [[ ! -d "$CLOUDERA_HTTPD_CONF_DIR/run" ]]; then
  mkdir -p "$CLOUDERA_HTTPD_CONF_DIR/run"
fi

CLOUDERA_HTTPD_CONF="$CLOUDERA_HTTPD_CONF_DIR/httpd.conf"
CLOUDERA_HUE_CONF="$CLOUDERA_HTTPD_CONF_DIR/hue.conf"

# Add absolute path for sslpassphrase.sh script, we use environment variable AGENT_COMMON_DIR value
perl -pi -e "s#{{CLOUDERA_HTTPD_SSL_SCRIPT_DIR}}#$CLOUDERA_HTTPD_SSL_SCRIPT_DIR#g" $CLOUDERA_HUE_CONF

# Make sure the config is valid
"$CLOUDERA_HTTPD_EXECUTABLE_PATH" -f "$CLOUDERA_HTTPD_CONF" -t
if [ "$?" != 0 ]; then
  echo "$CLOUDERA_HTTPD_CONF is invalid" 1>&2
  exit 1
fi

if [ "$1" = "refresh" ]; then
  pid="$2"
  if [ "$pid" = "" ]; then
    echo "httpd process pid was not specified" 1>&2
    exit 1
  fi

  exec kill -USR1 $pid
else
  ARGS=""
  if [ -n "$CLOUDERA_HTTPD_USE_SSL" ]; then
    ARGS="-DCLOUDERA_HTTPD_USE_SSL"
  fi

  # Run httpd in the foreground so the agent can observe if httpd crashes.
  exec "$CLOUDERA_HTTPD_EXECUTABLE_PATH" -f "$CLOUDERA_HTTPD_CONF" -DFOREGROUND $ARGS
fi
