home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 400-499 / ff473.lzh / CNewsSrc / cnews_src.lzh / batch / batchsplit < prev    next >
Text File  |  1990-05-28  |  3KB  |  103 lines

  1. #! /bin/sh
  2. # Prepare some batches of size $1 in files named togo.[0-9] .  We prepare a
  3. # total of 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. # Buglet:  does not count the "#! rnews nnnnn" headers in sizes.
  15. #
  16. # If the togo files do not contain file sizes, we make an arbitrary guess
  17. # at an average size.
  18.  
  19. case $# in
  20. 0)    echo 'Usage: batchsplit size' >&2
  21.     exit 2
  22.     ;;
  23. esac
  24.  
  25. # =()<. ${NEWSCONFIG-@<NEWSCONFIG>@}>()=
  26. . ${NEWSCONFIG-/usr/lib/news/bin/config}
  27.  
  28. PATH=$NEWSCTL/bin:$NEWSBIN/batch:$NEWSBIN:$NEWSPATH ; export PATH
  29. umask $NEWSUMASK
  30.  
  31. # pick an input file, shuffling togo aside (with locking) if needed
  32. if test -s togo.next
  33. then
  34.     input=togo.next
  35. elif test -s togo.more
  36. then
  37.     input=togo.more
  38. else
  39.     # Locking.
  40.     lock="$NEWSCTL/LOCK"
  41.     ltemp="$NEWSCTL/L.$$"
  42.     echo $$ >$ltemp
  43.     trap "rm -f $ltemp ; exit 0" 0 1 2 15
  44.     while true
  45.     do
  46.         if newslock $ltemp $lock
  47.         then
  48.             trap "rm -f $ltemp $lock ; exit 0" 0 1 2 15
  49.             break
  50.         fi
  51.         sleep 30
  52.     done
  53.  
  54.     # Do it.
  55.     rm -f togo.more
  56.     mv togo togo.more
  57.     >togo
  58.     input=togo.more
  59.  
  60.     # Unlock.
  61.     trap 0 1 2 15
  62.     rm -f $ltemp $lock
  63. fi
  64.  
  65. # main processing
  66. rm -f togo.overflow togo.count
  67. awk 'BEGIN { total = 0 ; ninbatch = 0 ; bno = 1 ; limit = '$1'
  68.         batch = "togo." bno ; nbatches = 7 }
  69.     {
  70.         if (NF == 1)
  71.             size = 3000    # Arbitrary guess.
  72.         else
  73.             size = $NF
  74.         if (total + size > limit && ninbatch > 0) {
  75.             # Go to next batch.
  76.             bno++
  77.             if (bno <= nbatches) {
  78.                 batch = "togo." bno
  79.                 ninbatch = 0
  80.             } else if (bno == nbatches+1 && FILENAME == "togo.more") {
  81.                 batch = "togo.next"
  82.                 limit = 4 * nbatches * limit
  83.             } else {
  84.                 print NR - 1 >"togo.count"
  85.                 exit
  86.             }
  87.             total = 0
  88.         }
  89.         ninbatch++
  90.         total += size
  91.         print >batch
  92.     }' $input
  93.  
  94. # handle the overflow case efficiently
  95. if test -s togo.count
  96. then
  97.     sed "1,`cat togo.count`d" $input >togo.overflow
  98.     rm togo.count
  99.     mv togo.overflow $input
  100. else
  101.     rm $input
  102. fi
  103.