#!/bin/bash
# Author:  EinicYeo <einicyeo AT gmail.com>
# BLOG:  https://www.infvie.com
#
# Notes: EnvoyinStack for CentOS/RedHat 6+

Install_Elasticstack() {
  pushd ${envoyinstack_dir}/src > /dev/null

  # install elasticstack
    yum -y install elasticsearch-${elasticstack_ver}-1.x86_64.rpm && systemctl enable elasticsearch.service && systemctl start elasticsearch.service
    if [ -f "/usr/local/elasticsearch/bin/elasticsearch" ];then
      echo "${CSUCCESS}Elasticsearch installed successfully! ${CEND}"
      printf"
     --------------------------------------------------------------------------------
     *                    The platinum version never expires                        *
     * After the deployment is completed, you can enter the following operations:   *
     * bin/elasticsearch-setup-passwords auto|interactive                           *
     * auto                                                                         *
     * Outputs randomly-generated passwords to the console.                         *
     * interactive                                                                  *
     * Prompts you to manually enter passwords.                                     *
     --------------------------------------------------------------------------------
     * Commercial support is available at                                           *
     *   https://www.infvie.com                                                     *
     --------------------------------------------------------------------------------
    "
   else
      echo "${CFAILURE}Elasticsearch install failed, Please contact the author! ${CEND}"
      exit
   fi
  
   yum -y install kibana-${elasticstack_ver}-1.x86_64.rpm && systemctl enable kibana.service
  
   if [ -f "/usr/local/kibana/bin/kibana" ];then
      echo "${CSUCCESS}Kibana installed successfully! ${CEND}"
      printf"
      --------------------------------------------------------------------------------
      * To use Kibana with security features:                                        *
      *                                                                              *
      * Configure security in Elasticsearch.                                         *
      * Configure Kibana to use the appropriate built-in user.                       *
      *                                                                              *
      * Update the following settings in the kibana.yml configuration file:          *
      *                                                                              *
      * elasticsearch.username: "kibana_system"                                      *
      * elasticsearch.password: "kibanapassword"                                     *
      --------------------------------------------------------------------------------
      * Commercial support is available at                                           *
      *   https://www.infvie.com                                                     *
      --------------------------------------------------------------------------------  
      "
  else
      echo "${CFAILURE}Kibana install failed, Please contact the author! ${CEND}"
      exit
   fi
  popd > /dev/null
}

Uninstall_Elasticstack() {
  if [ -f "/usr/local/elasticsearch/bin/elasticsearch" ]; then
    echo "${CMSG}Elasticsearch uninstall completed${CEND}"
    yum -y remove elasticsearch-${elasticstack_ver}-1.x86_64.rpm
  fi
  
  if [ -f "/usr/local/kibana/bin/kibana" ]; then
    echo "${CMSG}Kibana uninstall completed${CEND}"
    yum -y remove kibana-${elasticstack_ver}-1.x86_64.rpm
  fi
}

