#!/bin/bash
#
# Prints the current newsgroups in PIMPPA
#
# -a : Active groups only
# -d : Disabled groups only
#
# no args: All groups
#
#

FLAG_DISABLED=1

############################

case $1 in
	'')
		mysql --execute="USE pimppa;SELECT DISTINCT g_name as 'Active Group(s)', area_name as 'Destination' FROM p_groups, p_areas WHERE NOT (g_flags & $FLAG_DISABLED) AND g_dest=area_id ORDER BY g_name; SELECT DISTINCT g_name as 'Disabled Group(s)', area_name as 'Destination' FROM p_groups, p_areas WHERE (g_flags & $FLAG_DISABLED) AND g_dest=area_id ORDER BY g_name;"
		;;
	-a)
		mysql --execute="USE pimppa;SELECT DISTINCT g_name as 'Active Group(s)', area_name as 'Destination' FROM p_groups, p_areas WHERE NOT (g_flags & $FLAG_DISABLED) AND g_dest=area_id ORDER BY g_name;"
		;;
	-d)
		mysql --execute="USE pimppa;SELECT DISTINCT g_name as 'Disabled Group(s)', area_name as 'Destination' FROM p_groups, p_areas WHERE (g_flags & $FLAG_DISABLED) AND g_dest=area_id ORDER BY g_name;"
		;;
	-h)
		echo Usage: $0 [-a] [-d]
		;;
esac

# Just a little test
WARNING=`mysql -N --execute="USE pimppa; SELECT count(*) FROM p_groups LEFT JOIN p_areas ON area_id=g_dest WHERE area_id IS NULL;"`
if [ $WARNING -gt 0 ]; then
	echo WARNING! The following newsgroups have non-existing target areas.
	mysql --execute="USE pimppa; SELECT g_name, g_dest as 'Destination id' FROM p_groups LEFT JOIN p_areas ON area_id=g_dest WHERE area_id IS NULL;"
fi
	
exit

