#!/bin/sh
#
# Written by: Scott Wilkerson (nagios@nagios.org)
# Copyright (c) 2010-2016 Nagios Enterprises, LLC.
# 
# NRDS Client Install Script
###########################

. ./init.sh

PROGNAME=`basename $0`

get_interval() {
    os=$1
    interval=$2

    case $os in
        AIX|HP-UX|SunOS)
            if [ $interval -eq 1 ] ; then 
                outputinterval="*"
            else
                minute=0
                while [ $minute -lt 60 ] ; do
                    if [ "x$outputinterval" != "x" ] ; then
                        outputinterval="${outputinterval},"
                    fi
                    outputinterval="${outputinterval}${minute}"
                    minute=`expr $minute + $interval`
                done
            fi
            ;;
        Darwin|Linux)
            outputinterval="*/$interval"
            ;;
    esac
    echo $outputinterval
}

print_usage() {
    echo ""
    echo "$PROGNAME - NRDS Installation Script"
    echo ""
    echo "Usage: $PROGNAME <HOSTNAME> <INTERVAL>"
    echo ""
    echo "HOSTNAME - The name sent to the Nagios server to identify this host (no spaces)"
    echo "INTERVAL - Frequency in minutes the checks should run 1-59"
}
if [ "x$3" != "x" ];then
    echo "ERROR: Only expected 2 arguments."
    print_usage
    exit 1
fi
if [ "x$1" = "x" ] || [ "x$2" = "x" ];then
    print_usage
    exit 1
fi
cronhost="$1"
croninterval="$2"
installdir="$nrdpdir/clients"

# Ensure bash is installed
which bash | grep "^/" > /dev/null 2> /dev/null
if [ $? -eq 0 ] ; then
    bash=`which bash`
else
    echo "The NRDS client requires bash. Please install bash and then re-run this script."
    exit 1
fi

# Ensure perl is installed
which perl | grep "^/" > /dev/null 2> /dev/null
if [ $? -eq 0 ] ; then
    perl=`which perl`
else
    echo "The NRDS client requires perl. Please install perl and then re-run this script."
    exit 1
fi

. ./1-usersgroups

echo "Installing NRDS Client"
echo ""
eval `grep "^SEND_NRDP" nrds/nrds.cfg | sed -e 's/^SEND_NRDP/SEND_DIR/' | sed -e 's/\/send_nrdp\.sh//'`
mkdir -p "$SEND_DIR"
cp send_* $SEND_DIR
chmod 775 $SEND_DIR/send_*
mkdir -p "$installdir/nrds"
cp nrds/nrds.cfg nrds/nrds_common.pl "$installdir/nrds"
cat nrds/nrds.pl |\
    sed -e "s%#!/usr/bin/perl%#!$perl%" |\
    sed -e "s%^my \$configfile = .*%my \$configfile = \"$installdir/nrds/nrds.cfg\";%" |\
    sed -e "s%my \$PATH=.*%my \$PATH=\"$PATH\";%" |\
    sed -e "s%require \"nrds_common\.pl.*%require \"$installdir/nrds/nrds_common.pl\";%" |\
    sed -e "s%^my \$nrds_updater = .*%my \$nrds_updater = \"$installdir/nrds/nrds_updater.pl\";%" \
    > "$installdir/nrds/nrds.pl"
cat nrds/nrds_updater.pl |\
    sed -e "s%#!/usr/bin/perl%#!$perl%" |\
    sed -e "s%^my \$configfile = .*%my \$configfile = \"$installdir/nrds/nrds.cfg\";%" |\
    sed -e "s%my \$PATH=.*%my \$PATH=\"$PATH\";%" |\
    sed -e "s%require \"nrds_common\.pl.*%require \"$installdir/nrds/nrds_common.pl\";%" \
    > "$installdir/nrds/nrds_updater.pl"
chown -R $user:$group "$installdir"
chmod -R ug+xw "$installdir"

# This is different in many OS's, we may need to split this to a seperate script
echo "Adding cron jobs for $cronhost at a $croninterval minute interval"
echo ""
crontmp="/tmp/cron-${cronhost}"
touch "$crontmp"
crontabinterval=`get_interval $os $croninterval`
# this might not be right
case $os in
    AIX|HP-UX|SunOS)
        crontab -l $user |\
                sed "/-H '$cronhost'/d" > "$crontmp"
        echo "$crontabinterval * * * * $installdir/nrds/nrds.pl -H '$cronhost' > /dev/null 2>&1" >> "$crontmp"
        echo "" >> "$crontmp"
        su - $user -c "crontab \"$crontmp\""
        ;;
    Darwin)
        crontab -u $user -l |\
                sed "/-H '$cronhost'/d" > "$crontmp"
        echo "$crontabinterval * * * * $installdir/nrds/nrds.pl -H '$cronhost' > /dev/null 2>&1\n" >> "$crontmp"
        echo "" >> "$crontmp"
        crontab -u $user "$crontmp"
        ;;
    Linux)
        crontab -u $user -l |\
                sed "/-H '$cronhost'/d" > "$crontmp"
        `which echo` "$crontabinterval * * * * $installdir/nrds/nrds.pl -H '$cronhost' > /dev/null 2>&1" >> "$crontmp"
        `which echo` "" >> "$crontmp"
        crontab -u $user "$crontmp"
        
        ;;
esac

# Make sure the user is not in the cron.deny file and, if the cron.allow file
# exists, is in the cron.allow file
if [ -f $crondeny ] ; then
    # Determine whether user is in cron.deny file
    grep $user $crondeny > /dev/null 2> /dev/null
    if [ $? -eq 0 ] ; then
        # If so, remove them from that file
        cp $crondeny /tmp/cron.deny
        grep -v $user /tmp/cron.deny > $crondeny
        rm /tmp/cron.deny
    fi
fi
if [ -f $cronallow ] ; then
    # If the cron.allow file exists, determine whether the user is in that file
    grep $user $cronallow > /dev/null 2> /dev/null
    if [ $? -eq 1 ] ; then
        # If not, add them to that file
        echo $user >> $cronallow
    fi
fi
echo "Crontabs installed OK"


echo "Updating config and plugins"
update="`$installdir/nrds/nrds_updater.pl -f -H '$cronhost'`"
echo $update
echo "Installation complete"
exit $ret
