#!/bin/bash

PACKNAME=dsi-conf-nrpe
#CONFIG=/etc/${PACKNAME}/${PACKNAME}.conf
#source ${CONFIG}

SHAREDIR=/usr/share/${PACKNAME}
SPATCH=${SHAREDIR}/spatch
DPKGSTATEFILE=/var/run/${PACKNAME}.dpkg

CLEAN=no
RESTART=yes
SILENT=no
VERBOSE=no
DPKGOPT=no
APTMODE=no

function usage () {
    echo "$0: [--upgrade|--update||--clean|--revert] [--silent|--verbose] [--restart|--dont-restart] [--dpkg]"
    echo "--upgrade - updates configuration and restart the services"
    echo "--update - updates configuration but don't restart the services"
    echo "--clean - try to remove configuration and restart services"
    echo "--revert - remove the patchs for easier updates, so don't restart ther services"
    echo "--restart - restart of the services"
    echo "--dont-restart - do not restart the services"
    echo "--dpkg - only perform actions if dpkg mode is enabled"
    echo "--apt - run using apt mode"
}

# Tests if the patch is applied
function applied-patch () {
    if ${SPATCH} status $1 | grep -q " not applied " ; then
	# False
	return 1;
    else
	# True
	return 0;
    fi
}

# Deapply all the listed patches
function deapply-patches () {
    # Nao esta a funcionar
    for patch in $* ; do
	if applied-patch $patch ; then
	    ${SPATCH} deapply $patch
	fi
    done
    #${SPATCH} deapply $*
}

# Update to the most recent patch
function update-patches () {
    deapply-patches $*
    ${SPATCH} apply $1
}

# Condicionally update to the last patch
function update-clean-patches () {
    deapply-patches $*
    if [ $CLEAN = no ] ; then
	${SPATCH} apply $1
    fi
}

# Experimental function for adding line to sudoers
function append-sudoers () {
    if ! test -x /usr/sbin/visudo ; then
	return
    fi
    if ! fgrep -q "$1" /etc/sudoers ; then
	cp -p /etc/sudoers /etc/newsudoers
	echo >> /etc/newsudoers
	echo "$2" >> /etc/newsudoers
	echo "$1" >> /etc/newsudoers
	if visudo -q -c -s -f /etc/newsudoers ; then
	    echo 
	    echo "new sudoers generated with success and will be updated"
	    echo "Please check /etc/sudoers for possible errors"
	    mv /etc/newsudoers /etc/sudoers || return 1
	    echo "/etc/sudoers updated with success"
	else
	    visudo -c -s -f /etc/newsudoers 
	    echo
	    echo "new sudoers file /etc/newsudoers is broken"
	    echo "please report this bug to the package maintainer"
	    echo "The old /etc/sudoers is still in place"
	fi
    fi
}

######################################################################
# Funtions to configure packages



######################################################################
# Parsing options
while [ $# -ge 1 ] ; do
    case $1 in
	--upgrade)
	    CLEAN=no
	    RESTART=yes
	    shift
	    ;;
	--update)
	    CLEAN=no
	    RESTART=no
	    shift
	    ;;
	--clean)
	    CLEAN=yes
	    RESTART=yes
	    shift
	    ;;
	--revert)
	    CLEAN=yes
	    RESTART=no
	    shift
	    ;;
	--silent)
	    SILENT=yes
	    VERBOSE=no
	    shift
	    ;;
	--verbose)
	    SILENT=no
	    VERBOSE=yes
	    shift
	    ;;
	--restart)
	    RESTART=yes
	    shift
	    ;;
	--dont-restart)
	    RESTART=no
	    shift
	    ;;
	--dpkg)
	    DPKGOPT=yes
	    shift
	    ;;
	--apt)
	    APTMODE=yes
	    echo "Running in dpkg mode"
	    shift
	    ;;
	help)
	    usage
	    shift
	    exit 0
	    ;;
	*)
	    usage
	    CLEAN=no
	    RESTART=yes
	    shift
	    exit 1
    esac
done

if [ $DPKGOPT = "yes" ] ; then
    case $TpConfSrvDpkgMode in
	N*|n*)
	    if [ $SILENT = "no" ] ; then
		echo "dpkg mode disabled"
	    fi
	    exit 0
	    ;;
    esac
    if [ $CLEAN = "yes" ] ; then
	touch $DPKGSTATEFILE
    else
	rm -f $DPKGSTATEFILE
    fi
fi

case $TpConfSrvAptMode in
    Y*|y*)
	echo "Running in apt mode"
	if [ $APTMODE = "yes" ] ; then
	    cat > /tmp/$PACKNAME.apt-mode
	    exit 0
	fi
    ;;
    *)
	if [ $APTMODE = "yes" ] ; then
	    if [ $SILENT = "no" ] ; then
		echo "apt mode disabled"
	    fi
	    exit 0
	fi
esac


# configure /etc/sudoers for nrpe server
cp -p /etc/sudoers /etc/sudoers.old
append-sudoers "nagios ALL=(ALL) NOPASSWD: /usr/bin/arrayprobe" \
    "# Nagios NRPE privilege specification for check_hp_raid"
append-sudoers "nagios ALL=(ALL) NOPASSWD: /usr/lib/tp-nagios-plugins/check_3ware-raid -m" \
    "# Nagios NRPE privilege specification for check_3ware_raid"
append-sudoers "nagios ALL=(ALL) NOPASSWD: /usr/sbin/smartctl" \
    "# Nagios NRPE privilege specification for SMART checks"

