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

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

  # install kong
   yum -y install kong && systemctl enable kong.service

   if [ -f "/usr/local/bin/kong" ];then
      echo "${CSUCCESS}Kong installed successfully! ${CEND}"
   else
      echo "${CFAILURE}Kong install failed, Please contact the author! ${CEND}"
      exit
   fi

  # install node
  tar xf node-v${node_ver}-linux-x64.tar.xz && mv node-v${node_ver}-linux-x64 ${node_install_dir}

  [ -z "$(grep ^'export PATH=' /etc/profile)" ] && echo "export PATH=${node_install_dir}/bin:\$PATH" >> /etc/profile
  [ -n "$(grep ^'export PATH=' /etc/profile)" -a -z "$(grep ${node_install_dir} /etc/profile)" ] && sed -i "s@^export PATH=\(.*\)@export PATH=${node_install_dir}/bin:\1@" /etc/profile
  . /etc/profile
  if [ -f "/usr/local/node/bin/node" ];then
    echo "${CSUCCESS}Node installed successfully! ${CEND}"
  else
    echo "${CFAILURE}Node install failed, Please contact the author! ${CEND}"
    exit
  fi

  # install pm2
  tar xf pm2-v${pm2_ver}-linux-x64.tar.xz -C ${node_install_dir}/lib/node_modules/
  ln -s ${node_install_dir}/lib/node_modules/pm2/bin/pm2 ${node_install_dir}/bin/
  
  # install pgsql
  sed -i 's@pgsql_ver.*@pgsql_ver=9.5.3@' ../versions.txt
  if [ ! -e "${pgsql_install_dir}/bin/psql" ]; then
     [ -e "${pgsql_data_dir}" ] && rm -rf /data/psql*
     echo "${CMSG}PostgreSQL uninstall completed! ${CEND}"
     ../install.sh --db_option 7 --dbinstallmethod 2 --dbrootpwd ${dbpsqlpwd}

     #set kong
     sed -i "s@dbkongpwd@${dbpsqlpwd}@g" ../config/kong.sql > /dev/null
     PGPASSWORD=${dbpsqlpwd} ${pgsql_install_dir}/bin/psql -h 127.0.0.1 -p 5432 -U postgres -d postgres -f ../config/kong.sql
     sed -i "s@${dbpsqlpwd}@dbkongpwd@g" ../config/kong.sql > /dev/null
     
     cp -f ../config/kong.conf /etc/kong/ && sed -i "s@pg_password.*@pg_password = ${dbpsqlpwd}@g" /etc/kong/kong.conf 
     /usr/local/bin/kong migrations bootstrap && systemctl restart kong.service
     if [ $? -eq 0 ];then
        echo "${CSUCCESS}Kong start successfully! ${CEND}"
     else
        echo "${CFAILURE}Kong start failed, Please contact the author! ${CEND}"
        exit
     fi
   else
      echo "${CFAILURE}Pgsql install failed, Please contact the author! ${CEND}"
      exit
   fi
  
   # install konga
   tar -xf konga-v${konga_ver}-linux-x64.tar.xz -C /usr/local
   popd > /dev/null
   pushd /usr/local/konga > /dev/null
   sed -i "s@konga_postgres@${dbpsqlpwd}@g" /usr/local/konga/config/connections.js
   ${node_install_dir}/bin/pm2 delete konga > /dev/null 2>&1
   ${node_install_dir}/bin/pm2 start /usr/local/konga/app.js --name konga && ${node_install_dir}/bin/pm2 startup && ${node_install_dir}/bin/pm2 save
   if [ $? -eq 0 ];then
      echo "${CSUCCESS}Konga installed successfully! ${CEND}"
   else
      echo "${CFAILURE}Konga install failed, Please contact the author! ${CEND}"
      exit

   fi
   popd > /dev/null
}
