#! /bin/sh
##  $Revision: 1.1 $
##  Script to execute checkgroups text; results to stdout.

# =()<ACTIVE=@<_PATH_ACTIVE>@>()=
ACTIVE=/usr/local/news/active
# =()<EGREP=@<_PATH_EGREP>@>()=
EGREP=/usr/local/bin/egnugrep
# =()<NEWSGROUPS=@<_PATH_NEWSGROUPS>@>()=
NEWSGROUPS=/usr/local/news/newsgroups
LOCALGROUPS=${NEWSLIB}/localgroups
# =()<TMPDIR=${TMPDIR-@<_PATH_TMP>@}>()=
TMPDIR=${TMPDIR-/tmp}

T=${TMPDIR}

if [ -s ${NEWSGROUPS} ] ; then
    cp ${NEWSGROUPS} ${NEWSGROUPS}.old
else
    cat /dev/null >${NEWSGROUPS}
fi

cat /dev/null >${T}/$$out

##  Copy the article without headers, append local newsgroups.
cat >${T}/$$msg
test -f ${LOCALGROUPS} && cat ${LOCALGROUPS} >>${T}/$$msg

##  Get the top-level newsgroup names from the message and turn it into
##  an egrep pattern.
cat /dev/null >${T}/$$b
PATS=`sed <${T}/$$msg \
	-e 's/[ 	].*//' -e 's/\..*//' \
	-e 's/^!//' -e '/^$/d' \
	-e 's/^/^/' -e 's/$/[. 	]/' \
    | sort -u \
    | (tr '\012' '|' ; echo '' )\
    | sed -e 's/|$//'`
${EGREP} -v "${PATS}" ${NEWSGROUPS} >${T}/$$a
cat ${T}/$$a >${NEWSGROUPS}
sed -e "/^!/d" ${T}/$$msg >>${NEWSGROUPS}
rm -f ${T}/$$b ${T}/$$msg

${EGREP} "${PATS}" ${ACTIVE} | sed 's/ .*//' | sort >${T}/$$active
${EGREP} "${PATS}" ${NEWSGROUPS} | sed 's/[ 	].*//' | sort >${T}/$$newsgrps

comm -13 ${T}/$$active ${T}/$$newsgrps >${T}/$$missing
comm -23 ${T}/$$active ${T}/$$newsgrps >${T}/$$remove

${EGREP} "${PATS}" ${ACTIVE} | sed -n "/m\$/s/ .*//p" | sort >${T}/$$amod.all
${EGREP} "${PATS}" ${NEWSGROUPS} |
sed -n "/Moderated/s/[ 	][ 	]*.*//p" | sort >${T}/$$ng.mod

comm -12 ${T}/$$missing ${T}/$$ng.mod >${T}/$$add.mod
comm -23 ${T}/$$missing ${T}/$$ng.mod >${T}/$$add.unmod
cat ${T}/$$add.mod ${T}/$$add.unmod >>${T}/$$add

comm -23 ${T}/$$amod.all ${T}/$$remove >${T}/$$amod
comm -13 ${T}/$$ng.mod ${T}/$$amod >${T}/$$ismod
comm -23 ${T}/$$ng.mod ${T}/$$amod >${T}/$$nm.all
comm -23 ${T}/$$nm.all ${T}/$$add >${T}/$$notmod

if [ -s ${T}/$$remove ] ; then
    (
	echo "# The following newsgroups are non-standard."
	sed "s/^/#	/" ${T}/$$remove
	echo "# You can remove them by executing the commands:"
	for i in `cat ${T}/$$remove` ; do
	    echo "	ctlinnd rmgroup $i"
	done
	echo ''
    ) >>${T}/$$out
fi

if [ -s ${T}/$$add ] ; then
    (
	echo "# The following newsgroups were missing and should be added."
	sed "s/^/#	/" ${T}/$$add
	echo "# You can do this by executing the command(s):"
	for i in `cat ${T}/$$add.unmod` ; do
	    echo "	ctlinnd newgroup $i y ''"
	done
	for i in `cat ${T}/$$add.mod` ; do
	    echo "	ctlinnd newgroup $i m ''"
	done
	echo ''
    ) >>${T}/$$out
fi

if [ -s ${T}/$$ismod ] ; then
    (
	echo "# The following groups are incorrectly marked as moderated:"
	sed "s/^/#	/" ${T}/$$ismod
	echo "# You can correct this by executing the following:"
	for i in `cat ${T}/$$ismod` ; do
	    echo "	ctlinnd changegroup $i y"
	done
	echo ''
    ) >>${T}/$$out
fi

if [ -s ${T}/$$notmod ] ; then
    (
	echo "# The following groups are incorrectly marked as unmoderated:"
	sed "s/^/#	/" ${T}/$$notmod
	echo "# You can correct this by executing the following:"
	for i in `cat ${T}/$$notmod` ;do
	    echo "	ctlinnd changegroup $i m"
	done
	echo ''
    ) >>${T}/$$out
fi

test -s ${T}/$$out && cat ${T}/$$out

rm -f ${T}/$$*
