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

##  $Revision: 1.11 $
##  Newgroup control-message handler

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

##  Valid parameters and group name?
case "$#" in
1|2)
    ;;
*)
    ${SM} ${ARTICLE} | ${WRITELOG} ${LOGFILE} "Unparseable newgroup by ${FROM}"
    exit
    ;;
esac
case "$1" in
*[\ \	/:]*)
    ${SM} ${ARTICLE} | ${WRITELOG} ${LOGFILE} "Unsafe newgroup by ${FROM}"
    exit
    ;;
esac


#### Simple check for a bork-style group. Uncomment to stop newgroups 
#### with names deeper than 7 levels being created.

#ELEMS=`echo "$1" | awk -F. '{print NF}'`
#if [ $ELEMS -gt 7 ]; then
#    ${SM} ${ARTICLE} | ${WRITELOG} ${LOGFILE} "Too many elements in newsgroup name by ${FROM}" 
#    exit
#fi

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

case "$2" in
[Mm]*)
    set $1 m
    ;;
*)
    set $1 y
    ;;
esac

##  Kill the chefs; from <wesommer@athena.mit.edu>.
##  That is, turn "alt.foo.bar.bar.bar" into an "x" group.
#case `echo $1 | ${AWK} -F. '{
#	if (NF > 1 && $NF != "misc") {
#	    if (NF > 5 || $NF == $(NF - 1) || $(NF - 1) == $(NF - 2)) {
#		print "bozo";
#	    }
#	}
#    }'` in
#bozo*)
#      set $1 x
#      ;;
#esac

##  Compare group against active file.
SUFFIX=""
LINE=`grep "^$1 " <${ACTIVE} 2>/dev/null`
case "${LINE}" in
"")
    ##  New group.
    WORD=created
    ;;
"$1 "*" $2")
    ##  Exact match; ignore it.
    exit
    ;;
"$1 "*" ="*|"$1 "*" x"|"$1 "*" j")
    ${SM} ${ARTICLE} | ${WRITELOG} ${LOGFILE} "Rejected special newsgroup ($1) by ${FROM}"
    exit
    ;;
*)
    case "$2" in
    m)
	WORD="made moderated"
	SUFFIX=" (Moderated)"
	;;
    y)
	WORD="made unmoderated"
	;;
    esac
    ;;
esac

case ${ACTION} in
mail)
    P1=$1
    P2=$2
    export FROM WORD PATHBIN P1 P2 ARTICLE
    (
	echo "${FROM} requested that $1 be ${WORD}."
	echo 'To do this, type the following:'
	echo "	${PATHBIN}/ctlinnd newgroup ${P1} ${P2} ${FROM}"
	echo ''
	echo 'The original article was:'
	${SM} ${ARTICLE}
    ) | ${WRITELOG} ${LOGFILE} "newgroup $1 $2 ${FROM}"
    exit
    ;;
logit)
    ${SM} ${ARTICLE} | ${WRITELOG} ${LOGFILE} "skipping newgroup $1 $2 ${FROM} (would be ${WORD})" 
    exit
    ;;
esac

##  Do the work.
ctlinnd -s newgroup $1 $2 "${FROM}"
${SM} ${ARTICLE} | ${WRITELOG} ${LOGFILE} "newgroup $1 $2 ${WORD} ${FROM}"

##  Get a description of the newsgroup; first from the article, and
##  then from the newsgroups file.
DESC=`${SM} ${ARTICLE} | ${SED} -n -e "
:top
/^For your newsgroups file:\$/b scan
n
b top
:scan
n
/$1[ 	]/ {
p
q
}
b scan"`
test -z "${DESC}" && {
    DESC=`${EGREP} "^$1	" ${NEWSGROUPS} | ${SED} "s/[ 	]*(Moderated)//"`
    test -z "${DESC}" && DESC="$1	?"
}
DESC="${DESC}${SUFFIX}"

##  Lock so we can update newsgroups
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 "^$1[	]" <${NEWSGROUPS} >${TEMP}
cat ${TEMP} >${NEWSGROUPS}
echo "${DESC}" >>${NEWSGROUPS}
rm ${TEMP} ${LOCK}
exit
