#!/bin/bash
# Created by Steve Hajek - Liquidware Labs - 11/5/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 wipe all pending audit data files from all "
echo "Network Stations connected to this HUB. "
echo ""
echo "This script needs all Network 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 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
		./massclear $server
	done
	rm -f .serverlist
fi
