#! /bin/sh
# fixscript will replace this line with code to load innshellvars

##  $Revision: 1.1 $
##  Script to execute checkgroups text; results to stdout.

T=${TMPDIR}

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.
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} "${PATS}" ${ACTIVE} | ${EGREP} "${1:-.}" | ${SED} 's/ .*//' | sort >${T}/$$active
${EGREP} "${PATS}" ${T}/$$msg | ${EGREP} "${1:-.}" | ${SED} 's/[ 	].*//' | sort >${T}/$$newsgrps

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

${EGREP} "${PATS}" ${ACTIVE} | ${EGREP} "${1:-.}" | ${SED} -n '/ m$/s/ .*//p' | sort >${T}/$$amod.all
${EGREP} "${PATS}" ${T}/$$msg | ${EGREP} "${1:-.}" |
${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 "	${PATHBIN}/ctlinnd rmgroup $i"
	    ${EGREP} "^$i	" ${NEWSGROUPS} >>${T}/$$ngdel
	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 "	${PATHBIN}/ctlinnd newgroup $i y ${FROM}"
	    ${EGREP} "^$i	" ${T}/$$msg >>${T}/$$ngadd
	done
	for i in `cat ${T}/$$add.mod` ; do
	    echo "	${PATHBIN}/ctlinnd newgroup $i m ${FROM}"
	    ${EGREP} "^$i	" ${T}/$$msg >>${T}/$$ngadd
	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 "	${PATHBIN}/ctlinnd changegroup $i y"
	    ${EGREP} "^$i	" ${T}/$$msg >>${T}/$$ngchng
	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 "	${PATHBIN}/ctlinnd changegroup $i m"
	    ${EGREP} "^$i	" ${T}/$$msg >>${T}/$$ngchng
	done
	echo ''
    ) >>${T}/$$out
fi

test -s ${T}/$$out && {
    cat ${T}/$$out
    echo 'exit # so you can feed this message into the shell'
    echo "# And remember to update ${NEWSGROUPS}."
    test -s ${T}/$$ngdel && {
	echo "# Remove these lines:"
	${SED} "s/^/#	/" ${T}/$$ngdel
	echo ''
    }
    test -s ${T}/$$ngadd && {
	echo "# Add these lines:"
	${SED} "s/^/#   /" ${T}/$$ngadd
	echo ''
    }
    test -s ${T}/$$ngchng && {
	echo "# Change these lines:"
	${SED} "s/^/#   /" ${T}/$$ngchng
	echo ''
    }
}

rm -f ${T}/$$*
