#!/bin/bash

# Alias: NeDI Network Discovery
# Menu: Addons
# Description:
#  NeDI is a great addon for discover for network environments


case "$1" in
   default)
       echo "on"
   ;;
   choices)
       echo "on: use NeDI Network Discovery"
       echo "off: disable NeDI Network Discovery"
   ;;
   set)
        NEDI_CFG=${OMD_ROOT}/etc/apache/conf.d/nedi.conf

        if [ "$2" == "on" ]; then

        cat > $NEDI_CFG <<EOF
Alias /${OMD_SITE}/nedi "${OMD_ROOT}/local/share/nedi/html"

<Directory "${OMD_ROOT}/local/share/nedi/html">
  Options +Indexes +ExecCGI +FollowSymLinks
  AllowOverride None
  DirectoryIndex index.php
    <IfModule mod_access_compat.c>
        Order deny,allow
        Allow from all
    </IfModule>
    <IfModule !mod_access_compat.c>
        Require all granted
    </IfModule>
</Directory>
EOF



        mkdir -p ${OMD_ROOT}/local/share/nedi
        tar -C ${OMD_ROOT}/local/share/nedi -xzf /opt/ebs/share/nedi/omd-site.tar.gz
        cat /opt/ebs/share/nedi/nedi.conf | sed -e "s:###SITE###:${OMD_SITE}:g" -e "s:###ROOT###:${OMD_ROOT}:g"  > ${OMD_ROOT}/local/share/nedi/nedi.conf
        /usr/bin/mysqlshow  -uroot -pEagleEye | grep nedi\\_${OMD_SITE} >/dev/null 2>&1
        if [ $? == 1 ]; then
                ${OMD_ROOT}/local/share/nedi/nedi.pl -i root EagleEye >> ${OMD_ROOT}/var/log/nedi.log 2>&1
        else
		echo "" > /dev/null
                #${OMD_ROOT}/local/share/nedi/nedi.pl -i updatedb >> ${OMD_ROOT}/var/log/nedi.log 2>&1
        fi
        else
          [ -f $NEDI_CFG ] && rm $NEDI_CFG
        echo "DROP DATABASE IF EXISTS nedi_${OMD_SITE};" | mysql -hlocalhost -uroot -pEagleEye
        rm -rf ${OMD_ROOT}/local/share/nedi
        fi
        ;;
esac

