home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / CNEWS / _CNEWS.TAR / usr / lib / newsbin / input / newsrun < prev    next >
Encoding:
Text File  |  1994-09-02  |  3.5 KB  |  166 lines

  1. #! /bin/sh
  2. # Process spooled news.
  3.  
  4. # =()<. ${NEWSCONFIG-@<NEWSCONFIG>@}>()=
  5. . ${NEWSCONFIG-/var/lib/news/bin/config}
  6.  
  7. PATH=$NEWSCTL/bin:$NEWSBIN/input:$NEWSBIN/relay:$NEWSBIN:$NEWSPATH ; export PATH
  8. umask $NEWSUMASK
  9.  
  10. # Should we be running on this machine at all?
  11. if test -r $NEWSCTL/server
  12. then
  13.     me="`hostname`"
  14.     server="`cat $NEWSCTL/server`"
  15.     if test " $server" != " `hostname`"
  16.     then
  17.         exec rsh $server exec /bin/sh -c "PATH=$PATH exec newsrun $*"
  18.         exit 2        # should never be reached
  19.     fi
  20. fi
  21.  
  22. # Options.
  23. hold=
  24. case "$1" in
  25. -h)    hold=y ; shift    ;;
  26. esac
  27.  
  28. # Go to our base of operations.
  29. cd $NEWSARTS/in.coming
  30.  
  31. # Consider what grades we should process, and check for work.
  32. sawstop=n
  33. stoppat='0\.'
  34. if test -r stop
  35. then
  36.     pat="$stoppat"
  37.     sawstop=y
  38. else
  39.     pat='[0-9]'
  40. fi
  41. case "$1" in
  42. '')            ;;
  43. *)    pat="[$1]\."    ;;
  44. esac
  45. if test " `ls | egrep \"^$pat\" | wc -l`" -eq 0
  46. then
  47.     exit 0    
  48. fi
  49.  
  50. # Lock against others running.
  51. lock="$NEWSCTL/LOCKinput"
  52. ltemp="$NEWSCTL/L.$$"
  53. echo $$ >$ltemp
  54. trap "rm -f $ltemp ; exit 0" 0 1 2 15
  55. if newslock $ltemp $lock
  56. then
  57.     trap "rm -f $ltemp $lock ; exit 0" 0 1 2 15
  58. else
  59.     exit 0
  60. fi
  61.  
  62. # Misc. setup.
  63. stale="`staleness`"
  64.  
  65. # Master loop.
  66. while :                # "while true", but : is faster
  67. do
  68.     # Find some work.  "sort -n" gets grades ordered right.
  69.     them="`ls | egrep \"^$pat\" | sort -n | sed 50q`"
  70.     if test " $them" = " "        # if no work...
  71.     then
  72.         break            # NOTE BREAK OUT
  73.     fi
  74.  
  75.     # Check space.  It is *probably* better to stop processing
  76.     # when things get too full.  (This test is actually a bit
  77.     # inaccurate since the batches may be compressed, but it's
  78.     # good enough to catch major space problems.)
  79.     allsize=`sizeof $them`
  80.     # decompressed + articles = maybe 5*compressed
  81.     if test " `spacefor $allsize articles`" -gt 4    # lots of room
  82.     then
  83.         muchroom=y
  84.     else
  85.         muchroom=
  86.     fi
  87.  
  88.     # Decompression etc.
  89.     list=
  90.     for f in $them
  91.     do
  92.         # If the stop file has come into existence, punch out to
  93.         # the outer loop to process what we've got and then rethink.
  94.         if test " $sawstop" = " n" -a -r stop
  95.         then
  96.             sawstop=y
  97.             pat="$stoppat"
  98.             break        # NOTE BREAK OUT
  99.         fi
  100.  
  101.         # Space check, if we're close.
  102.         if test " $muchroom" != " y"
  103.         then
  104.             batchsize=`sizeof $f`
  105.             # again, decompressed + articles = maybe 5*compressed
  106.             if test " `spacefor $batchsize articles`" -le 4
  107.             then
  108.                 # observe that list is always empty here
  109.                 exit 0        # just no room
  110.             fi
  111.         fi
  112.  
  113.         # Save a copy in hold if requested.
  114.         if test " $hold" = " y" -a -d hold
  115.         then
  116.             ln $f hold/$f
  117.         fi
  118.  
  119.         # Decompress if necessary.
  120.         text=${f}.t
  121.         case $f in
  122.         *.Z)    uncompress <$f >$text    ;;
  123.         *.7)    c7decode <$f | uncompress >$text    ;;
  124.         *.t)    text=$f ; : okay    ;;
  125.         *)    uncompress <$f >$text 2>/dev/null ||
  126.                 { rm -f $text ; text=$f ; : okay ; }    ;;
  127.         esac || ln $f bad/$f
  128.         if test " $f" != " $text"
  129.         then
  130.             rm -f $f
  131.         fi
  132.  
  133.         # Empty batches need no processing.
  134.         if test -s $text
  135.         then
  136.             list="$list $text"
  137.         else
  138.             rm -f $text
  139.         fi
  140.  
  141.         # If we are tight on space, run things one at a time.
  142.         if test " $muchroom" != " y" -a " $list" != " "
  143.         then
  144.             break        # NOTE BREAK OUT
  145.         fi
  146.     done
  147.  
  148.     # Do it.  -r redirects stdout and stderr into logs.  -n makes
  149.     # history entries for refused articles; this is right for
  150.     # NNTP-feed sites and doesn't hurt uucp-feed sites unless
  151.     # they refuse a good fraction of what they get.  -u tells relaynews
  152.     # to unlink the files that work.
  153.     if test " $list" != " "
  154.     then
  155.         relaynews -r -n $stale -u $list
  156.         doexplode
  157.         # Deal with the leftovers, if any; mv 2> is simple and quick.
  158.         mv $list bad 2>/dev/null
  159.         # And just in case that didn't work...
  160.         rm -f $list
  161.     fi
  162.     sleep 45        # give somebody else a shot at the lock
  163. done
  164.  
  165. exit 0
  166.