#!/bin/bash
# Created by Steve Hajek - Liquidware Labs - 10/16/2015

if [ $(id -u) != "0" ]; then
	echo "Error: You must be logged in as root to run this script."
	exit 10
fi
if [ -z "$1" ]; then
	echo "Syntax: $0 firstNTPserver [secondNTPserver]"
	exit 10
fi
echo "This script will NTP server settings across and expects "
echo "all Network Stations and DB appliances connected to this HUB."
echo ""
echo "This script needs all appliances using the default sspassword for all accounts."
echo ""

# Build server list starting with DB if separate appliance...
PGHOST=`grep PGHOST /etc/tntdb.conf | awk -F= '{ print $2 }'`
PGPORT=`grep PGPORT /etc/tntdb.conf | awk -F= '{ print $2 }'`
if [ "$PGHOST" != "127.0.0.1" ]; then
	echo $PGHOST > .serverlist
fi
# Grab list of HUB and network station IPs from DB and add to 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 interface.delete_time is null;" | PGPASSWORD=reports psql -h $PGHOST -p $PGPORT -t portal reports | awk -F\  '{ print $1 }' >> .serverlist
# End server list - begin work...

for server in `cat .serverlist`; do
	./massntp $server $1 $2
done
rm -f .serverlist
