#!/bin/csh -f

# Copyright (c) 1991, Andrew Rosen.
# All rights reserved.
#
# This  software is supplied free of charge.  This software, or any part
# of it,  may  not  be  redistributed or otherwise made available to, or
# used  by,  any  other  person  without the inclusion of this copyright
# notice.  This software may not be used to make a profit in any way.
#
# This  software  is provided with absolutely no warranty, to the extent
# permitted  by  applicable  state law.  In no event, unless required by
# applicable law,  will the author(s) of this software be liable for any
# damages caused by this software.


source ~acs/acs-vars

set noclobber
renice +20 $$


## Do pings first
(glob ${pingdir}/*) >& /dev/null
if ( ${status} == 0 ) then
  # Check lock file
  (echo "$$" > ${acsdir}/LCK..ping) >& /dev/null

  if ( ${status} != 1 ) then
    # Process each ping message
    foreach ping ( ${pingdir}/* )
      echo "Doing ${ping}"
      set size = `ls -l ${ping} | awk '{ print $4 }'`
      echo -n "Processing ${ping}... ${size} bytes " >> ${acsdir}/stats/ping
      do-ping ${ping}

      # If file is still there, do-ping crashed
      if ( -f ${ping} ) then
        echo "Problem with ${ping}" | Mail -s "ACS ping problem" acs
        mv ${ping} ${acsdir}/junk/PING
      endif

      echo ""
      echo "done" >> ${acsdir}/stats/ping
    end

    # Remove lock file
    rm -f ${acsdir}/LCK..ping address
  else
    echo -n "Pings are locked by process "
    cat ${acsdir}/LCK..ping
  endif
endif


## Do replies next
(glob ${replydir}/*) >& /dev/null
if ( ${status} == 0 ) then
  # Check lock file
  (echo "$$" > ${acsdir}/LCK..reply) >& /dev/null

  if ( ${status} != 1 ) then
    # Process each reply message
    foreach reply ( ${replydir}/* )
      echo "Doing ${reply}"
      set size = `ls -l ${reply} | awk '{ print $4 }'`
      echo -n "Processing ${reply}... ${size} bytes " >> ${acsdir}/stats/reply
      do-reply ${reply}

      # If file is still there, do-reply crashed
      if ( -f ${reply} ) then
        echo "Problem with ${reply}" | Mail -s "ACS reply problem" acs
        mv ${reply} ${acsdir}/junk/REPLY
      endif

      echo ""
      echo "done" >> ${acsdir}/stats/reply
    end

    # Remove lock file
    rm -f ${acsdir}/LCK..reply address
  else
    echo -n "Replies locked by process "
    cat ${acsdir}/LCK..reply
  endif
endif


## Do posts last
(glob ${postdir}/*) >& /dev/null
if ( ${status} == 0 ) then
  # Check lock file
  (echo "$$" > ${acsdir}/LCK..post) >& /dev/null

  if ( ${status} != 1 ) then
    # Process each post message
    foreach post ( ${postdir}/* )
      echo "Doing ${post}"
      set size = `ls -l ${post} | awk '{ print $4 }'`
      echo -n "Processing ${post}... ${size} bytes " >> ${acsdir}/stats/post
      do-post ${post}

      if ( -f ${post} ) then
        echo "Problem with ${post}" | Mail -s "ACS post problem" acs
        mv ${post} ${acsdir}/junk/POST
      endif

      echo ""
      echo "done" >> ${acsdir}/stats/post
    end

    # Remove lock file
    rm -f ${acsdir}/LCK..post address
  else
    echo -n "Posts locked by process "
    cat ${acsdir}/LCK..post
  endif
endif
