#!/bin/sh

# Description:  Upgrade BESClient to latest version
#
# 

CP="/bin/cp"
ECHO="/bin/echo"
VAROPTDIR="/var/opt/BESClient"
ETCOPTDIR="/etc/opt/BESClient"
CONFIG="besclient.config"
MKDIR="/bin/mkdir -p"
CHMOD="/bin/chmod 755" 
BESTMP="`pwd`"
UNAME=`which uname`
OS=`${UNAME} -s`
BESINIT="besclient"

### verify script is running as root
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi

### install besclient rpm package.
installBes() {

SUSE=0
if [ -e /etc/SuSE-release ]; then
SUSE=`grep -ic suse /etc/SuSE-release`
fi

REDHAT=0
if [ -e /etc/redhat-release ]; then
REDHAT=`grep -icE 'Red Hat|CentOS|Oracle' /etc/redhat-release`
elif [ -e /etc/system-release ]; then
REDHAT=`grep -icE "Amazon Linux AMI|Amazon Linux release|Amazon Linux 2|Aliyun Linux" /etc/system-release`
fi

if [ -e /usr/bin/lsb_release ]; then
DEBIAN=`lsb_release -d | grep -ic debian`
UBUNTU=`lsb_release -d | grep -ic ubuntu`
else
DEBIAN=0
UBUNTU=0
fi

if [ `uname -m` = "x86_64" ]; then

# SUSE installer
if [ $SUSE -eq 1 ]; then
	# this is a SuSE server, install the SuSE package.
	echo "this is a SuSE server, install the SuSE package ..."
	SUSE_VERSION=`grep VERSION /etc/SuSE-release | awk '{printf ("%d\n", $3 * 100)}'`
	if [ $SUSE_VERSION -lt 1099 ];then
	rpm -Uvh /tmp/bes-installer/BESAgent-9.5.11.191-sle10.x86_64.rpm
	chkconfig --level 012456 besclient off
	chkconfig --level 3 besclient on
	else
	rpm -Uvh /tmp/bes-installer/BESAgent-9.5.11.191-sle11.x86_64.rpm
	chkconfig --level 012456 besclient off
	chkconfig --level 3 besclient on
	fi

# Debian installer
elif [ $DEBIAN -ne 0 ];then
	# this is a Debian Linux, install the Debian package.
	echo "this is a Debian Linux, install the Debian package ..."
	dpkg -i /tmp/bes-installer/BESAgent-9.5.11.191-debian6.amd64.deb
	update-rc.d besclient defaults

# Ubuntu installer
elif [ $UBUNTU -ne 0 ];then
	# this is a Ubuntu Linux, install the Ubuntu package.
	echo "this is a Ubuntu Linux, install the Ubuntupackage ..."
	dpkg -i /tmp/bes-installer/BESAgent-9.5.11.191-ubuntu10.amd64.deb
	update-rc.d besclient defaults
	chown -R root:root /var/opt/BESClient
	chown -R root:root /etc/opt/BESClient
	chown -R root:root /opt/BESClient
	chown root:root /etc/init.d/besclient

# RHEL installer
elif [ $REDHAT -eq 1 ];then
	# this is a RHEL/CentOS/Oracle Linux, install the Redhat package.
	echo "this is a RHEL/CentOS/Oracle Linux, install the Redhat package ..."
	rpm -Uvh /tmp/bes-installer/BESAgent-9.5.11.191-rhe6.x86_64.rpm
	chkconfig --level 012456 besclient off
	chkconfig --level 3 besclient on
fi
fi
}


### install besclient.
                installBes
                /etc/init.d/besclient start

### END.
