#!/usr/bin/python

import smtplib
import glob
import os
import re
import struct
import sys
import time

def parseCommandline():
    '''Analyze path given on command line'''
    if( len( sys.argv ) > 1 ):
        outFolder = sys.argv[1]
    else:
        outFolder = "."
    return outFolder

def sendNotification( subject ):
    '''Sends the e-mail/text'''      
    sender = 'tethys@tellum.shore.mbari.org'
    receivers = []
    receivers.append( 'driftertrack@mbari.org' )
    receivers.append( 'godin@mbari.org' )
    
    message = msg = 'From: %s\nSubject: %s' % (sender,subject)
    
    try:
       smtpObj = smtplib.SMTP('localhost')
       smtpObj.sendmail(sender, receivers, message)         
       print "Successfully sent tethysCtr"
    except:
       print "Error: unable to send tethysCtr"
    

def main():
    '''The main program'''
    outFolder = parseCommandline()

    if os.path.exists(outFolder + "/shore.asc"):
        ascFile = open(outFolder + "/shore.asc", 'r')
        latitudePattern = re.compile('\d+\-\d+\-\d+T\d+:\d+:\d+.\d+Z,(\d+)\.\d+ [\w\d_]+\<\-\-[\w\d_]+\.[\w\d_]+Latitude\=(\-?\d+\.\d+)')
        longitudePattern = re.compile('\d+\-\d+\-\d+T\d+:\d+:\d+.\d+Z,(\d+)\.\d+ [\w\d_]+\<\-\-[\w\d_]+\.[\w\d_]+Longitude\=(\-?\d+\.\d+)')
        stringLon = "canon_front<--canon_front.FrontLongitude"
        line = 0
        lastLatitude = ''
        lastLatitudeLine = 0
        theList = [];
        for ascLine in ascFile.readlines():
            line = line + 1;
            latitudeMatches = latitudePattern.match( ascLine )
            if latitudeMatches:
                lastLatitude = latitudeMatches.group( 2 )
                lastLatitudeLine = line
            longitudeMatches = longitudePattern.match( ascLine )
            if longitudeMatches and line - 1 == lastLatitudeLine:
                listEntry = '%s,%s,%s,%s' %( 'tethysCtr', longitudeMatches.group( 1 ), longitudeMatches.group( 2 ), lastLatitude )
                theList.append( listEntry );
                print listEntry
                
        ascFile.close()
        
        missionCount = len(theList)
        print "count: %d" %missionCount
        previousCount = 0
        
        if os.path.exists(outFolder + "/tethysCtr.log"):
            f = open(outFolder + "/tethysCtr.log",'r')
            logFile = f.read()
            previousCount = int(logFile)        
            print "previous count: " + str(previousCount)
            f.close()
        else:
            logFile = open(outFolder + "/tethysCtr.log",'w')
            logFile.write( str(previousCount+1) )
            logFile.close()
     
        if( missionCount > previousCount):
            print "NO MATCH SEND tethysCtr"
            sendNotification(theList[previousCount])
            logFile = open(outFolder + "/tethysCtr.log",'w')
            logFile.write( str(previousCount+1) )
            logFile.close()        
        else:
              print "tethysCtr matches"                   
        

if __name__ == '__main__':

    main()
    
