#!/bin/csh -f

unlimit

setenv KRBTKFILE /tmp/tkt_lopip_nightly
set PPDIR=/afs/sipb.mit.edu/project/periodic-postings
set POSTER="${PPDIR}/post_faq/install/bin/post_faq.pl -quiet 1 -sigfile none"
set D=${PPDIR}/data/
set MAKEDEBUG=
set DEBUG=
set path = (/rtfm/bin /bin/athena /usr/athena/bin \
	    /etc /usr/etc /bin /usr/bin /usr/ucb)

# setenv because used by mail_policy.pl

setenv NADIR $PPDIR/news.answers

# setenv because used by make-header.pl

setenv PPNEWS $PPDIR/data/periodic-posting-news
# we need this to because you can't use ':h' with environment
# variables in csh (a bug which is fixed in tcsh)
set ppnews = $PPNEWS

set today = `date`

if ("$today" =~ Sun*) then
	mail news-answers-request@MIT.Edu <<EOF
To: news-answers-request@MIT.Edu
Subject: Automatic weekly reminder: FAQ archive needs to be cleaned up

This message is an automatic weekly reminder that the periodic FAQ
archive cleanup on rtfm.mit.edu needs to be performed.  This message
should remain in the *.answers queue until the cleanup is performed,
and the moderator who does the cleanup should lock this message while
doing it.  Don't forget that packing the *.answers, faq-maintainers
and faq-maintainers-announce mail archives is also part of the regular
cleanup.
EOF
endif

set pname = discuss
set keytab = /usr/spool/discuss/keytab
set hostprinc = discuss/`hostname`

/usr/athena/bin/kinit -k -t ${keytab} $hostprinc
if ($status) then
   echo "${0}: Error extracting tickets from srvtab."
   exit 1
endif

aklog sipb.mit.edu >& /dev/null
if ($status) then
    echo "${0}: Error getting tokens for cell sipb.mit.edu."
    kdestroy >& /dev/null
    exit 1
endif

$NADIR/mail_policy.pl

cd $PPDIR/src
/usr/ccs/bin/make $MAKEDEBUG all install > /dev/null || goto done
set DIFFTIME=`awk '/^periodic-postings\/diff/ {print $3}' ${D}faq.config` \
	|| goto done
rcs -l -q $ppnews || goto done
setenv PP_COMPAT 1
$POSTER $DEBUG -config ${D}faq.config || goto done
set NEWDIFFTIME=`awk '/^periodic-postings\/diff/ {print $3}' ${D}faq.config` \
	|| goto done
if ("$DIFFTIME" != "$NEWDIFFTIME") then
	chmod +w $ppnews || goto done
	cp /dev/null $ppnews || goto done
	(cd ${ppnews:h}; \
         ci -u -q -m"Automatic clearing of posted news" $PPNEWS) || goto done
	make diff-base > /dev/null || goto done
endif

done:

unlog
kdestroy >&/dev/null || cp /dev/null ${KRBTKFILE}
