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

export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
clear
printf "
#######################################################################
#              Author:  EinicYeo <einicyeo AT gmail.com>              #
#                 EnvoyinStack for CentOS/RedHat 6+                   #
#              Upgrade Software versions for EnvoyinStack             #
#      For more information please visit https://www.infvie.com       #
#######################################################################
"
# Check if user is root
[ $(id -u) != "0" ] && { echo "${CFAILURE}Error: You must be root to run this script${CEND}"; exit 1; }

envoyinstack_dir=$(dirname "`readlink -f $0`")
pushd ${envoyinstack_dir} > /dev/null
. ./versions.txt
. ./config/options.conf
. ./include/color.sh
. ./include/check_os.sh
. ./include/check_dir.sh
. ./include/download.sh
. ./include/get_char.sh
. ./include/upgrade_web.sh
. ./include/upgrade_db.sh
. ./include/upgrade_redis.sh
. ./include/upgrade_envoyinstack.sh

# get the IP information
PUBLIC_IPADDR=$(./include/get_public_ipaddr.py)
IPADDR_COUNTRY=$(./include/get_ipaddr_state.py ${PUBLIC_IPADDR})

Show_Help() {
  echo
  echo "Usage: $0  command ...[version]....
  --help, -h                  Show this help message
  --nginx        [version]    Upgrade Nginx
  --openresty    [version]    Upgrade OpenResty
  --tomcat       [version]    Upgrade Tomcat
  --db           [version]    Upgrade MySQL/MariaDB
  --redis        [version]    Upgrade Redis
  --envoyinstack              Upgrade EnvoyinStack latest
  "
}

ARG_NUM=$#
TEMP=`getopt -o h --long help,nginx:,openresty:,tomcat:,db:,redis:,envoyinstack -- "$@" 2>/dev/null`
[ $? != 0 ] && echo "${CWARNING}ERROR: unknown argument! ${CEND}" && Show_Help && exit 1
eval set -- "${TEMP}"
while :; do
  [ -z "$1" ] && break;
  case "$1" in
    -h|--help)
      Show_Help; exit 0
      ;;
    --nginx)
      nginx_flag=y; NEW_nginx_ver=$2; shift 2
      ;;
    --openresty)
      openresty_flag=y; NEW_openresy_ver=$2; shift 2
      ;;
    --tomcat)
      tomcat_flag=y; NEW_tomcat_ver=$2; shift 2
      ;;
    --db)
      db_flag=y; NEW_db_ver=$2; shift 2
      ;;
    --redis)
      redis_flag=y; NEW_redis_ver=$2; shift 2
      ;;
    --envoyinstack)
      NEW_envoyinstack_ver=latest; shift 1
      ;;   
    --)
      shift
      ;;
    *)
      echo "${CWARNING}ERROR: unknown argument! ${CEND}" && Show_Help && exit 1
      ;;
  esac
done

Menu() {
  while :; do
    printf "
What Are You Doing?
\t${CMSG} 1${CEND}. Upgrade Nginx/OpenResty
\t${CMSG} 2${CEND}. Upgrade Tomcat
\t${CMSG} 3${CEND}. Upgrade MySQL/MariaDB
\t${CMSG} 4${CEND}. Upgrade Redis
\t${CMSG} 5${CEND}. Upgrade EnvoyinStack latest
\t${CMSG} q${CEND}. Exit
"
    echo
    read -e -p "Please input the correct option: " Upgrade_flag
    if [[ ! "${Upgrade_flag}" =~ ^[1-5,q] ]]; then
      echo "${CWARNING}input error! Please only input 1~5 and q${CEND}"
    else
      case "${Upgrade_flag}" in
        1)
          [ -e "${nginx_install_dir}/sbin/nginx" ] && Upgrade_Nginx
          [ -e "${openresty_install_dir}/nginx/sbin/nginx" ] && Upgrade_OpenResty
          ;;
        2)
          Upgrade_Tomcat
          ;;
        3)
          Upgrade_DB
          ;;
        4)
          Upgrade_Redis
          ;;
        5)
          Upgrade_EnvoyinStack
          ;;
        q)
          exit
          ;;
      esac
    fi
  done
}

if [ ${ARG_NUM} == 0 ]; then
  Menu
else
  [ "${nginx_flag}" == 'y' ] && Upgrade_Nginx
  [ "${openresty_flag}" == 'y' ] && Upgrade_OpenResty
  [ "${tomcat_flag}" == 'y' ] && Upgrade_Tomcat
  [ "${db_flag}" == 'y' ] && Upgrade_DB
  [ "${redis_flag}" == 'y' ] && Upgrade_Redis
  [ "${NEW_envoyinstack_ver}" == 'latest' ] && Upgrade_EnvoyinStack
fi
