#!/bin/bash
# Created by Steve Hajek - Liquidware Labs - 7/11/2017

if [ $(id -u) != "0" ]; then
	echo "Error: You must be logged in as root ON THE HUB to run this script."
	exit 10
fi
echo ""
echo "***This script will change ALL of the Network Stations on this HUB to report to a new HUB."
echo ""
echo -n "What is the IP of the new HUB? "
read NEWHUB
echo -n "What is an admin user for the webUI? (Usually ssadmin) "
read -r ADMIN
echo -n "What is the password for the $ADMIN account? "
read -r PW
echo -n "What is the friend account password on the Network Stations? "
read -r FRIEND
echo -n "What is the root account password on the Network Stations? "
read -r ROOT
echo "----------------------------------------------"

# Get DB server and port for query...
PGHOST=`/opt/tnt/bin/tntdbconf PGHOST`
PGPORT=`/opt/tnt/bin/tntdbconf PGPORT`
# 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...

SERVMIN=0
for server in `cat .serverlist`; do
	echo "Updating $server to new HUB: $NEWHUB"
	./massupdate $server $NEWHUB $ADMIN $PW $FRIEND $ROOT
	echo "----------------------------------------------"
done
rm -f .serverlist
