#!/bin/sh

# Description:  Local Installer for BESClient
#
# 

CP="/bin/cp"
ECHO="/bin/echo"
VAROPTDIR="/var/opt/BESClient"
ETCOPTDIR="/etc/opt/BESClient"
MHECONFIG="besclient-mhe.config"
CHKCONFIG=`which chkconfig`
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

### make sure we have our arguments
if [ -z "$1" ]; then
        ${ECHO} "Usage: $0 {MHE}"
        ${ECHO} "  e.g.  $0 MHE"
        exit 1
fi

### install besclient rpm package
mkdir -p $ETCOPTDIR
chown root:root $ETCOPTDIR
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`
fi

DEBIAN=`lsb_release -d | grep -ic debian`

UBUNTU=`lsb_release -d | grep -ic ubuntu`

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 -Uvvh BESAgent-9.1.1117.0-sle9.x86_64.rpm
	chkconfig --level 012456 besclient off
	chkconfig --level 3 besclient on
	else
	rpm -Uvvh BESAgent-9.1.1117.0-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.1.1117.0-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 BESAgent-9.1.1134.0-ubuntu10.amd64.deb
	update-rc.d besclient defaults

# RHEL installer
else
	if [ $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 -Uvvh BESAgent-9.1.1117.0-rhe5.x86_64.rpm
	chkconfig --level 012456 besclient off
	chkconfig --level 3 besclient on
	fi

fi
fi
}

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

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

### which DC are we located?
case "$1" in
        MHE)
                installBes
                installConfigs
                installMasthead
                ${ECHO} "Copying besclient-mhe.config to besclient.config"
                ${CP} ${BESTMP}/${MHECONFIG} ${VAROPTDIR}/besclient.config
                /etc/init.d/besclient start
                ;;


	*)
		${ECHO} "MHE"
		${ECHO} $"Usage: $0 {MHE}"
		exit 1
		;;
esac
