#!/bin/sh

#This script copies specific PBT configs from the default snmpd.conf to the running copy.
#These are configs that should NOT be changed by the user.
#Forces certain PBT specific configs to be consistent and accurate for the target controller.
#This script should be run by start.sh to override any conflicting customer settings.

SNMPCONF=/home/admin/conf/snmpd.conf
SNMPCONFDEF=/home/admin/conf/default/snmpd.conf

ReplaceConfig () {
	#$1 = input config
	#$2 = output config
	#$3 = config to replace
	
	#Match starting at beginning of line, take only first match
	CONF=$(grep "^$3" $1 | head -1)

	#Make sure the config we found at least contains the desired name.
	# Prevents empty/missing configs from overwriting the running copy
	echo "$CONF" | grep -q "$3"
	RES=$(echo $?)

	if [[ $RES == "0" ]]
	then 
		echo "Replacing $3 in $2 with '$CONF' from $1"
		#Replace the whole line starting with the config (ignores commented configs)
		# If the line doesn't exist in the output file, append it to the end of the file
		grep -q "^$3" $2 && sed -i "/^$3/c $CONF" $2 || sed -i "$ a\ $CONF" $2 
	fi
} 

ReplaceConfig $SNMPCONFDEF $SNMPCONF syscontact
ReplaceConfig $SNMPCONFDEF $SNMPCONF sysObjectID
ReplaceConfig $SNMPCONFDEF $SNMPCONF sysDescr
ReplaceConfig $SNMPCONFDEF $SNMPCONF sysName

exit 0
