~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
     This is not used and incomplete
     I wrote this before writing the python script
     Keeping this for reference purposes only

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


#!/bin/bash

## Remember to chmod this file, eg
##  chmod 755 /Users/rsaporta/git/misc/rscripts/utils/EmailStatus_Relay.sh


DEL_WHEN_DONE=true



## Directory where email messages will be (temporarily) kept
## If spaces or special characters, use single quotes
msgdir=/usr/local/etc/rboxrelay 
##    Make sure the directory exists and the R user has permissions
##    (We're not checking here for efficiency & permission issues)
##    use mkdir -p 

## full /path/to/alpine  (ie the output of which alpine )
ALPINE=/usr/local/bin/alpine

## full /path/to/rscript
RSCRIPT=/usr/bin/Rscript


# Time Stamp the file
basefile="msg_$(date +%Y%m%d_%H%M%S)".txt
msgfile="$msgdir/$basefile"
echo file is $msgfile

# Stall for one second, to ensure no duplicate time stamps
sleep 1

$ALPINE -I i,e,\"$msgfile\",CR,D,Q,Y

I i e "TEST"


if [ -f "$msgfile" ]
then
  ## Run the Rscript to parse the email and send an OS X notification
  echo "processing '$msgfile' "
  $RSCRIPT --no-restore -e '.us(notify.welcome=FALSE); f="'$msgfile'"; notifyParseRelayEmail(f)'

  ## If the script was successful remove the file
  if [ $? -eq 0 ] && [ $DEL_WHEN_DONE = true ]
  then
    rm $msgfile
  else
    mecho "Error for file $msgfile"
  fi   
## if there was no file, means there is no new messages. No action needed
# else
fi
 
# /Users/rsaporta/git/misc/rscripts/utils/EmailStatus_Relay.sh


## This is the rscript.  Once notify is in a package, we can use  
##            --default-packages=list('notify')
##  $RSCRIPT --no-restore -e '.us(notify.welcome=FALSE); f="'$msgfile'"; notifyParseRelayEmail(f)'


