#!/usr/bin/python2

import os
import sys
import urllib2
import time
import HTMLParser
import json
import math
import zipfile
import tempfile

def getdata(url):
    #print url
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
    request = urllib2.Request(url, headers=headers)
    f = urllib2.urlopen(request)
    data = f.read()
    f.close()
    #print data
    return data.decode("utf-8")

def download(url, fname):
    response = urllib2.urlopen(url)
    info = response.info()
    if "Content-Length" in info:
        sz = int(info["Content-Length"])/1024
    else:
        sz = "(unknown)"
        
    f = open(fname, 'wb')
    count = 0
    while True:
        chunk = response.read(1024)
        if not chunk:
            break
        f.write(chunk)
        count +=1
        print "\r %d kb de %s" % (count,sz),
        sys.stdout.flush()
    f.close()
    response.close()
    
def writefile(pathname,data):
    f = open(pathname,"w")
    f.write(data)
    f.close()
    
def readfile(pathname):
    f = open(pathname,"r")
    data = f.read()
    f.close()
    return data


def main():
    gsbdir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0]))),"data","gsb")
    print "Download GSB files from OSGeo/proj-datumgrid"
    if not os.path.isdir(gsbdir):
        os.makedirs(gsbdir)
        
    zipfilename = os.path.join(tempfile.gettempdir(),"data.zip")   
    #print zipfilename
    if not os.path.exists(zipfilename):
        print "data.zip downloading"
        url = "https://github.com/OSGeo/proj-datumgrid/archive/refs/heads/master.zip"
        download(url,zipfilename)
    
    print "Extracting GSB files"
    zipf = zipfile.ZipFile(zipfilename, 'r')
    for pathname in zipf.namelist():
        if pathname.lower().endswith(".gsb"):
            fname = os.path.basename(pathname)
            print fname,
            data = zipf.read(pathname)
            targetfname = os.path.join(gsbdir,fname)
            if os.path.exists(targetfname):
                print " already exists",
            writefile(targetfname,data)
            print
    zipf.close()
    #os.remove(zipfilename)
    
if __name__ == "__main__":
    main()
    

# descarga https://github.com/OSGeo/proj-datumgrid/archive/refs/heads/master.zip en una carpeta temporal.
# copia los ficheros gsb a DATADIR/gsb
# Probablemente habria que hacerlo en python.