module.exports = function(grunt) {



    var globalConfig = {
        // Adjust this value to the assets destination path of your cms
        cms: 'cms'
    };



    grunt.initConfig({
        globalConfig: globalConfig,

        // Shell
        shell: {
            // Generates only the patterns.
            patternlab_patternsonly: {
                command: 'php core/builder.php -gp'
            },
            // Regenerates whole public dir.
            patternlab_generate: {
                command: 'php core/builder.php -g'
            }
        },

        // Connect
        connect: {
            options: {
                port: 8000,
                hostname: '0.0.0.0',
                livereload: 35729
            },
            livereload: {
                options: {
                    open: true,
                    base: [
                        './public'
                    ]
                }
            },
            server: {
                options: {
                    base: './public'
                }
            }
        },

        // Watch
        watch: {
            scss: {
                files: ['source/css/**/{.*,*,*/*}'],
                tasks: 'scss'
            },
            js: {
                files: ['source/js/**/{.*,*,*/*}'],
                tasks: 'js'
            },
            img: {
                files: ['source/images/**/{.*,*,*/*}'],
                tasks: ['copy:img_public', 'copy:img_cms']
            },
            fonts: {
                files: ['source/fonts/**/{.*,*,*/*}'],
                tasks: ['copy:fonts_public', 'copy:fonts_cms']
            },
            html: {
                files: [
                    'source/_patterns/**/*.mustache',
                    'source/**/*.json'
                ],
                tasks: [
                    'shell:patternlab_patternsonly'
                ],
                options: {
                    spawn: false
                }
            },
            livereload: {
                options: {
                    livereload: 35729
                },
                files: [
                    'public/css/style.css',
                    'public/patterns/**/*',
                    'public/js/**/*'
                ]
            }
        },

        // Concurrent
        concurrent: {
            dev: [
                'sass'
            ]
        },

        // Sass
        sass: {
            options: {
                require: 'sass-globbing',
                loadPath: 'bower_components',
                sourcemap: 'none'
            },
            dev: {
                options: {
                    style: 'expanded',
                    lineNumbers: true
                },
                files: {
                    'public/css/style.css': 'source/css/style.scss'
                }
            },
            cms: {
                options: {
                    style: 'compressed'
                },
                files: {
                    '<%= globalConfig.cms %>/css/style.css': 'source/css/style.scss'
                }
            }
        },

        // Autoprefixer
        autoprefixer: {
            options: {
                browsers: [
                    'last 3 version',
                    'ie 8',
                    'ie 9',
                    'ie 10'
                ]
            },
            cms: {
                src: '<%= globalConfig.cms %>/css/style.css'
            }
        },

        // Copy
        copy: {
            img_public: {
                files: [
                    {
                        expand: true,
                        cwd: 'source/images/',
                        src: ['./**/*.*'],
                        dest: 'public/images/'
                    }
                ]
            },
            // In the cms you don't want to have dummy images.
            img_cms: {
                files: [
                    {
                        expand: true,
                        cwd: 'source/images/ui/',
                        src: ['./**/*.*'],
                        dest: '<%= globalConfig.cms %>/images/ui/'
                    }
                ]
            },
            fonts_public: {
                files: [
                    {
                        expand: true,
                        cwd: 'source/fonts/',
                        src: ['./**/*.*'],
                        dest: 'public/fonts/'
                    }
                ]
            },
            fonts_cms: {
                files: [
                    {
                        expand: true,
                        cwd: 'source/fonts/',
                        src: ['./**/*.*'],
                        dest: '<%= globalConfig.cms %>/fonts/'
                    }
                ]
            },
            js_public: {
                files: [
                    {
                        expand: true,
                        cwd: 'source/js/',
                        src: ['script.js'],
                        dest: 'public/js/'
                    }
                ]
            }
        },

        // Concat
        concat: {
            options: {
                separator: ' ',
            },
            plugins: {
                src: ['source/js/plugins/*.js'],
                dest: 'public/js/plugins.js',
            },
            cms: {
                src: ['source/js/plugins/*.js', 'source/js/script.js'],
                dest: '<%= globalConfig.cms %>/js/script.js',
            }
        },

        // Uglifiy
        uglify: {
            cms: {
                files: {
                    '<%= globalConfig.cms %>/js/script.js': '<%= globalConfig.cms %>/js/script.js'
                }
            }
        },

        // Clean
        clean: {
            cms: {
                src: [
                    '<%= globalConfig.cms %>/css/',
                    '<%= globalConfig.cms %>/images/',
                    '<%= globalConfig.cms %>/js/',
                    '<%= globalConfig.cms %>/fonts/'
                ]
            }
        }

    });

    grunt.loadNpmTasks('grunt-autoprefixer');
    grunt.loadNpmTasks('grunt-concurrent');
    grunt.loadNpmTasks('grunt-contrib-clean');
    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-contrib-connect');
    grunt.loadNpmTasks('grunt-contrib-copy');
    grunt.loadNpmTasks('grunt-contrib-sass');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-shell');

    grunt.registerTask('scss', [
        'sass',
        'autoprefixer'
    ]);

    grunt.registerTask('js', [
        'copy:js_public',
        'concat',
        'uglify'
    ]);

    grunt.registerTask('default', [
        'shell:patternlab_generate',
        'clean',
        'concat',
        'copy',
        'uglify',
        'shell:patternlab_patternsonly',
        'concurrent',
        'connect:server',
        'watch'
    ]);

};

@@ -532,16 +532,163 @@
 php -g'%0A
+            %7D,%0A            // Starts the pattern lab watcher.%0A            patternlab_watch: %7B%0A                command: 'php core/builder.php -wrp'%0A
         
@@ -1343,22 +1343,40 @@
  tasks: 
+%5B
 's
-c
+a
 ss'
+, 'autoprefixer'%5D
 %0A       
@@ -1476,20 +1476,54 @@
  tasks: 
-'js'
+%5B'copy:js_public', 'concat', 'uglify'%5D
 %0A       
@@ -3560,32 +3560,110 @@
 %0A            %7D,%0A
+            dev: %7B%0A                src: 'public/css/style.css'%0A            %7D,%0A
             cms:
@@ -6376,24 +6376,26 @@
 %7D%0A%0A    %7D);%0A%0A
+%0A%0A
     grunt.lo
@@ -6847,189 +6847,30 @@
 );%0A%0A
-    grunt.registerTask('scss', %5B%0A        'sass',%0A        'autoprefixer'%0A    %5D);%0A%0A    grunt.registerTask('js', %5B%0A        'copy:js_public',%0A        'concat',%0A        'uglify'%0A    %5D);%0A
+%0A%0A    // Default task.
 %0A   
@@ -7122,11 +7122,310 @@
    %5D);%0A%0A
+    // Task for working with the patterns (reloads the site quicker).%0A    grunt.registerTask('patterns', %5B%0A        'shell:patternlab_generate',%0A        'clean',%0A        'concat',%0A        'copy',%0A        'uglify',%0A        'sass',%0A        'connect:server',%0A        'shell:patternlab_watch'%0A    %5D);%0A%0A%0A%0A
 %7D;%0A

add extra task for working with patterns