#!/bin/bash

### this will install tripwire agent.

PASSPHRASE=$1
CLIENTNAME=$2
CONTINUOUS=$3

INSTALLHOME="/tmp/tripwire-installer"

# Add Path
export PATH=$PATH:/sbin:/bin

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

if [ -f /etc/SuSE-release ]
then
SUSE=`head -1 /etc/SuSE-release| awk '{print $1}'`
        case $SUSE in
                SUSE)    VER=`head -1 /etc/SuSE-release | awk '{print $5}'`;;
                         *)      ;;
        esac
elif [ -f /etc/redhat-release ]
then		
REL=`awk '{print $1}' /etc/redhat-release`
        case $REL in
                Red)    VER=`awk '{print $7}' /etc/redhat-release|awk -F . '{print $1}'`;;
                CentOS) VER=`awk -F release '{print $2}' /etc/redhat-release | awk -F . '{print $1}'`;;
                *)      ;;
        esac
elif [ -f /etc/system-release ]
then
VER=6
fi
ARCH=`uname -p`
if [ -n $VER ]; then
        if 		[ $VER = 5 ]; then
                case $ARCH in
                        i686)   tw_pkg='te_agent_x86.bin';;
                        x86_64) tw_pkg='te_agent_x86_64.bin';;
                esac

        elif	[ $VER = 6 ]; then
                case $ARCH in
                        i686)   tw_pkg='te_agent_x86.bin';;
                        x86_64) tw_pkg='te_agent_x86_64.bin';;
                esac

        elif	[ $VER = 7 ]; then
                case $ARCH in
                        i686)   tw_pkg='te_agent_x86.bin';;
                        x86_64) tw_pkg='te_agent_x86_64.bin';;
                esac

        elif	[ $VER = 10 ]; then
                case $ARCH in
                        i686)   tw_pkg='te_agent_x86.bin';;
                        x86_64) tw_pkg='te_agent_x86_64.bin';;
                esac

		elif	[ $VER = 11 ]; then
                case $ARCH in
                        i686)   tw_pkg='te_agent_x86.bin';;
                        x86_64) tw_pkg='te_agent_x86_64.bin';;
                esac

		else
                echo "Error getting OS version"
        fi
		
#On some systems they change the perms and set the umask to 077 - so lets make it what we want it
umask 022

if [ -e /etc/oracle-release ] ; then 

$INSTALLHOME/$tw_pkg --IgnoreOSVersion true --eula accept --silent \
--server-host tripwire-web.datapipe.net --install-rtm false --server-port 9898 --install-rtm false --passphrase $PASSPHRASE

else
$INSTALLHOME/$tw_pkg --IgnoreOSVersion true --eula accept --silent \
--server-host tripwire-web.datapipe.net --server-port 9898 --passphrase $PASSPHRASE

fi
#------------
if [ -e /etc/oracle-release ] ; then
rpm -qa | grep teagent-realtime | xargs rpm -e
else
echo "The is not OEL"

fi
#-----------

serverpublicip=`curl http://whatismyip.datapipe.net/?scripted=1`

echo "java.rmi.server.hostname=$serverpublicip" >> /usr/local/tripwire/te/agent/data/config/agent.properties
echo "209.18.64.140 tripwire-web.datapipe.net tripwire-web" >> /etc/hosts
echo Continuous Audit:$CONTINUOUS >> /usr/local/tripwire/te/agent/data/config/agent.tags.conf
echo Client:$CLIENTNAME >> /usr/local/tripwire/te/agent/data/config/agent.tags.conf
/usr/local/tripwire/te/agent/bin/twdaemon start

fi
#
