home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / admin / news / n_upact < prev    next >
Encoding:
Korn shell script  |  1997-08-26  |  4.0 KB  |  179 lines

  1. #!/bin/ksh
  2. # @(#) n_upact.sh
  3. # 92/08/18 john dubois   Replaced main loop for performance
  4. # 92/08/25 Only make sure oldart is at least 5 digits, like old code did
  5. # 94/02/06 Sped up, added code to notice problems, don't replace active file if
  6. #          the new one doesn't have the same number of lines as the old one.
  7. # 94/07/13 chown/chgrp new file to news, in case not run as news.
  8. #          Skip groups already seen.
  9.  
  10. # n_upact: Update 3rd field (minimum art. #) of a 4-field active file.
  11.  
  12. # =()<. ${NEWSCONFIG-@<NEWSCONFIG>@}>()=
  13. . ${NEWSCONFIG-/usr/lib/news/config}
  14.  
  15. PATH=$NEWSCTL/bin:$NEWSBIN/expire:$NEWSBIN:$NEWSPATH ; export PATH
  16. umask $NEWSUMASK
  17.  
  18. cd $NEWSCTL || { echo "$0: can't cd to $NEWSCTL" >&2; exit 1; }
  19.  
  20. # check active file format
  21. set ""`sed 1q active`
  22. case $# in
  23. 4)    ;;
  24. *)    echo "$0: active file has other than 4 fields" >&2
  25.     exit 1 ;;
  26. esac
  27.  
  28. rm -f active.tmp
  29. if test -f active.tmp
  30. then
  31.     echo "$0: active.tmp exists and can't be removed; aborting" >&2
  32.     exit 1
  33. fi
  34.  
  35. # lock news system
  36. lock="$NEWSCTL/LOCK"
  37. ltemp="$NEWSCTL/L.$$"
  38. echo $$ >$ltemp
  39. trap "rm -f $ltemp ; exit 0" 0 1 2 15
  40. while true
  41. do
  42.     if newslock $ltemp $lock
  43.     then
  44.     trap "rm -f $ltemp $lock ; exit 0" 0 1 2 15
  45.     break
  46.     fi
  47.     sleep 30
  48. done
  49.  
  50.  
  51. # This only works in ksh
  52. OIFS=$IFS
  53. IFS="
  54. $OIFS"
  55. # Generate list of newsgroup directories, followed by entire active line
  56. awk '
  57. {
  58.     dir = $1
  59.     gsub(/\./,"/",dir)
  60.     print dir " " $0
  61. }
  62. ' $NEWSCTL/active |
  63. while read dir actline; do 
  64.     # For every group, mark its beginning with the active line 
  65.     # and then echo every article name
  66.     SpoolDir=$NEWSARTS/$dir
  67.     echo ":active $actline"
  68.     if [ -d $SpoolDir ]; then
  69.     if cd $SpoolDir; then
  70.         set -- *
  71.         echo "$*"
  72.     else
  73.         print -u2 "Warning: could not cd to spool dir '$SpoolDir'."
  74.     fi
  75.     fi
  76. done | awk '
  77. /^:active/ {
  78.     ActLine++
  79.     if (Group != "" && NoWriteLine == 0)
  80.     WriteLine()
  81.     NoWriteLine = 0
  82.     Group = $2
  83.     if ($2 in SeenGroups) {
  84.     printf "ERROR: already saw group \"%s\" on line %d of old active file;\n"\
  85.     "skipping this one (on line %d)!\n",
  86.     Group,SeenGroups[$2],ActLine | "cat 1>&2"
  87.     NoWriteLine = 1
  88.     DupCt++
  89.     }
  90.     SeenGroups[$2] = ActLine
  91.     OrigMaxArt = MaxArt = $3
  92.     OrigMinArt = $4
  93.     MinArt = OrigMaxArt + 1
  94.     FField = $5
  95.     next
  96. }
  97.  
  98. /^[0-9]+$/ {
  99.     if ($0 + 0 < MinArt + 0)
  100.     MinArt = $0
  101.     if ($0 + 0 > MaxArt + 0)
  102.     MaxArt = $0
  103.     next
  104. }
  105.  
  106. # No files = *, other files are probably directories
  107. $0 == "*" || $0 ~ /^[-a-zA-Z.0-9]+$/ {
  108.     next
  109. }
  110.  
  111. {
  112.     printf "upact: strange file in spool dir for group %s: %s\n",
  113.     Group,$0 | "cat 1>&2"
  114. }
  115.  
  116. function WriteLine() {
  117.     if (MinArt < OrigMinArt) {
  118.     printf "upact: NOTICE: group %s: earliest article found (%d)\n"\
  119.     "is lower than original min article (%d).\n",
  120.     Group,MinArt,OrigMinArt | "cat 1>&2"
  121.     }
  122.     if (MaxArt > OrigMaxArt) {
  123.     printf "upact: NOTICE: group %s: latest article found (%d)\n"\
  124.     "is higher than original max article (%d).  Fixed.\n",
  125.     Group,MaxArt,OrigMaxArt | "cat 1>&2"
  126.     OrigMaxArt = MaxArt
  127.     }
  128.     printf "%s %010d %05d %s\n",Group,OrigMaxArt,MinArt,FField
  129. }
  130.  
  131. END {
  132.     WriteLine()
  133.     if (DupCt > 0)
  134.     printf "upact: %d duplicate groups found\n",DupCt | "cat 1>&2"
  135. }
  136. ' > $NEWSCTL/active.tmp
  137.  
  138. IFS=$OIFS
  139.  
  140. cd $NEWSCTL
  141.  
  142. #while read group max min fourth
  143. #do
  144. #    dir=`echo $group | tr . / `    # map ng name to directory name
  145. #    min=
  146. #    if test -d $NEWSARTS/$dir
  147. #    then
  148. #        min=`ls $NEWSARTS/$dir | egrep '^[0-9]+$' | sort -nr | tail -1`
  149. #    fi
  150. #    case "$min" in        # no files, so use max+1
  151. #    "")    min=`awk "END{ print $max + 1 }" /dev/null`    ;;
  152. #    esac
  153. #    case "$min" in
  154. #    [0-9]|[0-9][0-9]|[0-9][0-9][0-9]|[0-9][0-9][0-9][0-9])    # short
  155. #        min=`expr 00000$min : '.*\(.....\)$'` ;;
  156. #    esac
  157. #
  158. #    echo $group $max $min $fourth
  159. #done <active >active.tmp
  160.  
  161. # replace active, carefully
  162. set -- `wc -l active`
  163. NOldGroups=$1
  164. set -- `wc -l active.tmp`
  165. if [ $1 -ne $NOldGroups ]; then
  166.     echo \
  167. "upact: Error generating new active file:
  168. active has $NOldGroups groups; new active has $1 groups
  169. (may be due to duplicate groups).  Aborting." 1>&2
  170.     exit 1
  171. fi
  172. rm -f active.old
  173. ln active active.old
  174. chown news active.tmp
  175. chgrp news active.tmp
  176. mv active.tmp active
  177.  
  178. exit 0
  179.