#!/bin/bash
#
# NixStats Installer
#
# @version		1.0.1
# @date			2016-03-03
# @copyright	(c) 2014 http://www.nixstats.com
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

# Set environment
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export LC_ALL=C

if [ $(id -u) != "0" ];
then
	echo "The installer needs to be run by a user with root privilege."
	exit 1
fi

if [ $# -lt 1 ]; then
    echo "NIXStats userid missing from installer command."
    exit 1
fi

if [ -n "$(command -v apt-get)" ]
then
    apt-get -q -y --force-yes update >/dev/null 2>&1
    apt-get -q -y --force-yes install gzip curl make gcc xz-utils build-essential >/dev/null 2>&1
    service cron start >/dev/null 2>&1
    if [ ! -d /opt ]
    then
        mkdir /opt
    fi
    cd /opt
    wget http://perso.orange.fr/sebastien.godard/sysstat-11.4.3.tar.xz >/dev/null 2>&1
    unxz sysstat-11.4.3.tar.xz >/dev/null 2>&1
    tar -xvf sysstat-11.4.3.tar >/dev/null 2>&1
    cd sysstat-11.4.3 >/dev/null 2>&1
    echo "Configuring sysstat..."
    ./configure --prefix=/opt/sysstat --disable-file-attr --disable-nls >/dev/null 2>&1
    echo "Making sysstat..."
    make >/dev/null 2>&1
    echo "Installing sysstat..."
    make install >/dev/null 2>&1
    cd ../
    rm -rf sysstat-11.4.3*

    if [ ! -f /opt/sysstat/bin/sar ]
    then
        apt-get -q -y --force-yes install sysstat >/dev/null 2>&1
    fi
fi

if [ -n "$(command -v yum)" ]
then
    yum -d0 -e0 -y install cronie gzip curl gcc make xz >/dev/null 2>&1
    service crond start >/dev/null 2>&1
    if [ ! -d /opt ]
    then
        mkdir /opt
    fi
    cd /opt
    wget http://perso.orange.fr/sebastien.godard/sysstat-11.4.3.tar.xz >/dev/null 2>&1
    unxz sysstat-11.4.3.tar.xz >/dev/null 2>&1
    tar -xvf sysstat-11.4.3.tar >/dev/null 2>&1
    cd sysstat-11.4.3 >/dev/null 2>&1
    echo "Configuring sysstat..."
    ./configure --prefix=/opt/sysstat --disable-file-attr --disable-nls >/dev/null 2>&1
    echo "Making sysstat..."
    make >/dev/null 2>&1
    echo "Installing sysstat..."
    make install >/dev/null 2>&1
    cd ../
    rm -rf sysstat-11.4.3*

    if [ ! -f /opt/sysstat/bin/sar ]
    then
        yum -d0 -e0 -y install sysstat >/dev/null 2>&1
    fi
fi

command -v crontab >/dev/null 2>&1 || { echo "cron is required but it's not installed.  Aborting." >&2; exit 1; }
command -v curl >/dev/null 2>&1 || { echo "curl is required but it's not installed.  Aborting." >&2; exit 1; }

if [ ! -d /etc/nixstats ]
then
mkdir -p /etc/nixstats
fi

if [ ! -d /opt/nixstats ]
then
mkdir -p /opt/nixstats
fi

if [ ! -d /tmp/nixstats/retry ]
then
mkdir -p /tmp/nixstats/retry
fi

#remove old agent and download a new one
if [ -f /etc/nixstats/nixstats.sh ]
then
    rm -f /etc/nixstats/nixstats.sh
fi

curl -k -o /opt/nixstats/nixstats.sh https://www.nixstats.com/nixstats2.sh >/dev/null 2>&1

if [ ! -f /etc/nixstats/user ]
then
echo "$1" > /etc/nixstats/user
fi

if [ ! -f /etc/nixstats/serverid ]
then
echo "$(ip addr | grep inet) $(hostname)"  | sha256sum | awk '{print $1}' > /etc/nixstats/serverid
fi

if [ ! -f /etc/nixstats/token ]
then
    touch /etc/nixstats/token
    token=$(wget --no-check-certificate -qO- --post-data="user=$1&tags=${@:2}&hostname="`hostname -s` https://api.nixstats.com/hello.php)
    if [ ${#token} -eq 24 ]
    then
        echo "$token">/etc/nixstats/token
    else
        echo "Error: could not create server token. Got "`$token`
        exit 1
    fi
fi

if id -u nixstats >/dev/null 2>&1; then
        echo "User nixstats found."
        userdel nixstats
        useradd nixstats -r -d /opt/nixstats -s /bin/false
        chown -R nixstats:nixstats /opt/nixstats
        chown -R nixstats:nixstats /tmp/nixstats
        chown -R nixstats:nixstats /etc/nixstats
        chmod -R 700 /opt/nixstats
else
        echo "Adding user nixstats."
        useradd nixstats -r -d /opt/nixstats -s /bin/false
        chown -R nixstats:nixstats /opt/nixstats
        chown -R nixstats:nixstats /tmp/nixstats
        chown -R nixstats:nixstats /etc/nixstats
        chmod -R 700 /opt/nixstats
fi

#remove old cronjobs and add new ones
crontab -u nixstats -l | grep -v 'nixstats'  | crontab -u nixstats - >/dev/null 2>&1
crontab -u nixstats -l 2>/dev/null | { cat; echo "* * * * * bash /opt/nixstats/nixstats.sh > /dev/null 2>&1"; } | crontab -u nixstats - >/dev/null 2>&1

#remove the root cronjob
crontab -u root -l | grep -v 'nixstats'  | crontab -u root - >/dev/null 2>&1

echo "Installation complete!"

if [ -f $0 ]
then
    rm -f $0
fi