#!/bin/bash

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
. $SCRIPT_DIR/common.sh "$@"


echo "-- SETTING UP ELASTICSEARCH ----------------------------------------------------"

export ES_VERSION=6.0.0-beta2

echo " - Increasing system vm.max_map_count setting"
sudo bash -c 'echo -e "\nvm.max_map_count=262144" >>  /etc/sysctl.conf'
sudo sysctl -w vm.max_map_count=262144 > /tmp/es_install_log 2>&1
fail_if_error $? "/tmp/es_install_log" -1

echo " - Creating elasticsearch user (if not exist)"
elascticsearch_user_id=`id -u elasticsearch > /tmp/es_install_log 2>&1`
if [[ $? != 0 ]]; then
    sudo useradd elasticsearch
fi

echo " - Creating elasticsearch data directory in /var/lib/elasticsearch"
sudo mkdir -p /var/lib/elasticsearch
sudo chown -R elasticsearch /var/lib/elasticsearch

echo " - Creating elasticsearch PID file directory"
sudo mkdir -p /var/run/elasticsearch
sudo chown -R elasticsearch /var/run/elasticsearch

echo " - Opening rights to logs folder to user elasticsearch"
sudo chmod -R 777 /usr/local/lib/elasticsearch-$ES_VERSION/logs/

echo " - Simlinking elasticsearch logs to /var/log/"
sudo ln -s /usr/local/lib/elasticsearch-$ES_VERSION/logs /var/log/elasticsearch

echo " - Simlinking ES config to /usr/local/etc/elasticsearch"
sudo ln -s /usr/local/lib/elasticsearch-$ES_VERSION/config /usr/local/etc/elasticsearch

echo " - Simlinking ES binaries and scripts to /usr/local/sbin"
sudo ln -s /usr/local/lib/elasticsearch-$ES_VERSION/bin/elasticsearch /usr/local/sbin/elasticsearch

echo " - Copying elasticsearch systemd file"
sudo cp $SCRIPT_DIR/utils/elasticsearch.service /lib/systemd/system/
sudo chmod 644 /lib/systemd/system/elasticsearch.service

echo " - Checking Systemd file"
if [[ `systemctl status elasticsearch | grep 'could not be found'` != "" ]]; then
    echo "elasticsearch systemd file installation failed"
    exit -12
fi
sudo systemctl status elasticsearch > /tmp/es_install_log 2>&1
if [[ $? != 0 && $? != 3 ]]; then
    echo "elasticsearch systemd file doesn't work as expected"
    exit -12
fi  

echo " - Adapting configuration in file elasticsearch.yml"
#get_ip_address
sed -i.bak s/"#cluster.name: my-application"/"cluster.name: mes-es-cluster"/g /usr/local/lib/elasticsearch-$ES_VERSION/config/elasticsearch.yml 
sed -i s/"#node.name: node-1"/"node.name: $NODE_NAME"/g /usr/local/lib/elasticsearch-$ES_VERSION/config/elasticsearch.yml
sed -i s/"#path.data: \/path\/to\/data"/"path.data: \/var\/lib\/elasticsearch"/g /usr/local/lib/elasticsearch-$ES_VERSION/config/elasticsearch.yml
sed -i s/"#path.logs: \/path\/to\/logs"/"path.logs: \/usr\/local\/lib\/elasticsearch-$ES_VERSION\/logs"/g /usr/local/lib/elasticsearch-$ES_VERSION/config/elasticsearch.yml
sed -i s/"#bootstrap.memory_lock: true"/"bootstrap.memory_lock: false"/g /usr/local/lib/elasticsearch-$ES_VERSION/config/elasticsearch.yml
#sed -i s/"#network.host: 192.168.0.1"/"network.host: $IP_ADDRESS"/g /usr/local/lib/elasticsearch-$ES_VERSION/config/elasticsearch.yml
sed -i s/"#network.host: 192.168.0.1"/"network.host: 0.0.0.0"/g /usr/local/lib/elasticsearch-$ES_VERSION/config/elasticsearch.yml
sed -i s/"#discovery.zen.minimum_master_nodes: 3"/"discovery.zen.minimum_master_nodes: 1"/g /usr/local/lib/elasticsearch-$ES_VERSION/config/elasticsearch.yml

if [[ $IS_SLAVE == 1 ]]; then
    echo " - Adapting configuration in file elasticsearch.yml - enabling discovery of master"
    sed -i s/"#discovery.zen.ping.unicast.hosts: \[\"host1\", \"host2\"\]"/"discovery.zen.ping.unicast.hosts: \[\"$MASTER_IP\"\]"/g /usr/local/lib/elasticsearch-$ES_VERSION/config/elasticsearch.yml
fi

echo " - Addressing issue with multiple interfaces but only one global"
sudo bash -c "echo -e \"\n#If you set a network.host that results in multiple bind addresses yet rely on a specific address\" >> /usr/local/lib/elasticsearch-$ES_VERSION/config/elasticsearch.yml"
sudo bash -c "echo \"#for node-to-node communication, you should explicitly set network.publish_host.\" >> /usr/local/lib/elasticsearch-$ES_VERSION/config/elasticsearch.yml"
sudo bash -c "echo \"network.publish_host: $IP_ADDRESS\" >> /usr/local/lib/elasticsearch-$ES_VERSION/config/elasticsearch.yml"

echo " - Adapting configuration in file jvm.options"
sed -i s/"-Xms2g"/"-Xms1000m"/g /usr/local/lib/elasticsearch-$ES_VERSION/config/jvm.options
sed -i s/"-Xmx2g"/"-Xmx1000m"/g /usr/local/lib/elasticsearch-$ES_VERSION/config/jvm.options

echo " - Testing systemd startup - starting Elasticsearch"
sudo systemctl start elasticsearch > /tmp/es_install_log 2>&1
fail_if_error $? "/tmp/es_install_log" -1

echo " - Testing systemd startup - Checking startup"
sleep 8
sudo systemctl status elasticsearch > /tmp/es_install_log 2>&1
fail_if_error $? "/tmp/es_install_log" -1

echo " - Testing systemd startup - stopping elasticsearch"
sudo systemctl stop elasticsearch > /tmp/es_install_log 2>&1
fail_if_error $? "/tmp/es_install_log" -1

echo " - Enabling elasticsearch on startup"
sudo systemctl enable elasticsearch > /tmp/es_install_log 2>&1
fail_if_error $? "/tmp/es_install_log" -1




