#! /bin/sh
if [ -d /usr/local/bcnu ]
then
	BCNUHOME="/usr/local/bcnu"
else

	echo "Enter directory name of bcnu home:[/usr/local/bcnu] \c"
	read home
	if [ -z "$home" ]
		then
			BCNUHOME="/usr/local/bcnu"
		else
			BCNUHOME=$home
	fi
fi
cd $BCNUHOME

. $BCNUHOME/etc/bcnuenv

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

echo "Please wait.."
LOAD=$1
BCNULOADED=${LOAD:=`$BCNUHOME/scripts/get_hosts managed | grep -v $BCNUHOST`}

BCNU="$BCNUHOME/bin/bcnu -s $BCNUPORT"

echo "Clients: $BCNULOADED"

for client in $BCNULOADED
do

echo "Upgrading $client"

if $BCNU -a get -f /etc/agents $client >$BCNUHOME/clients/agents.$client 
		then
			echo "OK - got agents"
	else
			echo "Failed to get agents"
			exit
	fi
if $BCNU -a send -f $BCNUHOME/etc/upgrade_agents -n /etc/agents $client 
		then
			echo "OK - sent updated agents"
	else
			echo "Failed to update agents"
			exit
	fi
if $BCNU -a send -f $BCNUHOME/agent/upgrade -n /agent/upgrade $client 
		then
			echo "OK - send upgrade"
	else
			echo "Failed to update agents"
			exit
	fi
if $BCNU -a reload $client 
		then
			echo "OK - Client reloaded"
	else
			echo "Failed to reload client"
	fi

echo "Client system bcnud will be shut down and upgraded - this may take"
echo "a few minutes - please be patient..."
sleep 60
done
