#! /bin/sh
# 
# Copyright (c) 2000 Carnegie Mellon University.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer. 
#
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
#
# 3. The name "Carnegie Mellon University" must not be used to
#    endorse or promote products derived from this software without
#    prior written permission. For permission or any other legal
#    details, please contact  
#      Office of Technology Transfer
#      Carnegie Mellon University
#      5000 Forbes Avenue
#      Pittsburgh, PA  15213-3890
#      (412) 268-4387, fax: (412) 268-7395
#      tech-transfer@andrew.cmu.edu
#
# 4. Redistributions of any form whatsoever must retain the following
#    acknowledgment:
#    "This product includes software developed by Computing Services
#     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
#
# CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
# THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
# FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
# AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
# OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# $Id: feedcyrus.in,v 1.9 2000/05/23 20:52:15 robeson Exp $
# Generated automatically from feedcyrus.in by configure.
##
##  Send news to Cyrus IMAPD
##  =()<. @<_PATH_SHELLVARS>@>()=
. @INNSHELLVARS@

CYRUS_DIR=/usr/local/cyrus/bin

PROGNAME=`basename $0`
LOCK=${LOCKS}/LOCK.${PROGNAME}
LOG=${MOST_LOGS}/${PROGNAME}.log

##  Go to where the action is.
test ! -d ${BATCH} && {
    echo "${PROGNAME}: directory ${BATCH} not found" 1>&2
    exit 1
}
cd ${BATCH}

##  Set up log file.
umask 002
test -z "${D_FLAG}" && {
    test ! -f ${LOG} && touch ${LOG}
    chmod 0660 ${LOG}
    exec >>${LOG} 2>&1
}
PARENTPID=$$
echo "${PROGNAME}: [${PARENTPID}] start"

##  Set up environment.
export BATCH PROGNAME PARENTPID CYRUS_DIR

##  Loop over all sites.
for PROG in collectnews; do

    ##  Start sending this site in the background.
    export PROG LOCKS BATCHFILE PROGNAME PARENTPID TMPDIR
    sh -c '
	BATCHFILE=${PROG}.input
	LOCK=${LOCKS}/LOCK.${PROG}
	trap "rm -f ${LOCK} ; exit 1" 1 2 3 15
	shlock -p $$ -f ${LOCK} || {
	    WHY="`cat ${LOCK}`"
	    echo "${PROGNAME}: [${PARENTPID}:$$] ${PROG} locked ${WHY} `date`"
	    exit
	}
	if [ -f ${PROG}.work ] ; then
	    cat ${PROG}.work >>${BATCHFILE}
	    rm -f ${PROG}.work
	fi
	mv ${PROG} ${PROG}.work
	if ctlinnd -s -t30 flush ${PROG}"!" ; then
	    if [ -f ${BATCHFILE} ] ; then
		cat ${PROG}.work >>${BATCHFILE}
		rm -f ${PROG}.work
	    else
		mv ${PROG}.work ${BATCHFILE}
	    fi
	    if [ -s ${BATCHFILE} ] ; then
		echo "${PROGNAME}: [${PARENTPID}:$$] begin ${PROG} `date`"
		${CYRUS_DIR}/${PROG} < ${BATCH}/${BATCHFILE} && rm -f ${BATCHFILE} 
		echo "${PROGNAME}: [${PARENTPID}:$$] end ${PROG} `date`"
	    else
		rm -f ${BATCHFILE}
	    fi
	fi
	rm -f ${LOCK}
    ' &
    sleep 5
done

wait
echo "${PROGNAME}: [${PARENTPID}] stop"
