module.exports = function (grunt) {

    const refDataDir = process.env.REF_DATA_DIR || process.env.npm_package_config_refDataDir;

    const pgConfig = {
        user: process.env.PGUSER || process.env.npm_package_config_pgUser,
        dbName: process.env.PGDBNAME || process.env.npm_package_config_pgDbName || 'apicarto',
        password: process.env.PGPASSWORD || process.env.npm_package_config_pgPassword
    };

    const sourceFiles = {
        qp: refDataDir + '/qp-politiquedelaville-shp.zip'
    };

    const importableLayers = {
        qp: {
            dataSource: '/vsizip/qp-politiquedelaville-shp.zip',
            layerName: 'quartiers_prioritaires',
            append: true
        }
    };

    grunt.initConfig({
        shell: {
            wget: {
                command: (scope) => 'wget -N --quiet ' + sourceFiles[scope]
            },
            importpg: {
                command: (scope) => {
                    const config = importableLayers[scope];

                    const ogrOptions = [
                        '--config PG_USE_COPY YES',
                        '-f PGDump',
                        '/vsistdout',
                        '-lco SRID=4326',
                        '-lco GEOMETRY_NAME=geom',
                        '-lco DROP_TABLE=IF_EXISTS',
                        '-nlt PROMOTE_TO_MULTI',
                        '-nln ' + config.layerName
                    ];
                    if (config.append) ogrOptions.push('-append');

                    const psqlOptions = [
                        '-h localhost',
                        '-d ' + pgConfig.dbName
                    ];
                    if (pgConfig.user) psqlOptions.push('-U ' + pgConfig.user);

                    return 'ogr2ogr ' + ogrOptions.join(' ') + ' ' + config.dataSource + ' | psql ' + psqlOptions.join(' ') + ' -f -';
                },
                options: {
                    failOnError: false,
                    execOptions: {
                        cwd: 'data',
                        env: pgConfig.password ? { PGPASSWORD: pgConfig.password } : {}
                    }
                }
            },
            options: {
                execOptions: {
                    cwd: 'data'
                }
            }
        }
    });

    grunt.loadNpmTasks('grunt-shell');

    grunt.registerTask('import-qp', [
        'shell:wget:qp',
        'shell:importpg:qp'
    ]);

    grunt.registerTask('import', [
        'import-qp'
    ]);

};

@@ -495,16 +495,184 @@
 shp.zip'
+,%0A        'communes-ign-metrocorse': refDataDir + '/COMMUNE_PARCELLAIRE_METROCORSE.zip',%0A        'communes-ign-reunion': refDataDir + '/COMMUNE_PARCELLAIRE_REUNION.zip'
 %0A    %7D;%0A
@@ -856,16 +856,530 @@
 d: true%0A
+        %7D,%0A        'communes-ign-metrocorse': %7B%0A            dataSource: '/vsizip/COMMUNE_PARCELLAIRE_METROCORSE.zip',%0A            layerName: 'communes_ign',%0A            convertToWgs84: true,%0A            pgClientEncoding: 'LATIN1'%0A        %7D,%0A        'communes-ign-reunion': %7B%0A            dataSource: '/vsizip/COMMUNE_PARCELLAIRE_REUNION.zip',%0A            layerName: 'communes_ign',%0A            dropTable: 'NO',%0A            convertToWgs84: true,%0A            createTable: 'NO',%0A            pgClientEncoding: 'LATIN1'%0A
         
@@ -1968,18 +1968,122 @@
 BLE=
-IF_EXISTS'
+' + (config.dropTable %7C%7C 'IF_EXISTS'),%0A                        '-lco CREATE_TABLE=' + (config.createTable %7C%7C 'ON')
 ,%0A  
@@ -2269,16 +2269,100 @@
 ppend');
+%0A                    if (config.convertToWgs84) ogrOptions.push('-t_srs EPSG:4326');
 %0A%0A      
@@ -2585,24 +2585,279 @@
 fig.user);%0A%0A
+                    const psqlEnvVars = %5B%5D;%0A                    if (pgConfig.password) psqlEnvVars.push('PGPASSWORD=' + pgConfig.password);%0A                    if (config.pgClientEncoding) psqlEnvVars.push('PGCLIENTENCODING=' + config.pgClientEncoding);%0A%0A
             
@@ -2936,16 +2936,46 @@
 ce + ' %7C
+ ' + psqlEnvVars.join(' ') + '
  psql ' 
@@ -3097,191 +3097,8 @@
 alse
-,%0A                    execOptions: %7B%0A                        cwd: 'data',%0A                        env: pgConfig.password ? %7B PGPASSWORD: pgConfig.password %7D : %7B%7D%0A                    %7D
 %0A   
@@ -3438,31 +3438,304 @@
 port
-', %5B%0A        'import-qp
+-communes-ign', %5B%0A        'shell:wget:communes-ign-metrocorse',%0A        'shell:wget:communes-ign-reunion',%0A        'shell:importpg:communes-ign-metrocorse',%0A        'shell:importpg:communes-ign-reunion'%0A    %5D);%0A%0A    grunt.registerTask('import', %5B%0A        'import-qp',%0A        'import-communes-ign
 '%0A  

Add import tasks for communes-ign