#!/usr/bin/env node

var tesselCanvas = require('tessel-canvas'),
    tessel = require('tessel-map'),
    fs = require('fs'),
    argv = require('optimist')
        .default('lon', 0)
        .default('lat', 0)
        .default('z', 2)
        .default('w', 500)
        .default('id', 'tmcw.map-1rgcfrpf')
        .default('h', 500)
        .default('output', 'map.png')
        .argv;

tesselCanvas(tessel.map()
    .dimensions([argv.w, argv.h])
    .center([parseFloat(argv.lon), parseFloat(argv.lat)])
    .template('http://a.tiles.mapbox.com/v3/' + argv.id + '/{z}/{x}/{y}.png')
    .zoom(argv.z)
    .state(), null, function(err, canvas) {
        fs.writeFileSync(argv.output, canvas.toBuffer());
    });
