#!/bin/bash -x

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

# log files
# date=`date +%F`
# script /tmp/tripwire_$date.log

#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 /tmp/tw
cd /tmp/tw

pkgloc="http://64.106.194.103/NotarnicolApps/tripwire/"
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 '{print $3}' /etc/redhat-release|awk -F . '{print $1}'`;;
 #               *)      dpkg;;
                                *)      ;;
        esac
fi
ARCH=`uname -p`
if [ -n $VER ]; then
        if 		[ $VER = 5 ]; then
                case $ARCH in
                        i686)   pkg='te_agent_8.3.7_en_linux_x86.tgz';;
                        x86_64) pkg='te_agent_8.3.7_en_linux_x86_64.tgz';;
                esac
                        echo "Downloading Package:"; cd /tmp/tw ; wget ${pkgloc}${pkg};

        elif	[ $VER = 6 ]; then
                case $ARCH in
                        i686)   pkg='te_agent_8.3.7_en_linux_x86.tgz';;
                        x86_64) pkg='te_agent_8.3.7_en_linux_x86_64.tgz';;
                esac
                        echo "Downloading Package:"; cd /tmp/tw ; wget ${pkgloc}${pkg};

        elif	[ $VER = 7 ]; then
                case $ARCH in
                        i686)   pkg='te_agent_8.3.7_en_linux_x86.tgz';;
                        x86_64) pkg='te_agent_8.3.7_en_linux_x86_64.tgz';;
                esac
                        echo "Downloading Package:"; cd /tmp/tw ; wget ${pkgloc}${pkg};

        elif	[ $VER = 10 ]; then
                case $ARCH in
                        i686)   pkg='te_agent_8.3.7_en_linux_x86.tgz';;
                        x86_64) pkg='te_agent_8.3.7_en_linux_x86_64.tgz';;
                esac
                        echo "Downloading Package:"; cd /tmp/tw ; wget ${pkgloc}${pkg};

		elif	[ $VER = 11 ]; then
                case $ARCH in
                        i686)   pkg='te_agent_8.3.7_en_linux_x86.tgz';;
                        x86_64) pkg='te_agent_8.3.7_en_linux_x86_64.tgz';;
                esac
                        echo "Downloading Package:"; cd /tmp/tw ; wget ${pkgloc}${pkg};		

		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

cd /tmp/tw
tar -zxvf te_agent_*
cd te_agent*
if [ -e /etc/oracle-release ] ; then 

./te_agent.bin --IgnoreOSVersion true --eula accept --silent \
--server-host tripwire.datapipe.net --install-rtm false --server-port 9898 --install-rtm false --passphrase uMpQaqn8Jm

else
./te_agent.bin --IgnoreOSVersion true --eula accept --silent \
--server-host tripwire.datapipe.net --install-rtm false --server-port 9898 --passphrase uMpQaqn8Jm

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

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


/sbin/service iptables stop
curl http://whatismyip.datapipe.net/?scripted=1 > /tmp/tw/public-ip

serverpublicip=`cat /tmp/tw/public-ip`

echo "java.rmi.server.hostname=$serverpublicip" >> /usr/local/tripwire/te/agent/data/config/agent.properties
cat /usr/local/tripwire/te/agent/data/config/agent.properties
echo "########################"
echo "64.106.188.163 tripwire.datapipe.net tripwire" >> /etc/hosts
cat /etc/hosts | grep "64.106.188.163 tripwire.datapipe.net tripwire"
echo "########################"
/usr/local/tripwire/te/agent/bin/twdaemon start
grep  Starting /usr/local/tripwire/te/agent/data/log/*
echo "########################"
grep ... /usr/local/tripwire/te/agent/data/log/teagent.log
echo "########################"
cd /tmp
rm -rf /tmp/tw

/sbin/service iptables start
fi

