#!/usr/bin/python
# -*- coding: utf-8 -*-
#

import urllib2
import os
import re
import commands
import datetime

def tstotime(lastime):
    """ return datetime from liste """
    return datetime.datetime(lastime[0],lastime[1],lastime[2],lastime[3],lastime[4],lastime[5])

def get_state(statex):
    """ get and valid the state.txt file from planet.openstreermap.org"""
    req = urllib2.Request(statex)
    req.add_header("User-Agent", "http://osmose.openstreetmap.fr")
    try:
        handle =  urllib2.urlopen(req)
    except urllib2.HTTPError:
        return None
    else:
        sr_seq = sr_time = None
        answer = handle.read()
        for ligne in answer.split('\n'):
            mat1=re.match("sequenceNumber=(?P<SEQUENCE>[0-9]+)", ligne.strip())
            if mat1:     
                sr_seq=int(mat1.group('SEQUENCE'))
                break
            
        for ligne in answer.split('\n'):
            mat = re.match('timestamp=(?P<YEAR>[0-9]{4})-(?P<MONTH>[0-9]{2})-(?P<DAY>[0-9]{2})T(?P<HOUR>[0-9]{2})\\\:(?P<MIN>[0-9]{2})\\\:(?P<SEC>[0-9]{2})Z',ligne.strip())
            if mat:
                sr_time=(int(mat.group('YEAR')), int(mat.group('MONTH')), int(mat.group('DAY')), int(mat.group('HOUR')), int(mat.group('MIN')), int(mat.group('SEC')))
                break
            
        if (sr_seq==None) or (sr_time==None):
            return None
        else:
            return answer, sr_seq, tstotime(sr_time)

def seq_to_url(seq):
    """ sequence format for url """
    if type(seq) !=int:
        seq2 = seq.replace(' ','').replace('/','')
        fi='%09d' % (int(seq2))
    else:
        fi='%09d' % (seq)
    return fi[0:3]+'/'+fi[3:6]+'/'+fi[6:9]
        
def lastdate_in_osm(osmconvert_path, fichier):
    """timestamp of a file with osmconvert  """
    last_time = None
    res = commands.getstatusoutput(('%s/osmconvert %s --out-timestamp') %(osmconvert_path, fichier))
    if res[0]:
        #2011-08-01T23:50:00Z
        mat = re.match('(?P<YEAR>[0-9]{4})-(?P<MONTH>[0-9]{2})-(?P<DAY>[0-9]{2})T(?P<HOUR>[0-9]{2})\:(?P<MIN>[0-9]{2})\:(?P<SEC>[0-9]{2})Z', res[1])
        if mat:
            last_time = (int(mat.group('YEAR')), int(mat.group('MONTH')), int(mat.group('DAY')), int(mat.group('HOUR')), int(mat.group('MIN')), int(mat.group('SEC')))
    else:
        res2 = commands.getstatusoutput(('%s/osmconvert %s --out-statistics') %(osmconvert_path, fichier))
        if not res2[0]:
            for ligne in res2[1].split('\n'):
                #timestamp max: 2011-07-31T19:59:46Z
                mat2 = re.match('timestamp max\: (?P<YEAR>[0-9]{4})-(?P<MONTH>[0-9]{2})-(?P<DAY>[0-9]{2})T(?P<HOUR>[0-9]{2})\:(?P<MIN>[0-9]{2})\:(?P<SEC>[0-9]{2})Z', ligne.strip())
                if mat2:
                    last_time = (int(mat2.group('YEAR')), int(mat2.group('MONTH')), int(mat2.group('DAY')), int(mat2.group('HOUR')), int(mat2.group('MIN')), int(mat2.group('SEC')))
                    break
    if last_time:
        return tstotime(last_time)
    else:
        return None
    
################################################################################
if __name__ == "__main__":
    import time
        
    osmconvert_path = "/home/osma/osmose/osmose-backend3/osmconvert"
    fichier = "/home/osma/osmose/data/extracts/france_guadeloupe.osm.pbf"
    fichier = "/home/osma/osmose/data/extracts/france_guadeloupe.osm"
    fichier = "/home/osma/osmose/data/extracts/france_champagne_ardenne.osm"    
    fichier = "/home/osma/osmose/data/extracts/champagne-ardenne.osm.pbf"        

  
    url_replication_m = "http://planet.openstreetmap.org/replication/minute/"
    url_replication_h = "http://planet.openstreetmap.org/replication/hour/"  
    url_replication_d = "http://planet.openstreetmap.org/replication/day/"  
    rep_periode_m = 60
    rep_periode_h= 3600
    rep_periode_d= 86400
    
    
    url_replication = url_replication_m
    rep_periode = rep_periode_m
    
#    1- definir temps du fichier osm ou pbf
    dtosm = lastdate_in_osm(osmconvert_path, fichier)
    if dtosm is None:
        print "fichier %s n'a pas de ts !" % fichier
    else:
        print 'ts du fichier osm' , dtosm

    #   2- lire le dernier timestamp sur planet ou ...:
        #(p_bool, p_state, p_seq , p_ts) = get_state(url_replication+"state.txt")
        resuget = get_state(url_replication+"state.txt")
        if resuget is None:
            print "pas de ts sur " + url_replication
        else:
            (p_state, p_seq , p_ts) = resuget
            print 'ts sur planet' , p_ts
            #   3- calculer l'ecart en seconde
            if not (p_ts > dtosm):
                print 'erreur: fichier osm plus recent que %s 000/000/state.txt...' % url_replication
            else:
                dtt1 = time.mktime(dtosm.timetuple())
                dtt2 = time.mktime(p_ts.timetuple())       
                #print "soit une différence de : %s minutes ou %s  heure(s)" % ((dtt2 - dtt1)/60 , ((dtt2 - dtt1)/3600) )
                print "soit un retard probable de : %s " % ( dtosm - p_ts )
                nxseq = seq_to_url(p_seq-(int((dtt2 - dtt1)/rep_periode) + 1)) + ".state.txt"                            
                resuget2 = get_state(url_replication + nxseq)
                if resuget2 is None:
                    print "pas de ts sur " + url_replication + nxseq
                else:
                    (tsm_state, tsm_seq , tsm_trouver) = resuget2
                    if tsm_trouver < p_ts:
                        print "la sequence est : " 
                        print tsm_state
                        flast = open('state.txt','w')
                        flast.write(tsm_state)
                        flast.close()
                    else:                        
                        print "revoir algo"
                        
                # variante nxseq ?: marge de 5 a la place de 1, puis boucle jusqu'a ce que le ts trouve soit superieur => ts = ts -1
                #nxseq = seq_to_url(p_seq-(int((dtt2 - dtt1)/rep_periode) + 5)) + ".state.txt"             
                
    
