#!/usr/bin/env python

import sys, os
import urlgrabber, urlgrabber.progress
import oam

def fetch_image_files(client, bbox, opts):
    # if opts.layer:
    #    path = str(opts.layer)
    #    if not opts.test and not os.path.isdir(path):
    #        os.makedirs(path)
    # else:
    files = []
    args = {"archive":"true"} if opts.source else {}
    for image in client.images_by_bbox(bbox, **args):
        target = image.path.split("/")[-1]
        if opts.dest:
            meter = urlgrabber.progress.text_progress_meter()
            target = os.path.join(opts.dest, target)
            print >>sys.stderr, image.path, "->", target
            urlgrabber.urlgrab(str(image.path), target, progress_obj=meter)
        else:
            print >>sys.stderr, image.path, "->", target
        files.append(target)
    return files

def run_gdalbuildvrt(files, opts):
    #glob = os.path.join(opts.path, "*.*")
    os.system("gdalbuildvrt %s %s" % (opts.vrt, " ".join(files)))

if __name__ == "__main__":
    parser = oam.option_parser("%prog [options] <west> <south> <east> <north>")
    parser.add_option("-s", "--source", dest="source", action="store_true", default=None, help="Include raw imagery sources")
    parser.add_option("-o", "--output", dest="vrt", default=None, help="Output VRT filename")
    parser.add_option("-d", "--destination", dest="dest", default=None, help="Destination directory for image downloads")
    opts, args = parser.parse_args()
    bbox = oam.parse_bbox(args)
    client = oam.build_client(opts)
    # oddly, we have to disable test mode for this script to do anything sensible.
    files = fetch_image_files(client, bbox, opts)
    if opts.vrt and files: run_gdalbuildvrt(files, opts)
