#!/bin/bash
# Copyright (c) 2011-2012 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

# attempt to find java
locate_cdh_java_home

# We make the client configs world readable so that they can be used by
# any user logged into Hue.

export HUE_CONF_DIR=$CONF_DIR

export HADOOP_CONF_DIR=$CONF_DIR/hadoop-conf

# If ORACLE Instant client is installed then add in LD_LIBRARY_PATH
if [ -e "${ORACLE_HOME}/lib" ]; then
  export LD_LIBRARY_PATH=${ORACLE_HOME}/lib:${LD_LIBRARY_PATH}
fi

function add_postgres_to_pythonpath() {
 # If python-psycopg2 postgres module is installed then add in Pythonpath
 PSYCOPG2_PATH=$(/usr/bin/python -c 'import psycopg2 ; print psycopg2.__file__.rsplit("/", 2)[0]')
 if [ ! -z ${PSYCOPG2_PATH} ]; then
   export PYTHONPATH=${PYTHONPATH}:${PSYCOPG2_PATH}
 fi
 return
}

add_postgres_to_pythonpath

# If Hue is using yarn, then hadoop conf files are in yarn-conf
if [[ -d $CONF_DIR/yarn-conf ]]; then
  export HADOOP_CONF_DIR=$CONF_DIR/yarn-conf
  export HADOOP_MAPRED_HOME=$CDH_MR2_HOME
fi
if [[ -d $HADOOP_CONF_DIR ]]; then
  chmod -R a+r $HADOOP_CONF_DIR
else
  # is_db_alive doesn't need $HADOOP_CONF_DIR, skipping ahead.
  if [ "is_db_alive" != "$1" ]; then
    echo "$HADOOP_CONF_DIR does not exist"
    exit 1
  fi
fi
export HIVE_CONF_DIR=$CONF_DIR/hive-conf
chmod -R a+r $HIVE_CONF_DIR

export HADOOP_HOME=$CDH_HADOOP_HOME
export HIVE_HOME=$CDH_HIVE_HOME
export HUE_HOME=$CDH_HUE_HOME
export HBASE_HOME=$CDH_HBASE_HOME
export PIG_HOME=$CDH_PIG_HOME

# Use the outermost pig script as it has non-trivial workarounds in it
export PIG_SCRIPT_HOME=$PIG_HOME/../..

echo "HUE_HOME=$HUE_HOME"
echo "HUE_CONF_DIR=$HUE_CONF_DIR"
echo "HADOOP_HOME=$HADOOP_HOME"
echo "HADOOP_CONF_DIR=$HADOOP_CONF_DIR"
echo "HIVE_HOME=$HIVE_HOME"
echo "HIVE_DEFAULT_XML=$HIVE_DEFAULT_XML"
echo "HIVE_CONF_DIR=$HIVE_CONF_DIR"
echo "HBASE_HOME=$HBASE_HOME"
echo "PIG_HOME=$PIG_HOME"

HBASE_CONF_DIR=$CONF_DIR/hbase-conf
if [[ -d $HBASE_CONF_DIR ]]; then
  chmod -R a+r $HBASE_CONF_DIR
  echo "HBASE_CONF_DIR=$HBASE_CONF_DIR"
fi

SQOOP2_CONF_DIR=$CONF_DIR/sqoop2-conf
if [[ -d $SQOOP2_CONF_DIR ]]; then
  echo "SQOOP2_CONF_DIR=$SQOOP2_CONF_DIR"
fi

# This must be writable by the hue user.
export PYTHON_EGG_CACHE=$HUE_CONF_DIR/.python-eggs

# 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#{{JAVA_HOME}}#$JAVA_HOME#g" $HUE_CONF_DIR/*.ini
perl -pi -e "s#{{HBASE_HOME}}#$HBASE_HOME#g" $HUE_CONF_DIR/*.ini
perl -pi -e "s#{{PIG_SCRIPT_HOME}}#$PIG_SCRIPT_HOME#g" $HUE_CONF_DIR/*.ini
perl -pi -e "s#{{HADOOP_CONF_DIR}}#$HADOOP_CONF_DIR#g" $HUE_CONF_DIR/*.ini
perl -pi -e "s#{{HUE_CONF_DIR}}#$HUE_CONF_DIR#g" $HUE_CONF_DIR/*.ini
if [[ -d $HBASE_CONF_DIR ]]; then
  perl -pi -e "s#{{HBASE_CONF_DIR}}#$HBASE_CONF_DIR#g" $HUE_CONF_DIR/*.ini
  perl -pi -e "s#{{HBASE_CONF_DIR}}#$HBASE_CONF_DIR#g" $HBASE_CONF_DIR/hbase-env.sh
fi
if [[ -d $SQOOP2_CONF_DIR ]]; then
  perl -pi -e "s#{{SQOOP2_CONF_DIR}}#$SQOOP2_CONF_DIR#g" $HUE_CONF_DIR/*.ini
  perl -pi -e "s#{{SQOOP2_CONF_DIR}}#$SQOOP2_CONF_DIR#g" $SQOOP2_CONF_DIR/*.properties
fi
if [ "$CDH_VERSION" -ge "4" ]; then
  perl -pi -e "s#{{HADOOP_HDFS_HOME}}#$CDH_HDFS_HOME#g" $HUE_CONF_DIR/*.ini
  perl -pi -e "s#{{HADOOP_MR1_HOME}}#$CDH_MR1_HOME#g" $HUE_CONF_DIR/*.ini
  perl -pi -e "s#{{HADOOP_MR2_HOME}}#$CDH_MR2_HOME#g" $HUE_CONF_DIR/*.ini
  perl -pi -e "s#{{HADOOP_BIN}}#$CDH_HADOOP_BIN#g" $HUE_CONF_DIR/*.ini
  SQOOP2_SCRIPT_HOME=$CDH_SQOOP2_HOME/../..
  perl -pi -e "s#{{SQOOP2_SCRIPT_HOME}}#$SQOOP2_SCRIPT_HOME#g" $HUE_CONF_DIR/*.ini
fi
perl -pi -e "s#{{CDH_MR1_HOME}}#$CDH_MR1_HOME#g" $HADOOP_CONF_DIR/*
perl -pi -e "s#{{CDH_MR2_HOME}}#$CDH_MR2_HOME#g" $HADOOP_CONF_DIR/*
perl -pi -e "s#{{CDH_HADOOP_HOME}}#$CDH_HADOOP_HOME#g" $HADOOP_CONF_DIR/*

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

# Ensure .sh and .py files are executable
make_scripts_executable


function run_syncdb_and_migrate_subcommands()
{
  "$HUE" syncdb --noinput
  if [ -z "$HUE_SKIP_MERGE" ]; then
    MERGE_FLAG="--merge"
  else
    MERGE_FLAG=""
  fi

  if [ "$CDH_VERSION" -ge "5" ]; then
    "$HUE" migrate "$MERGE_FLAG"
  fi
}

if [ "$1" == beeswax_server ]; then
  JDBC_JARS="$CLOUDERA_MYSQL_CONNECTOR_JAR:$CLOUDERA_POSTGRESQL_JDBC_JAR:$CLOUDERA_ORACLE_CONNECTOR_JAR"
  if [[ -z "$HADOOP_EXTRA_CLASSPATH_STRING" ]]; then
    export HADOOP_EXTRA_CLASSPATH_STRING="$JDBC_JARS"
  else
    export HADOOP_EXTRA_CLASSPATH_STRING="$HADOOP_EXTRA_CLASSPATH_STRING:$JDBC_JARS"
  fi

  if [ "$CDH_VERSION" -ge "4" ]; then
    # Disable IPv6. This is automatically done by bin/hadoop for CDH3
    # clients.
    export HADOOP_CLIENT_OPTS="-Djava.net.preferIPv4Stack=true $HADOOP_CLIENT_OPTS"
  fi
fi

set_classpath_in_var HADOOP_EXTRA_CLASSPATH_STRING

HUE="$HUE_HOME/build/env/bin/hue"

if [[ "$1" == runcpserver ]]; then
  # Test that the SSL config is sane.
  # Hue only started shipping with pyOpenSSL in CDH4.2. So we try our best to
  # catch it early and give a readable error.
  if grep -q '^\s*ssl_certificate\s*=\s*.\+' *.ini ; then
    $HUE_HOME/build/env/bin/python -c "import OpenSSL" >& /dev/null
    if [[ "$?" -ne "0" ]] ; then
      echo "ERROR: pyOpenSSL is not available. Prior to CDH4.2, you may need to manually install it in order to enable HTTPS. Please see http://tiny.cloudera.com/cdh4-hue-installation."
      exit 1
    fi
  fi

  # Copy the Hue SQLite db if necessary. This is a workaround for
  # CDH <= 4.2.0, where /usr/share/hue/desktop and its contents are deleted
  # during uninstallation of the hue package. To avoid loss of the SQLite
  # database, we copy the database file in /usr/share/hue/desktop to a safer
  # location that is the new configuration value for the Hue Desktop db file 
  if [ ! -z "$HUE_OLD_DB_PATH" ] && [ -f "$HUE_OLD_DB_PATH" ]; then
    # User has a db in /usr/share/hue/desktop. Uh oh. CDH-11067.
    # Let's try to copy it to the configured new db location.
    if [ -z "$HUE_DB_PATH" ]; then
      echo "ERROR: Missing environment variable HUE_DB_PATH"
      exit 1
    fi
    if [ ! -f "$HUE_DB_PATH" ]; then
      echo "INFO: Copying $HUE_OLD_DB_PATH to $HUE_DB_PATH"
      cp $HUE_OLD_DB_PATH $HUE_DB_PATH
      RET=$?
      if [ ! $RET -eq 0 ]; then
        echo "ERROR: Unable to copy $HUE_OLD_DB_PATH to $HUE_DB_PATH."
        exit 1
      fi
    else
      echo "INFO: $HUE_DB_PATH already exists. Not copying $HUE_OLD_DB_PATH to $HUE_DB_PATH."
    fi
  fi

  # We always run syncdb and migrate for Hue server whenever it is started.
  run_syncdb_and_migrate_subcommands
elif [[ $1 == kt_renewer ]]; then
  # The Kerberos ticket renewer role needs to know where kinit is.
  if [ -d /usr/kerberos/bin ]; then
    export PATH=/usr/kerberos/bin:$PATH
  fi
  KINIT_PATH=`which kinit`
  KINIT_PATH=${KINIT_PATH-/usr/bin/kinit}
  perl -pi -e "s#{{KINIT_PATH}}#$KINIT_PATH#g" $HUE_CONF_DIR/*.ini
fi

if [ "dumpdata" = "$1" ]; then
  umask 037
  "$HUE" "$1" --indent 2 > "$2"
elif [ "syncdb" = "$1" ]; then
  run_syncdb_and_migrate_subcommands
elif [ "ldaptest" = "$1" ]; then
  "$HUE" "$1"
else 
  exec "$HUE" "$@"
fi
