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

##  $Revision: 1.13 $
##  Rmgroup control-message handler

PROG=rmgroup
##  Some shells don't pass in $* unless we explicitly pass it in here.
. ${PATHBIN}/parsecontrol "$@"

EXISTS=`${AWK} "\\$1 == \"$1\"" ${ACTIVE}`
if test -z "$EXISTS"; then
     # Group doesn't exist
     exit
fi

##  Make sure the article is approved
${SM} ${ARTICLE} | ${SED} -e '/^$/q' >${TEMP}
grep '^Approved:' <${TEMP} >/dev/null 2>&1 || {
    ${SM} ${ARTICLE} | ${WRITELOG} ${LOGFILE} "Unapproved rmgroup by ${FROM}"
    rm -f ${TEMP}
    exit
}
rm -f ${TEMP}

case ${ACTION} in
mail)
    P1=$1
    export FROM P1 PATHBIN ARTICLE
    (
	echo "${FROM} requested that ${P1} be removed."
	echo 'To do this, type the following:'
	echo "	${PATHBIN}/ctlinnd rmgroup "'"'${P1}'"'
	echo ''
	echo 'The full article was:'
	${SM} ${ARTICLE} | sed -e 's/^~/~~/'
    ) | ${MAILCMD} -s "rmgroup $1 by ${FROM}" ${NEWSMASTER}
    exit
    ;;
esac

case ${ACTION} in
logit)
    ${SM} ${ARTICLE} | ${WRITELOG} ${LOGFILE} "rmgroup $1 by ${FROM} skipped"
    exit
    ;;
esac

##  Do the work.
ctlinnd -s rmgroup "$1"
${SM} ${ARTICLE} | ${WRITELOG} ${LOGFILE} "rmgroup $1 by ${FROM}"

##  We're sending to a file.
LOCK=${LOCKS}/LOCK.newsgroups
PID=$$
TRY=0
MAXTRY=60
export LOCK MAXTRY PID LOGFILE TRY
while [ ${TRY} -lt ${MAXTRY} ]; do
    shlock -p ${PID} -f ${LOCK} && break
    sleep 2
    TRY=`expr ${TRY} + 1`
done

##  If we got the lock, update the file; otherwise, give up.
test ${TRY} -lt ${MAXTRY} || {
    echo "Newgroup cannot update newsgroups.  Locked by" `cat ${LOCK}` 1>&2
    exit
}

${EGREP} -v "^$GROUPNAME[	]" <${NEWSGROUPS} >${TEMP}
cat ${TEMP} >${NEWSGROUPS}
rm ${TEMP} ${LOCK}
exit
