// Load packages
var saveJSON = require('jsonfile')
saveJSON.spaces = 2
var path = require('path')
var utils = require('./utils')
var webpack = require('webpack')
var list = require('list-dir')
var merge = require('webpack-merge')
var isThere = require('is-there')
var deleteFiles = require('delete')
var baseWebpackConfig = require('./webpack.base.conf')
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var HtmlWebpackPlugin = require('html-webpack-plugin')
var AppCachePlugin = require('appcache-webpack-plugin')
var OnBuildPlugin = require('on-build-webpack')
var replace = require('replace-in-file')
var copy = require('cpx')

// Load configuration
var cfg = require('./config.js')
var pkg = require(cfg.packageRoot + 'package.json')

// Update copyright year in license
replace.sync({
  files: path.resolve(__dirname, '../LICENSE'),
  replace: /Copyright \(c\) ([0-9]{4}) scriptPilot/,
  with: 'Copyright (c) ' + (new Date()).getFullYear() + ' scriptPilot'
})

// Update version in demo app
if (!cfg.isInstalled) {
  var demoApp = require(cfg.appRoot + 'package.json')
  demoApp.version = pkg.version
  saveJSON.writeFileSync(cfg.appRoot + 'package.json', demoApp)
}

// Load app configuration
var app = require(cfg.appRoot + 'package.json')

// Define icon tags
let iconTags = ''
let iconFiles = []
let icons = list.sync(path.resolve(cfg.packageRoot, 'icons'))
for (let i = 0; i < icons.length; i++) {
  if (/^icon-([0-9]+)\.([0-9]+)\.png/.test(icons[i])) {
    let size = icons[i].match(/^icon-([0-9]+)\.([0-9]+)\.png/)[1]
    iconTags += '<link rel="icon" type="image/png" size="' + size + 'x' + size + '" href="icons/' + icons[i] + '" />'
    iconFiles.push(icons[i])
  } else if (/^apple-touch-icon-([0-9]+)\.([0-9]+)\.png/.test(icons[i])) {
    let size = icons[i].match(/^apple-touch-icon-([0-9]+)\.([0-9]+)\.png/)[1]
    iconTags += '<link rel="apple-touch-icon" type="image/png" size="' + size + 'x' + size + '" href="icons/' + icons[i] + '" />'
    iconFiles.push(icons[i])
  }
}

// Define production webpack configuration
var webpackConfig = merge(baseWebpackConfig, {
  module: {
    loaders: utils.styleLoaders({ sourceMap: cfg.build.productionSourceMap, extract: true })
  },
  devtool: cfg.build.productionSourceMap ? '#source-map' : false,
  output: {
    path: path.resolve(cfg.appRoot, 'www/build-' + app.version),
    filename: '[name].[chunkhash].js',
    chunkFilename: '[id].[chunkhash].js'
  },
  vue: {
    loaders: utils.cssLoaders({
      sourceMap: cfg.build.productionSourceMap,
      extract: true
    })
  },
  plugins: [
    new webpack.DefinePlugin({
      'process.env': cfg.build.env
    }),
    new webpack.optimize.UglifyJsPlugin({
      compress: {
        warnings: false
      }
    }),
    new webpack.optimize.OccurrenceOrderPlugin(),
    new ExtractTextPlugin('[name].[contenthash].css'),
    new HtmlWebpackPlugin({
      filename: path.resolve(cfg.appRoot, 'www/build-' + app.version + '/index.html'),
      template: 'index.ejs',
      title: app.title,
      iconTags: iconTags,
      manifest: ' manifest="manifest.appcache"',
      inject: true,
      minify: {
        removeComments: true,
        collapseWhitespace: true,
        removeAttributeQuotes: true
      },
      chunksSortMode: 'dependency'
    }),
    new AppCachePlugin({
      cache: null,
      network: ['*'],
      fallback: null,
      settings: null,
      exclude: [/\.(js|css)\.map$/],
      output: 'manifest.appcache'
    }),
    new OnBuildPlugin(function (stats) {
      // Copy icons files
      for (let i = 0; i < iconFiles.length; i++) {
        copy.copySync(path.resolve(cfg.packageRoot, 'icons', iconFiles[i]), path.resolve(cfg.appRoot, 'www/build-' + app.version + '/icons'))
      }

      // Compress images
      let images = list(path.resolve(cfg.appRoot, 'www/build-' + app.version + '/img'))
      for (let i = 0; i < images.length; i++) {
        console.log('Compress ' + images[i])
      }

      // Update version in .htaccess file after successful build
      replace.sync({
        files: cfg.appRoot + 'www/.htaccess',
        replace: /\/build-([0-9]+)\.([0-9]+)\.([0-9]+)\//g,
        with: '/build-' + app.version + '/'
      })

      // Delete .babelrc file
      if (cfg.isInstalled && isThere(cfg.projectRoot + '.babelrc')) {
        deleteFiles.sync([cfg.projectRoot + '.babelrc'], {force: true})
      }
    })
  ]
})

// Optionally, add compression plugin
if (cfg.build.productionGzip) {
  var CompressionWebpackPlugin = require('compression-webpack-plugin')
  webpackConfig.plugins.push(
    new CompressionWebpackPlugin({
      asset: '[path].gz[query]',
      algorithm: 'gzip',
      test: new RegExp(
        '\\.(' +
        cfg.build.productionGzipExtensions.join('|') +
        ')$'
      ),
      threshold: 10240,
      minRatio: 0.8
    })
  )
}

module.exports = webpackConfig

@@ -1279,25 +1279,340 @@
 tags
-%0Alet iconTags = '
+ and save manifest json%0Alet manifest = %7B%0A  name: app.title,%0A  icons: %5B%5D,%0A  theme_color: app.iconBackgroundColor,%0A  background_color: app.iconBackgroundColor,%0A  display: 'standalone'%0A%7D%0Alet iconTags = '%3Cmeta name=%22theme-color%22 content=%22' + app.iconBackgroundColor + '%22 /%3E' +%0A               '%3Clink rel=%22manifest%22 src=%22manifest.json%22 /%3E
 '%0Ale
@@ -2000,16 +2000,126 @@
 ons%5Bi%5D)%0A
+    manifest.icons.push(%7B%0A      src: icons%5Bi%5D,%0A      sizes: size + 'x' + size,%0A      type: 'image/png'%0A    %7D)%0A
   %7D else
@@ -2426,17 +2426,122 @@
 i%5D)%0A  %7D%0A
-
 %7D
+%0AsaveJSON.writeFileSync(path.resolve(cfg.appRoot, 'www/build-' + app.version, 'manifest.json'), manifest)
 %0A%0A// Def

add theme meta tag and manifest.json