#!/bin/sh
# vim:set sw=4 ts=4:
#
# $Id: make_snmpd,v 1.5 2005/04/11 14:14:39 fabian Exp $
#
#############################################################################
#
# ALICE
# Automatic Linux Installation and Configuration Environment
#
# Copyright (c) 2000-2002 SuSE Linux Solutions AG, Eschborn, Germany
#               2002-2004 SuSE Linux AG, Eschborn, Germany
#               2005           SUSE GmbH, Nuernberg, Germany
#
# 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; either version 2 of the License, or
# (at your option) any later version.
#
# 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#############################################################################
#
# Author: Stephan Siano
#
#############################################################################
#
# make_snmpd
# configures the snmpd
# TODO: Check if it works with newest versions of snmpd
#
# Sections: snmpd
# Tags:     SNMPD_CONF, SNMPD_COMMUNITY
#
test -n "$alice_dir" || alice_dir="/usr/lib/alice2/"
export alice_dir

while [ $# -gt 0 ]
do
   case $1 in
       -h | --help | -? ) usage
                          exit 1
                          ;;
       -fqhn ) export fqhn=$2; shift
            ;;

   esac
   shift
done   

. $alice_dir/lib/alicerc

WELCOME

std_snmpd_conf=/etc/snmpd.conf
#
export std_snmpd_conf
############################################################################
#
# save the old configuration
#
test -f ${std_snmpd_conf} && BACKUP --error ${std_snmpd_conf} 
#
# First priority is on a special snmpd.con file
#
#config_file=$(get_config_file snmpd.conf) || FATAL NOCFG snmpd.conf
#. ${config_file}
GET_CONFIGURATION snmpd $CFG_DEBUG
if [ -z "$SNMPD_COMMUNITY" ]
then
   GOOD_BYE
fi
if [ -z "$SNMPD_CONF" ]
then
   GOOD_BYE
fi
echo "# modified by alice $(date)" > /tmp/snmpd.conf.ALICE.$$
#
# replace the SNMP community configuration
#
awk 'BEGIN { flag = 0 }
     $1 == "community" && flag==0 { print (snmpd_line) 
                                    flag=1 }
     $1 != "community" { print ($0) }
    ' snmpd_line="$SNMPD_COMMUNITY" $std_snmpd_conf >> /tmp/snmpd.conf.ALICE.$$
#
# now replace the other stuff of the kind 
# blabla: foo
#
# workaround: remove later
export SNMPD_CONF
awk 'BEGIN { FS=OFS=":"
             snmpd_conf_line = ENVIRON["SNMPD_CONF"]
             wnum=split(snmpd_conf_line,tmp_conf,"[:\n]")
             for (i=1; i<wnum;i+=2) { if (tmp_conf[i]=="") { i++ }
                                      snmpd_conf_token[tmp_conf[i]]=tmp_conf[i+1]
                                    }
           }
    $1 in snmpd_conf_token     { print $1, snmpd_conf_token[$1] }
    !($1 in snmpd_conf_token)  { print $0 }
    ' /tmp/snmpd.conf.ALICE.$$ > ${std_snmpd_conf}.alice
#
CHANGED ${std_snmpd_conf} || {
    BACKUP ${std_snmpd_conf} 
    mv ${std_snmpd_conf}.alice ${std_snmpd_conf} 
    #
    # Now we invite snmpd to reload the information
    #
    if [ -x $rc_d_dir/snmpd ]
    then
      if [ "$($rc_d_dir/snmpd status)" = "Checking for service snmpd: OK" ]
      then
	 $rc_d_dir/snmpd stop
	 $rc_d_dir/snmpd start
      fi
    else
      FATAL NOEXE $rc_d_dir/snmpd    
    fi
}
#
############################################################################
# Thats all
GOOD_BYE 
