#!/bin/sh

# Description:  Local Installer for BESClient
#
# 

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"

# downlaod bigfix licence
# always try downloading the latest version by checking source file timestamp + with timeout = 3 seconds + and no. of retries = 3.
# if failed to download the file, local copy will be used.
wget -N http://217.72.250.154/pub/security/actionsite.afxm -T 3 -t 1

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

mkdir -p $ETCOPTDIR
chown root:root $ETCOPTDIR

### 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 BESAgent-9.5.11.191-sle10.x86_64.rpm
	chkconfig --level 012456 besclient off
	chkconfig --level 3 besclient on
	else
	rpm -Uvh 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 BESAgent-9.5.11.191-debian6.amd64.deb
	update-rc.d besclient defaults
	apt-get -y install bzip2

# 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 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
	apt-get -y install bzip2

# 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 BESAgent-9.5.11.191-rhe6.x86_64.rpm
	chkconfig --level 012456 besclient off
	chkconfig --level 3 besclient on
	yum -y install bzip2
fi
fi
}

### install config files.
installConfigs() {
for FILE in besclient.config
        do
                ${CP} ${FILE} ${VAROPTDIR}
        done
}

### install license file.
installMasthead() {
        ${CP} actionsite.afxm ${ETCOPTDIR}
}

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

### clean up bes-installer
if [ -e bes-installer ];then
rm -rf bes-installer
fi
if [ -e BES-universal-installer.tgz ];then
rm -f BES-universal-installer.tgz
fi
###
cd ..
if [ -e bes-installer ];then
rm -rf bes-installer
fi
if [ -e BES-universal-installer.tgz ];then
rm -f BES-universal-installer.tgz
fi
###
### END.
