#!/bin/bash
DEVICE_KEY=''
OS_BINARY_TYPE=''
THIRTY_TWO_BIT='32-bit'
SIXTY_FOUR_BIT='64-bit'

getHardwarePlatform() {       
    if [ "`which file`" = "" ]; then       
        if [ `/usr/bin/getconf LONG_BIT` = "64" ]; then
            OS_BINARY_TYPE="$SIXTY_FOUR_BIT"
        elif [ `/usr/bin/getconf LONG_BIT` = "32" ]; then
            OS_BINARY_TYPE="$THIRTY_TWO_BIT"
        fi
    else
        if /usr/bin/file /sbin/init | grep 'ELF 64-bit' &>/dev/null; then
            OS_BINARY_TYPE="$SIXTY_FOUR_BIT"
        elif /usr/bin/file /sbin/init | grep 'ELF 32-bit' &>/dev/null; then
            OS_BINARY_TYPE="$THIRTY_TWO_BIT"
        elif [ `/usr/bin/getconf LONG_BIT` = "64" ]; then
            OS_BINARY_TYPE="$SIXTY_FOUR_BIT"
        elif [ `/usr/bin/getconf LONG_BIT` = "32" ]; then
            OS_BINARY_TYPE="$THIRTY_TWO_BIT"
        fi
    fi   
}   

installAgent() {
    if [ ! -f /etc/init.d/site24x7monagent ]; then
        getHardwarePlatform
        if [ "$OS_BINARY_TYPE" == "$THIRTY_TWO_BIT" ]; then
            wget https://plus.site24x7.com//sagent//Site24x7_Linux_32bit.install
            chmod 755 Site24x7_Linux_32bit.install
            ./Site24x7_Linux_32bit.install -i -key=$DEVICE_KEY               
        else
            wget https://plus.site24x7.com//sagent//Site24x7_Linux_64bit.install
            chmod 755 Site24x7_Linux_64bit.install
            ./Site24x7_Linux_64bit.install -i -key=$DEVICE_KEY
        fi
    else
        echo "Site24x7 monitoring agent is already installed in this machine"
    fi
}

installAgent
