text: 'use strict';
  // gulp
  var gulp = require('gulp');
  var paths = gulp.paths;
  var options = gulp.options;
  // plugins
  var $ = require('gulp-load-plugins')();
  // modules
  var wiredep = require('wiredep');
  var mainBowerFiles = require('main-bower-files');
  
  // inject www/**/*.js, bower components, css into index.html
  // inject environment variables into config.js constant
  gulp.task('inject-all', ['styles', 'wiredep', 'bower-fonts', 'environment', 'build-vars'], function () {
  
    return gulp.src('www/index.html')
      .pipe(
        $.inject( // www/**/*.js files
          gulp.src(paths.jsFiles)
            .pipe($.plumber()) // use plumber so watch can start despite js errors
            .pipe($.naturalSort())
            .pipe($.angularFilesort()),
          {relative: true}))
      .pipe(
        $.inject( // inject compiled css
          gulp.src('.tmp/*/styles/*.css', {read: false})
            .pipe($.naturalSort()),
          {
            ignorePath: '../.tmp',
            relative: true,
          }))
      .pipe(gulp.dest('www'));
  });
  
  // build styles to tmp
  gulp.task('styles', ['clean'], function () {
  
    // compile css starting from each module's scss
    return gulp.src('www/*/styles/!(_)*.scss')
      .pipe($.plumber())
      .pipe($.sourcemaps.init())
      .pipe($.sass.sync().on('error', $.sass.logError))
      .pipe($.sourcemaps.write())
      .pipe($.autoprefixer({ browsers: ['last 2 versions'], remove: false}))
      .pipe(gulp.dest('.tmp/'));
  });
  
  // inject bower components into index.html
  gulp.task('wiredep', function () {
  
    //return gulp.src('www/index.html')
    //  // we're not excluding the ionic css here
    //  .pipe(wiredep.stream())
    //  .pipe(gulp.dest('www/'));
  })
  ;
  
  // copy bower fonts
  gulp.task('bower-fonts', ['clean'], function () {
    // to do www/fonts (ionic css requires it to be in this folder)
    var DEST = 'www/fonts';
    var fontFiles = mainBowerFiles({filter: /\.(eot|otf|svg|ttf|woff|woff2)$/i});
  
    return gulp.src(fontFiles)
      .pipe($.changed(DEST))
      .pipe(gulp.dest(DEST));
  });
  
  /**
   * transforms object into a string format that is ready for injection at indentation 4
   * @param  {Object} obj Object with values to inject
   * @return {String}     properly formatted string
   */
  var injectFormat = function (obj) {
    // indentation of 2
    obj = JSON.stringify(obj, null, 2);
    // replace all doublequotes with singlequotes
    obj = obj.replace(/\"/g, '\'');
    // remove first and last line curly braces
    obj = obj.replace(/^\{\n/, '').replace(/\n\}$/, '');
    // remove indentation of first line
    obj = obj.replace(/^( ){2}/, '');
    // insert padding for all remaining lines
    obj = obj.replace(/\n( ){2}/g, '\n    ');
  
    return obj;
  };
  
  gulp.task('environment', function () {
    return gulp.src('www/*/constants/*config-const.js')
      .pipe(
        $.inject(
          gulp.src('www/main/constants/env-' + options.env + '.json'),
          {
            starttag: '/*inject-env*/',
            endtag: '/*endinject*/',
            transform: function (filePath, file) {
              var json;
              try {
                json = JSON.parse(file.contents.toString('utf8'));
              }
              catch (e) {
                console.log(e);
              }
  
              if (json) {
                json = injectFormat(json);
              }
              return json;
            }
          }))
      .pipe(gulp.dest('www/'));
  });
  
  gulp.task('build-vars', ['environment'], function () {
    return gulp.src('www/*/constants/*config-const.js')
      .pipe(
        $.inject(
          gulp.src(''),
          {
            starttag: '/*inject-build*/',
            endtag: '/*endinject*/',
            transform: function () {
              var obj = {};
              var buildVars = options.buildVars;
  
              if (buildVars) {
                // loop over build variables
                var variables = buildVars.split(',');
                for (var i = 0, variable; ((variable = variables[i])); i++) {
                  var splits = variable.split(':');
                  // add key and value to object
                  obj[splits[0]] = splits[1];
                }
                return injectFormat(obj);
              }
              else {
                return;
              }
            }
          }))
      .pipe(gulp.dest('www/'));
  });
  