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

  1. #! /bin/sh
  2. # Prepare up to $3 batches of nominal size $1, max size $2, bytes in files
  3. #   named togo.[0-9] .
  4. # Exit status:  0 some have been prepared, 1 none can be.
  5. # This is now just central administration, with batchsplitter doing the work.
  6.  
  7. # =()<. ${NEWSCONFIG-@<NEWSCONFIG>@}>()=
  8. . ${NEWSCONFIG-/etc/news/bin/config}
  9.  
  10. PATH=$NEWSCTL/bin:$NEWSBIN/batch:$NEWSBIN:$NEWSPATH ; export PATH
  11. umask $NEWSUMASK
  12.  
  13. regress=n
  14. case "$1" in
  15. '')    echo "Usage: $0 size max [nwanted]" >&2
  16.     exit 2
  17.     ;;
  18. -X)    regress=y
  19.     shift
  20.     ;;
  21. esac
  22.  
  23. nominal=$1
  24. max=$2
  25.  
  26. # Limit $3 to 7, to stay within awk's limits on file descriptors.
  27. case "$3" in
  28. [1-7])    nwanted="$3"    ;;
  29. *)    nwanted=7    ;;
  30. esac
  31.  
  32. # We ultimately work from togo, but if it's all we've got, we immediately
  33. # rename it togo.more so that we can unlock the news system.  If there
  34. # is more than will fit in the numbered batches, we put the next few
  35. # lots in togo.next, and use that thereafter until it's empty.  This
  36. # avoids the need to paw through a long togo.more every time when there's
  37. # a large backlog.
  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.     sigs='1 2 15'
  46.     lock LOCKexplode $$ || exit 1
  47.     trap "unlock LOCKexplode ; exit 1" $sigs
  48.     rm -f togo.more
  49.     mv togo togo.more
  50.     >togo
  51.     input=togo.more
  52.     trap $sigs
  53.     unlock LOCKexplode
  54. fi
  55.  
  56. # A little precaution... do there seem to be a lot of nonexistent files?
  57. # Check first three as quick screening, check next fifty to decide whether
  58. # a relatively-costly existence filtering is in order.
  59. nextonly=0
  60. lotsmissing=25
  61. if test " $regress" = " y"
  62. then
  63.     lotsmissing=0
  64. fi
  65. if test " `sed 3q $input | batchcheck -v | wc -l`" -gt 0 && \
  66.     test " `sed 50q $input | batchcheck -v | wc -l`" -gt $lotsmissing
  67. then
  68.     # We filter only togo.next, because filtering a big togo.more is
  69.     # a tedious job and best done a bit at a time.
  70.     while test -s $input
  71.     do
  72.         if test " $input" != " togo.next"
  73.         then
  74.             batchsplitter $input $nominal $max 0
  75.             input=togo.next
  76.         fi
  77.         batchcheck <togo.next >togo.tmp
  78.         mv togo.tmp togo.next
  79.         test -s togo.next && break    # if have articles, BREAK OUT
  80.         input=togo.more            # no luck -- try again
  81.     done
  82. fi
  83.  
  84. if test -s $input
  85. then
  86.     batchsplitter $input $nominal $max $nwanted
  87.     exit 0
  88. else
  89.     exit 1
  90. fi
  91.