#!/usr/bin/bash
#############################################################################
#  Copyright (C) 2013-2015 Lawrence Livermore National Security, LLC.
#  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
#  Written by Albert Chu <chu11@llnl.gov>
#  LLNL-CODE-644248
#
#  This file is part of Magpie, scripts for running Hadoop on
#  traditional HPC systems.  For details, see https://github.com/llnl/magpie.
#
#  Magpie is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  Magpie is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with Magpie.  If not, see <http://www.gnu.org/licenses/>.
#############################################################################

# This script sets up configuration files for jobs.  For the most
# part, it shouldn't be editted.  See job submission files for
# configuration details.

if [ "${STORM_SETUP}" != "yes" ]
then
    exit 0
fi

source ${MAGPIE_SCRIPTS_HOME}/magpie/exports/magpie-exports-dirs
source ${MAGPIE_SCRIPTS_HOME}/magpie/exports/magpie-exports-user
source ${MAGPIE_SCRIPTS_HOME}/magpie/lib/magpie-lib-defaults
source ${MAGPIE_SCRIPTS_HOME}/magpie/lib/magpie-lib-node-identification
source ${MAGPIE_SCRIPTS_HOME}/magpie/lib/magpie-lib-helper
source ${MAGPIE_SCRIPTS_HOME}/magpie/lib/magpie-lib-setup
source ${MAGPIE_SCRIPTS_HOME}/magpie/lib/magpie-lib-version-helper

# stormnoderank set if succeed
if ! Magpie_am_I_a_storm_node
then
    exit 0
fi

# For rest of setup, we will use cluster specific paths
Magpie_make_all_local_dirs_node_specific

extrajvmopts=""

stormlocaldir="${STORM_LOCAL_SCRATCHSPACE_DIR}/local"

if [ ! -d "${stormlocaldir}" ]
then
    mkdir -p ${stormlocaldir}
    if [ $? -ne 0 ] ; then
        echo "mkdir failed making ${stormlocaldir}"
        exit 1
    fi
fi

# Sets magpie_threads_to_use and magpie_processor_count
Magpie_calculate_threads_to_use

if [ "${STORM_SUPERVISOR_SLOTS}X" != "X" ]
then
    stormslotcount=${STORM_SUPERVISOR_SLOTS}
else
    stormslotcount=`expr ${magpie_processor_count} \/ 2`

    if [ "${stormslotcount}" == "0" ]
    then
        stormslotcount="1"
    fi
fi

if [ "${STORM_DAEMON_HEAP_MAX}X" != "X" ]
then
    stormdaemonheapmax="${STORM_DAEMON_HEAP_MAX}"
else
    stormdaemonheapmax="1024"
fi

if [ "${STORM_WORKER_HEAP_MAX}X" != "X" ]
then
    stormworkerheapmax="${STORM_WORKER_HEAP_MAX}"
else
    stormworkerheapmax="1024"
fi

# set java.io.tmpdir
extrajvmopts="${extrajvmopts}${extrajvmopts:+" "}-Djava.io.tmpdir=${STORM_LOCAL_SCRATCHSPACE_DIR}/tmp"

if [ ! -d "${STORM_LOCAL_SCRATCHSPACE_DIR}/tmp" ]
then
    mkdir -p ${STORM_LOCAL_SCRATCHSPACE_DIR}/tmp
    if [ $? -ne 0 ] ; then
        echo "mkdir failed making ${STORM_LOCAL_SCRATCHSPACE_DIR}/tmp"
        exit 1
    fi
fi

# disable hsperfdata if using NO_LOCAL_DIR
if [ "${MAGPIE_NO_LOCAL_DIR}" == "yes" ]
then
    extrajvmopts="${extrajvmopts}${extrajvmopts:+" "}-XX:-UsePerfData"
fi

if [ "${ZOOKEEPER_DATA_DIR_TYPE}" == "local" ]
then
    zookeepersessiontimeout=20000
    zookeeperconnectiontimeout=15000
else
    zookeepersessiontimeout=40000
    zookeeperconnectiontimeout=30000
fi

#
# Get config files for setup
#

# Magpie_find_conffile will set the 'pre' filenames
Magpie_find_conffile "Storm" ${STORM_CONF_FILES:-""} "storm.yaml" "pre_stormyaml"
Magpie_find_conffile "Storm" ${STORM_CONF_FILES:-""} "storm_env.ini" "pre_stormenvini"
Magpie_find_conffile "Storm" ${STORM_CONF_FILES:-""} "storm-daemon-env.sh" "pre_stormdaemonenvsh"

post_stormyaml=${STORM_CONF_DIR}/storm.yaml
post_stormenvini=${STORM_CONF_DIR}/storm_env.ini
post_stormdaemonenvsh=${STORM_CONF_DIR}/storm-daemon-env.sh

#
# Setup Storm configuration files and environment files
#

stormlocaldirsubst=`echo "${stormlocaldir}" | sed "s/\\//\\\\\\\\\//g"`
stormlogdirsubst=`echo "${STORM_LOG_DIR}" | sed "s/\\//\\\\\\\\\//g"`

extrajvmoptssubst=`echo "${extrajvmopts}" | sed "s/\\//\\\\\\\\\//g"`

cp ${pre_stormyaml} ${post_stormyaml}

sed -i \
    -e "s/NIMBUSHOST/${STORM_MASTER_NODE}/g" \
    -e "s/STORMLOCALDIR/${stormlocaldirsubst}/g" \
    -e "s/STORMLOGDIR/${stormlogdirsubst}/g" \
    -e "s/STORMZOOKEEPERPORT/${default_zookeeper_client_port}/g" \
    -e "s/STORMUIPORT/${default_storm_ui_port}/g" \
    -e "s/STORMLOGVIEWERPORT/${default_storm_logviewer_port}/g" \
    -e "s/STORMDRPCPORT/${default_storm_drpc_port}/g" \
    -e "s/STORMDRPCINVOCATIONSPORT/${default_storm_invocations_port}/g" \
    -e "s/STORMDRPCHTTPPORT/${default_storm_drpc_http_port}/g" \
    -e "s/NIMBUSTHRIFTPORT/${default_storm_thrift_port}/g" \
    -e "s/STORM_DAEMON_HEAP_MAX/${stormdaemonheapmax}/g" \
    -e "s/STORM_WORKER_HEAP_MAX/${stormworkerheapmax}/g" \
    -e "s/EXTRAJVMOPTS/${extrajvmoptssubst}/g" \
    -e "s/ZOOKEEPERSESSIONTIMEOUT/${zookeepersessiontimeout}/g" \
    -e "s/ZOOKEEPERCONNECTIONTIMEOUT/${zookeeperconnectiontimeout}/g" \
    ${post_stormyaml}

# Add Zookeeper servers to conf

echo "storm.zookeeper.servers:" >> ${post_stormyaml}

zookeepernodes=`cat ${ZOOKEEPER_CONF_DIR}/workers`
servercount=1
for zookeepernode in ${zookeepernodes}
do
    echo "     - \"${zookeepernode}\"" >> ${post_stormyaml}
    servercount=`expr ${servercount} + 1`
done

echo "" >> ${post_stormyaml}

# Add slots to conf

echo "supervisor.slots.ports:" >> ${post_stormyaml}

slotport=${default_storm_supervisor_slots_starting_port}
for i in `seq 1 ${stormslotcount}`
do
    echo "     - ${slotport}" >> ${post_stormyaml}
    slotport=`expr ${slotport} + 1`
done

stormjavahomesubst=`echo "${JAVA_HOME}" | sed "s/\\//\\\\\\\\\//g"`
stormconfdirsubst=`echo "${STORM_CONF_DIR}" | sed "s/\\//\\\\\\\\\//g"`
stormhomesubst=`echo "${STORM_HOME}" | sed "s/\\//\\\\\\\\\//g"`
magpiescriptshome=`echo "${MAGPIE_SCRIPTS_HOME}" | sed "s/\\//\\\\\\\\\//g"`

cp ${pre_stormenvini} ${post_stormenvini}

sed -i \
    -e "s/MAGPIEJAVAHOME/${stormjavahomesubst}/g" \
    -e "s/STORMJARJVMOPTS/${extrajvmoptssubst}/g" \
    ${post_stormenvini}

cp ${pre_stormdaemonenvsh} ${post_stormdaemonenvsh}

sed -i \
    -e "s/MAGPIEJAVAHOME/${stormjavahomesubst}/g" \
    -e "s/STORMLOGDIR/${stormlogdirsubst}/g" \
    -e "s/STORMCONFDIR/${stormconfdirsubst}/g" \
    -e "s/STORMHOME/${stormhomesubst}/g" \
    -e "s/STORMLOCALDIR/${stormlocaldirsubst}/g" \
    -e "s/MAGPIESCRIPTSHOME/${magpiescriptshome}/g" \
    ${post_stormdaemonenvsh}

if [ "${MAGPIE_REMOTE_CMD:-ssh}" != "ssh" ]
then
    echo "export STORM_SSH_CMD=\"${MAGPIE_REMOTE_CMD}\"" >> ${post_stormdaemonenvsh}
fi
if [ "${MAGPIE_REMOTE_CMD_OPTS}X" != "X" ]
then
    echo "export STORM_SSH_OPTS=\"${MAGPIE_REMOTE_CMD_OPTS}\"" >> ${post_stormdaemonenvsh}
fi

# Sets magpie_stormmajorminorversion
Magpie_get_storm_major_minor_version ${STORM_VERSION}

# Handle special settings depending on version
# 0 is =, 1 is >, 2 is <
Magpie_vercomp ${magpie_stormmajorminorversion} 1.0
vercomp_result=$?
if [ "${vercomp_result}" == "0" ] || [ "${vercomp_result}" == "1" ]
then
    sed -i -e "s/nimbus.host/# nimbus.host/" ${post_stormyaml}
else
    sed -i -e "s/nimbus.seeds/# nimbus.seeds/" ${post_stormyaml}
fi

exit 0
