#!/bin/sh
BCNUNOLOG="yes"
. /usr/local/bcnu/etc/bcnuenv
. $BCNUHOME/agent/agent_head

#set -x

bcnu_default()
{
TODAY=`date "+%a"`

sed -ne "\+^\[alias]+,\+^\[+p" $BCNUHOME/etc/hostinfo | sed -ne "/^[^\[#]/p" >$BCNUTMP/alert.alias
. $BCNUTMP/alert.alias
sed -ne "\+^\[default]+,\+^\[+p" $BCNUHOME/etc/hostinfo | sed -ne "/^[^\[#]/p" >$BCNUTMP/alert.default
. $BCNUTMP/alert.default
}

bcnu_contact()
{
unset INFO AGENTPAGER HOSTPAGER AGENTEMAIL HOSTEMAIL

if grep "\[host=$ahost\]" $BCNUHOME/etc/hostinfo >/dev/null
then	
	sed -ne "\+^\[host=$ahost]+,\+^\[+p" $BCNUHOME/etc/hostinfo | sed -ne "/^[^\[#]/p" >$BCNUTMP/alert.host
	. $BCNUTMP/alert.host
fi
EMAIL="$DEFEMAIL $HOSTEMAIL $AGENTEMAIL"
PAGER=${AGENTPAGER:-${HOSTPAGER:-$DEFPAGER}}
}

echo "# hostinfo initialized at `date '+%a %b %e %T %Z %Y'`" >$BCNUHOME/data/hostinfo

bcnu_default
echo "\$defemail=\"$DEFEMAIL\";" | sed "s/\@/\\\@/g" >>$BCNUHOME/data/hostinfo
echo "\$defpager=\"$DEFPAGER\";" >>$BCNUHOME/data/hostinfo
echo "\$defwin32=\"$DEFWIN32\";" >>$BCNUHOME/data/hostinfo

cd $BCNUHOME/data/hosts
for ahost in `ls`
do
	
#	ahost=`expr $hostline : '.*\[host=\(.*\)\]'`
	bcnu_contact
	if [ !  -z "$HOSTEMAIL" ] 
	then
		echo "\$hostemail{\"$ahost\"}=\"$EMAIL\";" | sed "s/\@/\\\@/g" >>$BCNUHOME/data/hostinfo
        fi
	if [ !  -z "$HOSTPAGER" ] 
	then
		echo "\$hostpager{\"$ahost\"}=\"$HOSTPAGER\";" >>$BCNUHOME/data/hostinfo
        fi
	if [ !  -z "$INFO" ] 
	then
		echo "\$hostinfo{\"$ahost\"}=\"$INFO\";" >>$BCNUHOME/data/hostinfo
	fi
done

cd $BCNUHOME/data/agents
for aagent in `ls`
do
	
	bcnu_contact
        if [ !  -z "$AGENTEMAIL" ] 
	then
		echo "\$agentemail{\"$aagent\"}=\"$EMAIL\";" | sed "s/\@/\\\@/g" >>$BCNUHOME/data/hostinfo
        fi
	if [ !  -z "$AGENTPAGER" ] 
	then
		echo "\$agentpager{\"$aagent\"}=\"$AGENTPAGER\";" >>$BCNUHOME/data/hostinfo
	fi
#	echo "\$agentinfo{\"$aagent\"}=\"$INFO\";" >>$BCNUHOME/data/hostinfo
done

echo "1;" >>$BCNUHOME/data/hostinfo


# now build the files for admin info

if [ ! -d $BCNUHOME/clients ]
        then
                mkdir $BCNUHOME/clients
fi

$BCNUHOME/scripts/get_hosts managed >$BCNUHOME/clients/managed.hosts
$BCNUHOME/scripts/get_hosts proxy >$BCNUHOME/clients/proxy.hosts
$BCNUHOME/scripts/get_hosts all >$BCNUHOME/clients/all.hosts


