#!/bin/bash
# Created by Steve Hajek - Liquidware Labs - 11/4/2015

if [ $(id -u) != "0" ]; then
	echo "Error: You must be logged in as root to run this script."
	exit 10
fi

echo "This script will check for and install updates on all "
echo "Network Stations connected to this HUB.  If you are multiple "
echo "releases behind, you may need to repeat this script to get current."
echo ""
echo "Script needs all Net Stations to have the default sspassword for all accounts."
echo ""
echo -n "Would you like to proceed? (Y/N) "
read choice
if [ "$choice" == "y" ] || [ "$choice" == "Y" ]; then
	# Get DB server and port for query...
	PGHOST=`grep PGHOST /etc/tntdb.conf | awk -F= '{ print $2 }'`
	PGPORT=`grep PGPORT /etc/tntdb.conf | awk -F= '{ print $2 }'`
	# Grab list of network station IPs from DB and create server list...
	echo "select interface.ip as stationip from tnt_config.gatekeeperbase station join tnt_config.policynode pn on (pn.id = station.nodeid) join tnt_config.interface interface on (interface.nodeid = station.nodeid) where pn.delete_time is null and pn.id != (select min(id) from tnt_config.policynode where type = 3 and delete_time is null) and interface.delete_time is null;" | PGPASSWORD=reports psql -h $PGHOST -p $PGPORT -t portal reports | awk -F\  '{ print $1 }' > .serverlist
	# End server list - begin updates...

	for server in `cat .serverlist`; do
		./massupdate $server
	done
	rm -f .serverlist
fi
