home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / auucp+-1.02 / fuucp_plus_src.lzh / unsharmap / getmap < prev    next >
Encoding:
Text File  |  1990-11-21  |  5.9 KB  |  227 lines

  1. #! /bin/sh
  2. # @(#)getmap.sh    1.15 2/5/89 14:13:35
  3. #
  4. #    Copyright (C) 1988 Ronald S. Karr and Landon Curt Noll
  5. #
  6. # See the file COPYING, distributed with smail, for restriction
  7. # and warranty information.
  8.  
  9. # getmap - unshar usenet map articles into the UNSHAR_MAP_DIR directory
  10. #
  11. # usage: getmap [-m mapdir] [-w workdir] [-b batchfile] [-u username]
  12. #
  13. #     -m mapdir    - 'mapdir' is where maps are unpacked,
  14. #              by default 'mapdir' is $UNSHAR_MAP_DIR
  15. #     -w workdir    - where logs and default batch files are kept,
  16. #              by default 'workdir' is 'mapdir'/work
  17. #      -b batch    - 'batch' is a file of filenames to unshar,
  18. #              '-' impiles read filenames from stdin,
  19. #              by default 'batch' is 'workdir'/batch
  20. #     -u username    - errors are Emailed to 'username' rather than
  21. #              Posmaster, the username '-' imples that
  22. #              the errors should be written to standard error
  23. #     -n newsgroups    - allowed newsgroups for articles
  24. #
  25. # unsharmap errors will be emailed to the postmaster.
  26. #
  27. # default files:
  28. #    $UNSHAR_MAP_DIR            - maps are unpacked here
  29. #    $UNSHAR_MAP_DIR/work/getmap.log    - log of getmap activity and errors
  30. #    $UNSHAR_MAP_DIR/work/getmap.err    - like getmap.log + skipped lines
  31. #                      removed if no major unshar errors
  32. #    $UNSHAR_MAP_DIR/work/batch        - default list of artciles to unshar
  33. #    batch.work                - tmp copy of batch work, normally
  34. #                      $UNSHAR_MAP_DIR/work/batch.work,
  35. #                      ignored if "-w -"
  36.  
  37. # locations and constants
  38. #
  39. PATH="/usr/bin:/bin:/usr/local/bin"
  40. UTIL_BIN="/usr/spool/maps/bin"
  41. UNSHAR_MAP_DIR="/usr/spool/maps"
  42. UNSHAR=$UTIL_BIN/unsharmap
  43.  
  44. # set defaults value for location that can change by option
  45. #
  46. MAPDIR=$UNSHAR_MAP_DIR
  47. WORKDIR=$MAPDIR/work
  48. BATCH=$WORKDIR/batch
  49. USERNAME=postmaster
  50. NEWSGROUPS=comp.mail.maps:sub.config.maps
  51.  
  52. # parse args
  53. #
  54. PROG=$0
  55. USAGE="usage: $PROG [-m mapdir] [-w workdir] [-b batch] [-u username]"
  56. set -- `getopt -n $PROG -q m:w:b:u:n: $*`
  57. if [ $? != 0 ]; then
  58.     echo $USAGE 1>&2
  59.     exit 1
  60. fi
  61. for i in $*; do
  62.     case $i in
  63.     -m) MAPDIR=$2; shift 2;;
  64.     -w) WORKDIR=$2; shift 2;;
  65.     -b) BATCH=$2; shift 2;;
  66.     -u) USERNAME=$2; shift 2;;
  67.     -n) NEWSGROUPS=$2; shift 2;;
  68.     --) shift; break;;
  69.     esac
  70. done
  71. if [ "$#" -ne 0 ]; then
  72.     echo $USAGE 1>&2
  73.     exit 2
  74. fi
  75. if [ "$BATCH" = "-" ]; then    # catch stdin case
  76.     BATCH=
  77. fi
  78. if [ "$USERNAME" = "-" ]; then    # catch the cat errors to stderr case
  79.     REPORT="cat 1>&2"
  80. else
  81.     REPORT="/usr/local/lib/sendmail -i -t"
  82. fi
  83.  
  84. # set locations now that we have the flags
  85. #
  86. LOG=$WORKDIR/getmap.log
  87. ERROR_LOG=$WORKDIR/getmap.err
  88. REBUILD=$WORKDIR/getmap.rebuild
  89.  
  90. # be sure the working file does not exist, unless we read from stdin
  91. #
  92. if [ ! -z "$BATCH" ]; then
  93.     BATCH_TMP="$BATCH".work
  94.     if [ -f "$BATCH_TMP" ]; then
  95.         echo "$PROG: working batch file $BATCH_TMP exists" 1>&2
  96.         echo "$PROG: remove $BATCH_TMP by hand if stale" 1>&2
  97.         exit 3
  98.     fi
  99. fi
  100.  
  101. # setup log files
  102. #
  103. if [ ! -z "$BATCH" ]; then
  104.     BATCH_MSG="$PROG: starting work on $BATCH at `date`"
  105. else
  106.     BATCH_MSG="$PROG: starting work on [stdin] at `date`"
  107. fi
  108. echo "$BATCH_MSG" > $ERROR_LOG
  109. if [ "$?" -ne 0 ]; then
  110.     echo "$PROG: can not clear $ERROR_LOG" 1>&2
  111.     exit 4
  112. fi
  113. touch $LOG 2>/dev/null
  114. if [ "$?" -ne 0 ]; then
  115.     echo "$PROG: can not write to $LOG" 1>&2
  116.     echo "$PROG: can not write to $LOG" >> $ERROR_LOG
  117.     (if [ "$USERNAME" != "-" ]; then
  118.     echo "To: $USERNAME"
  119.     echo "Subject: getmap error"
  120.     echo ""
  121.     echo ""
  122.      fi
  123.      echo "getmap error log follows -----"
  124.      cat $ERROR_LOG
  125.      echo "end of getmap error log =====") | eval "$REPORT"
  126.     exit 5
  127. fi
  128.  
  129.  
  130. # save the batch file, if not from stdin
  131. #
  132. if [ ! -z "$BATCH" ]; then
  133.     # do nothing if no batch file or no work in the batch file
  134.     if [ ! -f "$BATCH" -o ! -s "$BATCH" ]; then
  135.     echo "$PROG: no work in $BATCH" >> $LOG
  136.     rm -f $ERROR_LOG
  137.         exit 0
  138.     fi
  139.     mv $BATCH $BATCH_TMP
  140.     if [ "$?" -ne 0 ]; then
  141.         echo "$PROG: could not move $BATCH to $BATCH_TMP" 1>&2
  142.         echo "$PROG: could not move $BATCH to $BATCH_TMP" >> $ERROR_LOG
  143.     (if [ "$USERNAME" != "-" ]; then
  144.         echo "To: $USERNAME"
  145.         echo "Subject: getmap error"
  146.         echo ""
  147.         echo ""
  148.      fi
  149.      echo "getmap error log follows -----"
  150.      cat $ERROR_LOG
  151.      echo "end of getmap error log =====") | eval "$REPORT"
  152.     cat $ERROR_LOG >> $LOG
  153.     exit 6
  154.     fi
  155.  
  156. # if work from stdin, prep a file to be used to save a copy
  157. #
  158. else
  159.     BATCH_TMP=$WORKDIR/getmap.in$$
  160.     cat /dev/null > $BATCH_TMP
  161.     if [ "$?" -ne 0 ]; then
  162.         echo "$PROG: could not clear $BATCH_TMP to hold a copy of [stdin]" 1>&2
  163.         echo "$PROG: could not clear $BATCH_TMP to hold a copy of [stdin]" >> $ERROR_LOG
  164.     (if [ "$USERNAME" != "-" ]; then
  165.         echo "To: $USERNAME"
  166.         echo "Subject: getmap error"
  167.         echo ""
  168.         echo ""
  169.      fi
  170.      echo "getmap error log follows -----"
  171.      cat $ERROR_LOG
  172.      echo "end of getmap error log =====") | eval "$REPORT"
  173.     cat $ERROR_LOG >> $LOG
  174.     exit 7
  175.     fi
  176. fi
  177.  
  178. # process the map artcile files
  179. #
  180. if [ ! -z "$BATCH" ]; then
  181.     $UNSHAR -d $MAPDIR -n $NEWSGROUPS < $BATCH_TMP >> $ERROR_LOG
  182.     STATUS=$?
  183. else
  184.     tee -a $BATCH_TMP | $UNSHAR -d $MAPDIR >> $ERROR_LOG
  185.     STATUS=$?
  186. fi
  187.  
  188. # note if we unpacked anything
  189. if [ -s "$BATCH_TMP" ]; then
  190.     touch $REBUILD
  191. fi
  192.  
  193. # log the activity
  194. #
  195. cat $ERROR_LOG >> $LOG
  196.  
  197. # post processing - look for errors to report
  198. #
  199. if [ "$STATUS" -ne 0 ]; then
  200.     # form the mail message header
  201.     (if [ "$USERNAME" != "-" ]; then
  202.     echo "To: $USERNAME"
  203.     echo "Subject: getmap error"
  204.     echo ""
  205.     echo ""
  206.      fi
  207.      echo "getmap error log follows -----"
  208.      cat $ERROR_LOG
  209.      echo "end of getmap error log ====="
  210.      echo ""
  211.      echo ""
  212.      if [ ! -z "$BATCH" ]; then
  213.          echo "$BATCH_TMP work queue follows -----"
  214.          cat $BATCH_TMP
  215.          echo "end of $BATCH_TMP work queue ====="
  216.      else
  217.          echo "[stdin] work queue follows -----"
  218.      cat $BATCH_TMP
  219.          echo "end of [stdin] work queue ====="
  220.      fi) | eval "$REPORT"
  221. else
  222.     rm -f $ERROR_LOG    # no error, so remove the error log
  223. fi
  224. rm -f $BATCH_TMP
  225.  
  226. exit 0
  227.