/* global module */

module.exports = function (grunt) {
  'use strict';

  var sources, geojsVersion, vgl, geo, port, sourceList, templateData;

  geojsVersion = '0.1.0';
  sources = grunt.file.readJSON('sources.json');
  port = Number(grunt.option('port') || '8082');

  function moduleFiles(module) {
    var obj, files;

    obj = sources.modules[module];

    files = (obj.files || []).map(function (f) {
      if (obj.prefix) {
        f = obj.prefix + '/' + f;
      }
      return f;
    });

    return files;
  }

  vgl = moduleFiles('vgl');
  geo = {
    core: moduleFiles('geo.core'),
    gl: moduleFiles('geo.gl'),
    d3: moduleFiles('geo.d3')
  };

  sourceList = Array.prototype.concat(
    vgl.map(function (f) { return 'src/vgl/' + f; }),
    geo.core,
    geo.gl,
    geo.d3
  );

  templateData = {
    GEOJS_Version: geojsVersion,
    SOURCES_JSON: JSON.stringify(sourceList),
    SOURCES_ROOT: '/',
    BUNDLE_EXT: 'built/geojs.ext.min.js',
    BUNDLE_GEO: 'built/geojs.min.js'
  };

  grunt.config.init({
    pkg: grunt.file.readJSON('package.json'),

    template: {
      version: {
        options: {
          data: templateData
        },
        files: { 'src/core/version.js': 'src/core/version.js.in' }
      },
      loadDev: {
        options: {
          data: templateData
        },
        files: { 'dist/built/geo.all.dev.js': 'src/geo.all.dev.js.in' }
      },
      loadAll: {
        options: {
          data: templateData
        },
        files: { 'dist/built/geo.all.js': 'src/geo.all.js.in' }
      }
    },

    copy: {
      geo: {
        files: [
          {
            src: Array.prototype.concat(geo.core, geo.gl, geo.d3),
            dest: 'dist/',
            filter: 'isFile',
            flatten: false
          }
        ]
      },
      vgl: {
        files: [
          {
            src: vgl,
            dest: 'dist/src/vgl/',
            filter: 'isFile',
            flatten: true
          }
        ]
      }
    },

    uglify: {
      options: {
        sourceMap: true,
        sourceMapIncludeSources: true,
        report: 'min',
        beautify: {
          ascii_only: true,
          beautify: true
        },
        mangle: false
      },

      geojs: {
        files: {
          'dist/built/geojs.min.js': sourceList.map(function (f) {
            return 'dist/' + f;
          })
        }
      },

      ext: {
        files: {
          'dist/built/geojs.ext.min.js': [
            'node_modules/jquery-browser/lib/jquery.js',
            'node_modules/gl-matrix/dist/gl-matrix.js',
            'node_modules/proj4/dist/proj4-src.js',
            'node_modules/d3-browser/lib/d3.js'
          ]
        }
      }
    },

    watch: {
      uglify: {
        files: [
          'src/**/*.js',
          'vgl/src/**/*.js',
          'src/core/version.js.in',
          'Gruntfile.js',
          'sources.json'
        ],
        tasks: ['clean:source', 'template', 'copy', 'uglify:geojs']
      }
    },

    clean: {
      source: [ 'dist/src', 'src/core/version.js' ],
      all: [ 'dist', 'src/core/version.js' ]
    },

    express: {
      server: {
        options: {
          port: port,
          bases: ['dist']
        }
      }
    }
  });

  grunt.loadNpmTasks('grunt-template');
  grunt.loadNpmTasks('grunt-contrib-copy');
  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-contrib-clean');
  grunt.loadNpmTasks('grunt-express');

  grunt.registerTask('default', [
    'template',
    'copy',
    'uglify'
  ]);

  grunt.registerTask(
      'serve',
      'Serve the content at http://localhost:8082, ' +
      'use the --port option to override the default port',
      ['express', 'watch']
  );
};

@@ -136,18 +136,68 @@
 lateData
+, pkg;%0A%0A  pkg = grunt.file.readJSON('package.json')
 ;
-%0A
 %0A  geojs
@@ -210,15 +210,19 @@
 n = 
-'0.1.0'
+pkg.version
 ;%0A  
@@ -877,22 +877,22 @@
  GEOJS_V
-ersion
+ERSION
 : geojsV
@@ -986,34 +986,32 @@
 _EXT: 'built/geo
-js
 .ext.min.js',%0A  
@@ -1026,34 +1026,32 @@
 _GEO: 'built/geo
-js
 .min.js'%0A  %7D;%0A%0A 
@@ -1084,43 +1084,11 @@
 kg: 
-grunt.file.readJSON('package.json')
+pkg
 ,%0A%0A 
@@ -2287,18 +2287,16 @@
 uilt/geo
-js
 .min.js'
@@ -2447,18 +2447,16 @@
 uilt/geo
-js
 .ext.min

rename geojs -> geo