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

  1. #! /bin/sh
  2. # Prepare up to $2 batches of size $1 in files named togo.[0-9] .  We limit
  3. # $2 to 7 to stay within awk's limits on file descriptors (we need a
  4. # couple of other descriptors).  We ultimately work from togo, but if it's
  5. # the only thing we've got to work on, we immediately shuffle it aside into
  6. # togo.more so that we can unlock the news system.  If we've got an existing
  7. # non-empty togo.more, we use that.  As a further optimization, if there
  8. # is more than will fit in the numbered batches, we put the next few
  9. # lots in togo.next, and use that thereafter until it's empty.  This
  10. # avoids the need to paw through the whole huge list every time when
  11. # a large backlog has built up.  We also punt to sed to trim the big
  12. # list when we do process it, avoiding the need to run it all through awk.
  13. #
  14. # If the togo files do not contain file sizes, we make an arbitrary guess
  15. # at an average size.
  16.  
  17. case "$1" in
  18. '')    echo 'Usage: batchsplit size [nwanted]' >&2
  19.     exit 2
  20.     ;;
  21. -x)    lotsmissing=0
  22.     shift
  23.     ;;
  24. esac
  25.  
  26. case "$2" in
  27. [1-7])    nwanted="$2"    ;;
  28. *)    nwanted=7    ;;
  29. esac
  30.  
  31. # =()<. ${NEWSCONFIG-@<NEWSCONFIG>@}>()=
  32. . ${NEWSCONFIG-/var/lib/news/bin/config}
  33.  
  34. PATH=$NEWSCTL/bin:$NEWSBIN/batch:$NEWSBIN:$NEWSPATH ; export PATH
  35. umask $NEWSUMASK
  36.  
  37. # pick an input file, shuffling togo aside (with locking) if needed
  38. if test -s togo.next
  39. then
  40.     input=togo.next
  41. elif test -s togo.more
  42. then
  43.     input=togo.more
  44. else
  45.     # Locking.
  46.     lock="$NEWSCTL/LOCK"
  47.     ltemp="$NEWSCTL/L.$$"
  48.     echo $$ >$ltemp
  49.     trap "rm -f $ltemp ; exit 0" 0 1 2 15
  50.     while true
  51.     do
  52.         if newslock $ltemp $lock
  53.         then
  54.             trap "rm -f $ltemp $lock ; exit 0" 0 1 2 15
  55.             break
  56.         fi
  57.         sleep 30
  58.     done
  59.  
  60.     # Do it.
  61.     rm -f togo.more
  62.     mv togo togo.more
  63.     >togo
  64.     input=togo.more
  65.  
  66.     # Unlock.
  67.     trap 0 1 2 15
  68.     rm -f $ltemp $lock
  69. fi
  70.  
  71. # A little precaution... do there seem to be a lot of nonexistent files?
  72. # Check first three as quick screening, check next fifty to decide whether
  73. # a relatively-costly existence filtering is in order.
  74. nextonly=0
  75. lotsmissing=${lotsmissing-25}
  76. if test " `sed 3q $input | batchcheck -v | wc -l`" -gt 0 && \
  77.     test " `sed 50q $input | batchcheck -v | wc -l`" -gt $lotsmissing
  78. then
  79.     # need to filter togo.next, or generate one for filtering
  80.     case "$input" in
  81.     togo.next)    batchcheck <togo.next >togo.tmp
  82.             mv togo.tmp togo.next
  83.             if test ! -s togo.next
  84.             then
  85.                 # it's really bad
  86.                 rm -f togo.next
  87.                 input=togo.more
  88.                 nextonly=1
  89.             fi
  90.             ;;
  91.     togo.more)    nextonly=1        ;;
  92.     esac
  93. fi
  94.  
  95. # main processing
  96. rm -f togo.overflow togo.count
  97. awk 'BEGIN { total = 0 ; ninbatch = 0 ; bno = 1 ; limit = '$1'
  98.         batch = "togo." bno ; nbatches = '"$nwanted"'
  99.         if ('$nextonly' == 1) {
  100.             # just make a new togo.next, no togo.[0-9] batches
  101.             bno = nbatches
  102.             ninbatch = 1
  103.             total = limit+1
  104.         }
  105.     }
  106.     {
  107.         if (NF == 1) {
  108.             if ($1 ~ /^<.*>$/)    # probably ihave/sendme m-id
  109.                 size = length($0) + 1
  110.             else
  111.                 size = 3000    # Arbitrary guess.
  112.         } else
  113.             size = $NF + 15        # 15 for "#! rnews nnnnn"
  114.         if (total + size > limit && ninbatch > 0) {
  115.             # Go to next batch.
  116.             bno++
  117.             if (bno <= nbatches) {
  118.                 batch = "togo." bno
  119.                 ninbatch = 0
  120.             } else if (bno == nbatches+1 && FILENAME == "togo.more") {
  121.                 batch = "togo.next"
  122.                 limit = 4 * nbatches * limit
  123.             } else {
  124.                 print NR - 1 >"togo.count"
  125.                 exit
  126.             }
  127.             total = 0
  128.         }
  129.         ninbatch++
  130.         total += size
  131.         print >batch
  132.     }' $input
  133.  
  134. # handle the overflow case efficiently
  135. if test -s togo.count
  136. then
  137.     sed "1,`cat togo.count`d" $input >togo.overflow
  138.     rm togo.count
  139.     mv togo.overflow $input
  140. else
  141.     rm $input
  142. fi
  143.