#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Project: Update and crop osm dump file for Europe
# Author: Artem Svetlov <artem.svetlov@nextgis.com>



import os
import config
import argparse

def argparser_prepare():

    class PrettyFormatter(argparse.ArgumentDefaultsHelpFormatter,
        argparse.RawDescriptionHelpFormatter):

        max_help_position = 35

    parser = argparse.ArgumentParser(description='',
            formatter_class=PrettyFormatter)
    parser.add_argument('-u', '--update', type=str, choices=['day', 'hour', 'minute'], default='day',
                        help='command for osmupdate')

    parser.epilog = \
        '''Samples:
%(prog)s
''' \
        % {'prog': parser.prog}
    return parser

#if prevdump not exists - download CFO from geofabrik and crop to Europe
def updateDump(update = 'day',
               dump_url = 'http://download.geofabrik.de/europe/estonia-latest.osm.pbf',
               poly_url = 'http://download.geofabrik.de/europe/estonia.poly',
               work_dump = 'dump.osm.pbf',
               poly_file = 'bounds.poly'):
    

    
    poly_file = poly_url.split('/')[-1]
    downloaded_dump=dump_url.split('/')[-1]
    updated_dump='osm/just_updated_dump.osm.pbf'

    tempdirectory='osm'   
    if not os.path.exists(tempdirectory):
        os.makedirs(tempdirectory)

    #frist run of program
    if os.path.exists(work_dump) == False:
        os.system('wget  ' + dump_url)
        os.rename(downloaded_dump, work_dump) 
        
    os.system('wget  --timestamping '+poly_url)

    #if prevdump dump exists - run osmupdate, it updating it to last hour state with clipping, and save as currentdump
    cmd = 'osmupdate {work_dump}  {updated_dump} --{update}   -v --keep-tempfiles -B={poly_file}'.format(
        work_dump=work_dump,
        updated_dump=updated_dump,
        update=update,
        poly_file=poly_file)
    print cmd
    os.system(cmd)
    
    #if osmupdate worked good, it create updated_dump file. Work dump will replaced by it
    if os.path.exists(updated_dump) == True: 
        os.remove(work_dump)
        os.rename(updated_dump, work_dump)
    else:
        #osmupdate found your file is already up-to-date
        pass
    
    os.rmdir(tempdirectory)

    return 0
    

    
def filter_osm_dump_osmosis(work_dump='dump.osm.pbf',file_result='routesFinal.osm.pbf'):
        import json
        import pprint
        pp=pprint.PrettyPrinter(indent=2)

        refs=[]

        file_src = work_dump
        file_temp_1 = 'routes_temp.osm.pbf'
        file_result = 'routesFinal.osm.pbf'
        
        print 'Filter step 1'
        cmd='''
~/osmosis/bin/osmosis \
  -q \
  --read-pbf {file_src} \
  --tf accept-relations route=tram \
  --used-way --used-node \
  --write-pbf {file_temp_1}
'''.format(file_src=file_src,file_temp_1=file_temp_1)
        os.system(cmd)

        print 'Filter step 3'
        cmd='''
~/osmosis/bin/osmosis \
  -q \
  --read-pbf {file_temp_1} \
  --tf accept-relations "type=route" \
  --used-way --used-node \
  --write-pbf {file_result}
    '''
        cmd = cmd.format(file_temp_1=file_temp_1,file_result=file_result)
        os.system(cmd)
        os.remove(file_temp_1)
        
        
def filter_osm_dump(work_dump='dump.osm.pbf',file_result='routesFinal.osm.pbf'):
        import json
        import pprint
        pp=pprint.PrettyPrinter(indent=2)

        refs=[]

        file_src = work_dump
        file_temp_1 = 'routes_temp.osm.pbf'
        file_result = 'routesFinal.osm.pbf'
        
        o5m='temp.o5m'
        print 'Filter step 1'
        
        cmd='osmconvert {work_dump} -o={o5m}'.format(work_dump=work_dump,o5m=o5m)
        os.system(cmd)
        print 'Filter step 2' 
        cmd='osmfilter {o5m} --keep="route=tram" >temp2.o5m'.format(o5m=o5m)
        os.system(cmd)   
        print 'Filter step 3' 
        cmd='osmconvert temp2.o5m -o={file_result}'.format(file_result=file_result)
        os.system(cmd)
        
        os.remove(o5m)
        os.remove('temp2.o5m')
        
        

def importdb(host,database,username,password,filename='routesFinal.osm.pbf'):
    os.system('export PGPASS='+password)
    cmd = 'osm2pgsql --create --slim -E 3857 --cache-strategy sparse --cache 100 --host {host} --database {database} --username {username} {filename}'.format(host=host,
    database=database,username=username,password=password,filename=filename)
    print cmd
    os.system(cmd)        

def process(host,dbname,user,password):
    
    cmd = 'python ../../osmot.py -hs {host} -d {dbname} -u {user} -p {password}'
    cmd = cmd.format(
            host=host,
            dbname=dbname,
            user=user,
            password=password)

    os.system(cmd)
        
        
if __name__ == '__main__':
    host=config.host
    dbname=config.dbname
    user=config.user
    password=config.password

        
    parser = argparser_prepare()
    args = parser.parse_args()

    update = args.update
    
    updateDump(update,poly_file='europe.poly',dump_url=config.dump_url,poly_url=config.poly_url)
    filter_osm_dump(work_dump='dump.osm.pbf',file_result='routesFinal.osm.pbf')
    
    
    importdb(host,dbname,user,password)
    process(host,dbname,user,password) 

@@ -547,24 +547,9 @@
 e'%5D,
- default='day',
 %0A
+
     
@@ -804,21 +804,20 @@
 pdate = 
-'day'
+None
 ,%0A      
@@ -1063,25 +1063,124 @@
 oly'):%0A    %0A
+    if update is None:%0A        update_command = ''%0A    else:%0A        update_command = '--' + update
 %0A
-
     %0A    pol
@@ -1801,16 +1801,24 @@
 -%7Bupdate
+_command
 %7D   -v -
@@ -1935,23 +1935,39 @@
   update
-=update
+_command=update_command
 ,%0A      
@@ -5042,16 +5042,85 @@
 args()%0A%0A
+    %0A    if args.update is none:%0A        update = None%0A    else:%0A    
     upda

Update main.py