#!/bin/bash
#
# Toggles newsgroup enable/disable status for given 
# newsgroup name pattern.
#
# Original idea & script by Newsboy, tweaked by Igor
#

P_G_NAME="$1"
FLAG_DISABLED=1

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

if [ ! $1 ] ; then
        echo "Usage: $0 <newsgroup name sql pattern>"
        exit
fi

MATCHES=`mysql -N --execute="USE pimppa; SELECT count(*) FROM p_groups WHERE g_name like '$P_G_NAME';"`
if [ $MATCHES -gt 0 ]; then 
	# The OR-AND-NOT construction below tries to implement a XOR toggle
	mysql --execute="USE pimppa; UPDATE p_groups SET g_flags=((g_flags | $FLAG_DISABLED) & ~(g_flags & $FLAG_DISABLED)) WHERE g_name LIKE '$P_G_NAME';"
   
	# Print the toggled status
	mysql --execute="USE pimppa; SELECT g_name, IF((g_flags & $FLAG_DISABLED),'Disabled','Enabled') AS 'Status' FROM p_groups WHERE g_name LIKE '$P_G_NAME';"

else
	echo No groups match the given pattern $P_G_NAME
	exit
fi

