/* 
* post build tasks
*
* contains tasks that are not required for build, but required for go-live
* eg common replacement tasks
* */

var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var path = require('path');
var replace = require('gulp-replace-task');
var fs = require('fs');


var config = {
  applications: 'applications',
  elements: 'elements',
  dependencies: 'bower_components',
  resources: 'resources',
  demo: 'elements/demo'
};

var gaConfig = {
  id : 'UA-4365437-1',
  url : 'www.library.uq.edu.au',
  domain : 'library.uq.edu.au'
}

// inject preloader.html code into html pages
gulp.task('inject-preloader', function() {

  var regEx = new RegExp("#preloader#", "g");
  var browserUpdate = fs.readFileSync("app/bower_components/uqlibrary-browser-supported/preloader.html", "utf8");

  return gulp.src(dist('*'))
      .pipe(replace({patterns: [{ match: regEx, replacement: browserUpdate}], usePrefix: false}))
      .pipe(gulp.dest(dist()))
      .pipe($.size({title: 'inject-preloader'}));
});

// inject browser-update.js code into html pages
gulp.task('inject-browser-update', function() {

  var regEx = new RegExp("//bower_components/uqlibrary-browser-supported/browser-update.js", "g");
  var browserUpdate=fs.readFileSync("bower_components/uqlibrary-browser-supported/browser-update.js", "utf8");

  return gulp.src(config.resources + '/**/*.js')
      .pipe(replace({patterns: [{ match: regEx, replacement: browserUpdate}], usePrefix: false}))
      .pipe(gulp.dest(config.resources))
      .pipe($.size({title: 'inject-browser-update'}));
});

// inject values for GA
gulp.task('inject-ga-values', function() {

  if (process.env.CI_BRANCH !== "production")
    return;

  var gaIdEx = new RegExp("<GA-TRACKING-ID>", "g");
  var gaUrlEx = new RegExp("<GA-WEBSITE-URL>", "g");
  var gaDomainEx = new RegExp("<GA-COOKIE-DOMAIN>", "g");

  return gulp.src(config.elements + '/elements*.js')
      .pipe(replace({patterns: [{ match: gaIdEx, replacement: gaConfig.id}], usePrefix: false}))
      .pipe(replace({patterns: [{ match: gaUrlEx, replacement: gaConfig.url}], usePrefix: false}))
      .pipe(replace({patterns: [{ match: gaDomainEx, replacement: gaConfig.domain}], usePrefix: false}))
      .pipe(gulp.dest(config.elements))
      .pipe($.size({title: 'inject-ga-values'}));
});

//optimize css and js of application specific files
gulp.task('optimize', function () {
  gulp.src(config.applications + '/**/*')
      .pipe($.if('*.css',$.cssmin())) // Minify css output
      .pipe($.if('*.js',$.uglify({preserveComments: 'some'}))) // Minify js output
      .pipe(gulp.dest(config.applications));
});
@@ -563,16 +563,56 @@
 .au'%0A%7D%0A%0A
+var gtmConfig = %7B%0A  id: 'GTM-PX9H7R'%0A%7D%0A%0A
 // injec
@@ -1913,16 +1913,71 @@
 %22, %22g%22);
+%0A  var gtmIdEx = new RegExp(%22%3CGTM-CONTAINER-ID%3E%22, %22g%22);
 %0A%0A  retu
@@ -2316,32 +2316,131 @@
 refix: false%7D))%0A
+      .pipe(replace(%7Bpatterns: %5B%7B match: gtmIdEx, replacement: gtmConfig.id%7D%5D, usePrefix: false%7D))%0A
       .pipe(gulp

add gtm config