home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / batch / batchsplitter < prev    next >
Text File  |  1994-09-14  |  2KB  |  80 lines

  1. #! /bin/sh
  2. # Prepare up to $4 batches of size $2-$3 in files named togo.[0-9] ,
  3. # working from $1.  Note that $4 can be 0, a special case.
  4. #
  5. # If the togo files do not contain file sizes, we make an arbitrary guess
  6. # at an average size.
  7.  
  8. # =()<. ${NEWSCONFIG-@<NEWSCONFIG>@}>()=
  9. . ${NEWSCONFIG-/etc/news/bin/config}
  10.  
  11. PATH=$NEWSCTL/bin:$NEWSBIN/batch:$NEWSBIN:$NEWSPATH ; export PATH
  12. umask $NEWSUMASK
  13.  
  14. input="$1"
  15. maxbatch="$2"
  16. absmax="$3"
  17. nwanted="$4"
  18.  
  19. rm -f togo.overflow togo.count
  20. awk 'BEGIN {
  21.     batchsize = 0
  22.     bno = 1
  23.     ndone = 0
  24.     batch = "togo." bno
  25.     nwanted = '$nwanted'
  26.     maxbatch = '$maxbatch'
  27.     absmax = '$absmax'
  28.     origmax = maxbatch
  29.     minbatch = 1
  30.     if (nwanted == 0) {        # a bit of a kludge
  31.         minbatch = 0
  32.         maxbatch = 0
  33.         ndone = -1
  34.     }
  35. }
  36. {
  37.     if ($NF ~ /^<.*>$/)            # ihave/sendme or NNTP
  38.         size = length($0) + 1
  39.     else if (NF == 1 || $NF !~ /^[0-9]+$/)    # no size present
  40.         size = 3000            # Arbitrary guess.
  41.     else
  42.         size = $NF + 15            # 15 for "#! rnews nnnnn"
  43.     if (size > absmax) {
  44.         print "article", $1, ">" absmax "bytes, not sent to \"'"$NEWSSITE"'\""
  45.         next            # NOTE NEXT
  46.     }
  47.  
  48.     if (batchsize + size > maxbatch && batchsize >= minbatch) {
  49.         # Go to next batch.
  50.         ndone++
  51.         if (ndone < nwanted) {
  52.             bno++
  53.             batch = "togo." bno
  54.         } else if (ndone == nwanted && FILENAME == "togo.more") {
  55.             batch = "togo.next"
  56.             nnext = 4 * nwanted    # how many batches in togo.next
  57.             if (nnext < 20)        # at least 20
  58.                 nnext = 20
  59.             maxbatch = origmax * nnext
  60.             minbatch = 1
  61.         } else {
  62.             print NR - 1 >"togo.count"
  63.             exit        # punt to sed to rebuild togo.more
  64.         }
  65.         batchsize = 0
  66.     }
  67.     batchsize += size
  68.     print >batch
  69. }' $input
  70.  
  71. # handle the overflow case efficiently
  72. if test -s togo.count
  73. then
  74.     sed "1,`cat togo.count`d" $input >togo.overflow
  75.     rm togo.count
  76.     mv togo.overflow $input
  77. else
  78.     rm $input
  79. fi
  80.