#!/bin/bash
#
#    TDC: Test-driven configuration
#
#    generator of nagios config files
#
#    Copyright (C) 2020  Thorsten Alteholz
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation in version 2 of the License.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <https://www.gnu.org/licenses/>.
#

DEST=$1
WHAT=$2
APPEND=$3
PARA1="$4"
PARA2="$5"
PARA3="$6"

OUT=$DEST-$WHAT.cfg

if [ "x$APPEND" == "xonlycreateonce" ]; then
  # we only want to create the file once
  if [ -f $OUT ]; then
    exit 0
  fi
fi
if [ "x$APPEND" == "xno" ]; then
  rm -f $OUT
fi

if [ "x$PARA1" == "xdummy" ]; then
  # we just want to delete the file, do nothing more
  exit 0
fi

if [ "x$WHAT" == "xservice" ]; then
  # for tdc_manual we have $PARA2 containg the name of the manual test
  # all other services should have an empty string
  echo "define service {" >> $OUT
  echo "        hostgroup_name                  $PARA1" >> $OUT
  if [ -z "$PARA2" ]; then
    echo "        service_description             Service for `basename $DEST`" >> $OUT
  else
    echo "        service_description             Service $PARA2 for `basename $DEST`" >> $OUT
  fi
  echo "        check_command                   check_nrpe_1arg!$PARA1" >> $OUT
  echo "        use                             generic-service" >> $OUT
  echo "        notification_interval           0" >> $OUT
  echo "}" >> $OUT

  exit 0
fi

if [ "x$WHAT" == "xhost" ]; then
  # we can not give all stuff as parameter here, so we use a hostdb and grep everything
  hostuse=$PARA2
  if [ -f $PARA3 ]; then
    hostentry=`grep $PARA1 $PARA3`
    if [ ! -z "$hostentry" ]; then
      hostuse=`echo $hostentry|awk -F"#" '{printf("%s\n",$2)}'`
      if [ -z "$hostuse" ]; then
        # we have an empty "use" field in the database, so use default again
        hostuse=$PARA2
      fi
      hostaddress=`echo $hostentry|awk -F"#" '{printf("%s\n",$3)}'`
    fi
  fi
  # in case of an empty value, better do nothing
  if [ "x$PARA1" == "x" ]; then
    exit 2
  fi
  if [ "x$hostuse" == "x" ]; then
    exit 3
  fi
  echo "define host{" >> $OUT
  echo "        use                     $hostuse" >> $OUT
  echo "        host_name               $PARA1" >> $OUT
  if [ ! -z "$hostaddress" ]; then
     echo "        address                 $hostaddress" >> $OUT
  fi 
  echo "        }" >> $OUT

  exit 0
fi

if [ "x$WHAT" == "xhostgroup" ]; then
  echo "define hostgroup {" >> $OUT
  echo "        hostgroup_name  $PARA1" >> $OUT
  echo "        alias           $PARA1" >> $OUT
  echo "        members         $PARA2" >> $OUT
  echo "        }" >> $OUT

  exit 0
fi

exit 1
