/[MITgcm]/mitgcm.org/front_content/parse_emails
ViewVC logotype

Diff of /mitgcm.org/front_content/parse_emails

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.9 by jmc, Fri Nov 30 00:45:55 2007 UTC revision 1.23 by jmc, Wed May 5 15:16:41 2010 UTC
# Line 11  usage() Line 11  usage()
11  {  {
12      echo      echo
13      echo "Usage:  $0 [OPTIONS]"      echo "Usage:  $0 [OPTIONS]"
14      echo      echo
15      echo "where possible OPTIONS are:"      echo "where possible OPTIONS are:"
16      echo "  (-help|-h)           print usage"      echo "  (-h|-help)           print usage"
17      echo "  (-ind |-i )DIR       get mpack-created emails from DIR"      echo "  (-s|-silent)         silent mode"
18        echo "  (-v|-verbose)        verbose mode"
19        echo "  (-i |-ind )DIR       get mpack-created emails from DIR"
20      echo "                         [def=\"$INDIR\"]"      echo "                         [def=\"$INDIR\"]"
21      echo "  (-outd |-o )DIR      write the data to DIR"      echo "  (-o |-outd )DIR      write the data to DIR"
22      echo "                         [def=\"$OUTDIR\"]"      echo "                         [def=\"$BASEDIR/$monthDir\"]"
23      echo "  (-tempd |-t )DIR     use temporary directory DIR"      echo "  (-t |-tempd )DIR     use temporary directory DIR"
24      echo "                         [def=\"$TEMPDIR\"]"      echo "                         [def=\"$TEMPDIR\"]"
25      echo      echo "  (-u |-unpack )EXE    use executable EXE to unpack e-mails"
26        echo "                         [def=\"$MUNPACK\"]"
27        echo "  (-a |-addr )ADDR     send e-mail to ADDR if Error"
28        echo "                         [def='"$ADDRERR"']"
29        echo
30      exit 1      exit 1
31  }  }
32    
33  # defaults  # defaults
34  INDIR="/u/edhill/Mail/MITgcm-test"  INDIR="/u/u2/jmc/Mail/MITgcm-test"
35    BASEDIR="/u/u0/httpd/html/testing/results"
36    monthDir=`date +%Y`"_"`date +%m`
37  OUTDIR=  OUTDIR=
38  TEMPDIR=./ptmp  TEMPDIR=/tmp/prc_emails
39  MUNPACK=/u/edhill/local/bin/munpack  ERRMSG=/tmp/tmp.$$
40    MUNPACK=munpack
41    ADDRERR=
42    PRT=1
43    
44  #  Parse options  #  Parse options
45  ac_prev=  ac_prev=
# Line 42  for ac_option ; do Line 53  for ac_option ; do
53      fi      fi
54    
55      ac_optarg=`expr "x$ac_option" : 'x[^=]*=\(.*\)'`      ac_optarg=`expr "x$ac_option" : 'x[^=]*=\(.*\)'`
56        
57      case $ac_option in      case $ac_option in
58                    
59          -help | --help | -h | --h)          -help | --help | -h | --h)
60              usage ;;              usage ;;
61            -s | --s | -silent | --silent)
62                PRT=0 ;;
63            -v | --v | -verbose | --verbose)
64                PRT=2 ;;
65                    
66          -ind | --ind | -i | --i)          -ind | --ind | -i | --i)
67              ac_prev=INDIR ;;              ac_prev=INDIR ;;
68          --ind=* | -ind=* | --i=* | -i=*)          --ind=* | -ind=* | --i=* | -i=*)
69              INDIR=$ac_optarg ;;              INDIR=$ac_optarg ;;
70            
71          -outd | --outd | -o | --o)          -outd | --outd | -o | --o)
72              ac_prev=OUTDIR ;;              ac_prev=OUTDIR ;;
73          --outd=* | -outd=* | --o=* | -o=*)          --outd=* | -outd=* | --o=* | -o=*)
74              OUTDIR=$ac_optarg ;;              OUTDIR=$ac_optarg ;;
75            
76          -tempd | --tempd | -t | --t)          -tempd | --tempd | -t | --t)
77              ac_prev=TEMPDIR ;;              ac_prev=TEMPDIR ;;
78          --tempd=* | -tempd=* | --t=* | -t=*)          --tempd=* | -tempd=* | --t=* | -t=*)
79              TEMPDIR=$ac_optarg ;;              TEMPDIR=$ac_optarg ;;
80            
81            -u | --u | -unpack | --unpack)
82                ac_prev=MUNPACK ;;
83            -u=* | --u=* | -unpack=* | --unpack=*)
84                MUNPACK=$ac_optarg ;;
85    
86            -a | --a | -addr | --addr)
87                ac_prev=ADDRERR ;;
88            -a=* | --a=* | -addr=* | --addr=*)
89                ADDRERR=$ac_optarg ;;
90    
91          *)          *)
92              # copy the file list to FL_#              # copy the file list to FL_#
93                date
94              echo "Error: don't understand argument \"$ac_option\""              echo "Error: don't understand argument \"$ac_option\""
95              usage              usage
96              ;;              ;;
97            
98       esac       esac
99        
100  done  done
101    
102    if test ! -x $MUNPACK ; then
103            date
104            echo "ERROR: \"$MUNPACK\" is not executable"
105            exit 2
106    fi
107  if test "x$OUTDIR" = x ; then  if test "x$OUTDIR" = x ; then
108      OUTDIR="/u/u0/httpd/html/testing/results/"`date +%Y`"_"`date +%m`    OUTDIR="$BASEDIR/$monthDir"
109    else
110      monthDir=0
111  fi  fi
112  if test ! -e $OUTDIR ; then  if test ! -e $OUTDIR ; then
113      mkdir $OUTDIR      mkdir $OUTDIR
114      RETVAL=$?      RETVAL=$?
115      if test "x$RETVAL" = x ; then      if test "x$RETVAL" != x0 ; then
116            date
117          echo "ERROR: directory \"$OUTDIR\" doesn't exist and can't be created"          echo "ERROR: directory \"$OUTDIR\" doesn't exist and can't be created"
118          exit 1          exit 3
119        else
120            echo "Successfully created new dir: \"$OUTDIR\""
121      fi      fi
122      chgrp gcmpack $OUTDIR      chgrp gcmpack $OUTDIR
123      chmod 775 $OUTDIR      chmod 775 $OUTDIR
124  fi  fi
125    
 echo "Using OUTDIR=\"$OUTDIR\""  
 echo "Using INDIR=\"$INDIR\""  
   
126  all_files=`ls -1 $INDIR`  all_files=`ls -1 $INDIR`
127    nb_files=`echo "$all_files" | grep -c '^msg\.'`
128    
129    if test $PRT = 1 ; then
130      echo "Using OUTDIR=\"$OUTDIR\""
131      echo "Using INDIR=\"$INDIR\""
132      echo -n "Unpacking the emails ..."
133    elif test $nb_files != 0 ; then
134      echo -n "Unpacking $nb_files emails ("`date`
135      if test "x$ADDRERR" != x ; then
136         echo -n ", err-msg: '$ADDRERR'"
137      fi
138      echo ")"
139      echo " from '$INDIR' to '$OUTDIR'"
140    fi
141    
 echo -n "Unpacking the emails ..."  
142  for file in $all_files ; do  for file in $all_files ; do
143    
144      #  create local copy      #-- create local copy
145      test -e $TEMPDIR  &&  rm -rf $TEMPDIR      test -e $TEMPDIR  &&  rm -rf $TEMPDIR
146      mkdir $TEMPDIR      mkdir $TEMPDIR
147      cp $INDIR"/"$file $TEMPDIR      RETVAL=$?
148        if test "x$RETVAL" = x0 ; then
149          cp $INDIR"/"$file $TEMPDIR
150          RETVAL=$?
151        fi
152        if test "x$RETVAL" != x0 ; then
153          if test "x$ADDRERR" != x ; then
154            echo "parsing email error" > $ERRMSG
155            echo " processing file: '$INDIR/$file'" >> $ERRMSG
156            echo -n "'mkdir $TEMPDIR' or 'cp $INDIR/$file $TEMPDIR'" >> $ERRMSG
157            echo " returns error $RETVAL" >> $ERRMSG
158            mail -s 'parse_emails err_0' $ADDRERR < $ERRMSG
159            rm -f $ERRMSG
160          fi
161          mv -f $INDIR"/"$file $INDIR"/../fail2process/"$file
162          continue
163        fi
164    
165      #  ignore multi-part messages      #-- ignore multi-part messages
166      grep "Content-Type: message/partial" $INDIR"/"$file > /dev/null 2>&1      grep "Content-Type: message/partial" $INDIR"/"$file > /dev/null 2>&1
167      RETVAL=$?      RETVAL=$?
168      if test "x$RETVAL" = x0 ; then      if test "x$RETVAL" = x0 ; then
169          continue        if test "x$ADDRERR" != x ; then
170            echo "parsing email error" > $ERRMSG
171            echo 'grep "Content-Type: message/partial" returns error:' $RETVAL >> $ERRMSG
172            ls -l $INDIR"/"$file >> $ERRMSG
173            mail -s 'parse_emails err_1' $ADDRERR < $ERRMSG
174            rm -f $ERRMSG
175          fi
176          mv -f $INDIR"/"$file $INDIR"/../fail2process/"$file
177          continue
178      fi      fi
179    
180      #  munpack      #-- munpack
181      mun=`( cd $TEMPDIR ; $MUNPACK $file | cut -d ' ' -f 1 | head -1 )`      mun=`( cd $TEMPDIR ; $MUNPACK $file | cut -d ' ' -f 1 | head -1 )`
182      RETVAL=$?      RETVAL=$?
183      if test "x$RETVAL" != x0 ; then      if test "x$RETVAL" != x0 ; then
184          continue        if test "x$ADDRERR" != x ; then
185            echo "parsing email error" > $ERRMSG
186            echo "$MUNPACK $file returns error: $RETVAL" >> $ERRMSG
187            ls -l $INDIR"/"$file >> $ERRMSG
188            mail -s 'parse_emails err_2' $ADDRERR < $ERRMSG
189            rm -f $ERRMSG
190          fi
191          mv -f $INDIR"/"$file $INDIR"/../fail2process/"$file
192          continue
193      fi      fi
194    
195      #  un-tar      #-- un-tar
196      ( cd $TEMPDIR ; tar -xzvf $mun > out )      #( cd $TEMPDIR ; tar -xzvf $mun > out )
197        #   to remove small files "._mydir" that some MAC OS are adding
198        #   (for each file or dir) to a tar-file, use option "--exclude=":
199        ( cd $TEMPDIR ; tar -xzvf $mun --exclude="._*" > out )
200      RETVAL=$?      RETVAL=$?
201      if test "x$RETVAL" != x0 ; then      if test "x$RETVAL" != x0 ; then
202          continue        if test "x$ADDRERR" != x ; then
203            echo "parsing email error" > $ERRMSG
204            echo "tar -xzvf $mun returns error:" $RETVAL >> $ERRMSG
205            ls -l $INDIR"/"$file >> $ERRMSG
206            ls -l $mun >> $ERRMSG
207            mail -s 'parse_emails err_3a' $ADDRERR < $ERRMSG
208            rm -f $ERRMSG
209          fi
210          mv -f $INDIR"/"$file $INDIR"/../fail2process/"$file
211          continue
212      fi      fi
213      tdir=`cat $TEMPDIR"/out" | head -1 | sed -e 's|^./||g' | cut -d '/' -f 1`      tdir=`cat $TEMPDIR"/out" | head -1 | sed -e 's|^./||g' | cut -d '/' -f 1`
214      rm -f $TEMPDIR"/out"     #tdir=`( cd $TEMPDIR ; /bin/ls -l | grep '^d' | head -1 | awk '{print $NF}' )`
215        if test -d $TEMPDIR/$tdir ; then
216          rm -f $TEMPDIR"/out"
217        else
218          if test "x$ADDRERR" != x ; then
219            echo "parsing email error" > $ERRMSG
220            echo " fail to get a dir output name 'tdir=$tdir'" >> $ERRMSG
221            echo " from tar file '$TEMPDIR/$mun'" >> $ERRMSG
222            mail -s 'parse_emails err_3b' $ADDRERR < $ERRMSG
223            rm -f $ERRMSG
224          fi
225          mv -f $INDIR"/"$file $INDIR"/../fail2process/"$file
226          continue
227        fi
228    
229        #-- select which Monthly Output Dir:
230        locDir=$OUTDIR
231        if test "x$monthDir" != x0 ; then
232          dd=`echo $tdir | sed 's/_/ /g' | awk '{ for(i=1;i<=NF;i++) print $i }'\
233                   | grep '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' | tail -1`
234          mn=`echo $dd | sed 's/..$//' | sed 's/..$/_&/'`
235          if test "x$mn" != "x$monthDir" ; then
236           if test "x$mn" = x ; then
237            if test $PRT = 2 ; then echo " cannot get month from '$tdir'" ; fi
238           else
239           #  could comment out this line:
240           #if test $PRT = 2 ; then echo " chg month: '$mn' for '$tdir'" ; fi
241            locDir="$BASEDIR/$mn"
242            if test ! -d $locDir ; then
243              if test $PRT = 2 ; then echo "NO DIR: '$locDir' => '$tdir' POSTPONED" ; fi
244              if test "x$ADDRERR" != x ; then
245                echo "parsing email error" > $ERRMSG
246                echo "no dir '$locDir' for outp. '$tdir'" > $ERRMSG
247                ls -l $INDIR"/"$file >> $ERRMSG
248                mail -s 'parse_emails err_4' $ADDRERR < $ERRMSG
249                rm -f $ERRMSG
250              fi
251              continue
252            fi
253           fi
254          fi
255        fi
256    
257      #  copy to $OUTDIR and rename if necessary      #-- copy to $locDir and rename if necessary
258      if test -e $OUTDIR"/"$tdir ; then      sdir=$tdir
259        if test -e $locDir"/"$tdir ; then
260          ad=0          ad=0
261          while test -e $OUTDIR"/"$tdir"_"$ad ; do          while test -e $locDir"/"$tdir"_"$ad ; do
262              ad=$(( $ad + 1 ))              ad=$(( $ad + 1 ))
263          done          done
264          mv $TEMPDIR"/"$tdir $OUTDIR"/"$tdir"_"$ad > /dev/null 2>&1          sdir=$tdir"_"$ad
265          chmod -R a+rx $OUTDIR"/"$tdir"_"$ad > /dev/null 2>&1      fi
266  #       gzip $OUTDIR"/"$tdir"_"$ad"/output.txt"      if test $PRT = 2 ; then
267      else        if test "x$locDir" = "x$OUTDIR"
268          mv $TEMPDIR"/"$tdir $OUTDIR > /dev/null 2>&1        then echo " '$sdir' ($file)"
269          chmod -R a+rx $OUTDIR"/"$tdir > /dev/null 2>&1        else echo " '$sdir' ($file) => '$locDir'"
270  #       gzip $OUTDIR"/"$tdir"/output.txt"        fi
271        fi
272        mv $TEMPDIR"/"$tdir $locDir"/"$sdir > /dev/null 2>&1
273        RETVAL=$?
274        if test "x$RETVAL" != x0 ; then
275          if test "x$ADDRERR" != x ; then
276            echo "parsing email error" > $ERRMSG
277            echo "mv $TEMPDIR/$tdir $locDir/$sdir returns error:" $RETVAL >> $ERRMSG
278            echo -n "in dir: $TEMPDIR : " ; ls -l $TEMPDIR  >> $ERRMSG
279            echo -n "in dir: $OUTDIR : " ; ls -l $locDir  >> $ERRMSG
280            mail -s 'parse_emails err_5' $ADDRERR < $ERRMSG
281            rm -f $ERRMSG
282          fi
283          mv -f $INDIR"/"$file $INDIR"/../fail2process/"$file
284          continue
285      fi      fi
286        chmod -R a+rx $locDir"/"$sdir > /dev/null 2>&1
287    
288      #  remove the original file      #-- remove the original file
289      rm -f $INDIR"/"$file      rm -f $INDIR"/"$file
290    
291  done  done
292  echo "  done"  if test $PRT = 1 ; then echo "  done" ; fi
   
 # echo -n "gzipping all the \"output.txt\" files ..."  
 # (  
 #     cd $OUTDIR  
 #     outp=`find . -name output.txt`  
 #     if test "x$outp" != x ; then  
 #       gzip $outp  
 #     fi  
 # )  
 # echo "  done"  
   
 # echo -n "setting permissions to world-readable ..."  
 # chmod -R a+rx $OUTDIR > /dev/null 2>&1  
 # echo "  done"  
   
293    

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.23

  ViewVC Help
Powered by ViewVC 1.1.22