#!/usr/bin/env coffee

path = require("path")
util = require("util")

# because the environment is unknown when running as a command line utility we try the local require version
# first then fallback to our pellet npm version (this will be in the node_modules holding pellet) this is
# helpful because the local node_modules will not need to have all the loaders/modules for webpack installed
# i.e. cjsx, styl, coffee, etc.
try
  react = require("react")
catch ex
  react = require.resolve("pellet/node_modules/react")
try
  webpack = require("webpack")
catch ex
  webpack = require("pellet/node_modules/webpack")
try
  pelletPath = path.join(require.resolve("pellet"), "..", "node_modules")
catch ex
  pelletPath = ""
config =
  bail: true
  cache: true
  profile: true
  devtool: "#inline-source-map"
  entry: <%- _util.inspect(webpackEP) %>
  plugins: [
    new webpack.optimize.CommonsChunkPlugin()

    #new webpack.optimize.CommonsChunkPlugin('main.js', ['style', 'app']),
    new webpack.optimize.DedupePlugin()
  ]
  resolveLoader:
    modulesDirectories: [
      "node_modules"
      "bower_components"
    ]

  module:
    unknownContextCritical: false
    loaders: [
      {
        test: /\.json/
        loader: "json"
      }
      {
        test: /\.jsx/
        loader: "jsx"
      }
      {
        test: /\.cjsx/
        loader: "coffee!cjsx"
      }
      {
        test: /\.styl$/
        loader: "style!css!autoprefixer!stylus"
      }
      {
        test: /\.less$/
        loader: "style!css!autoprefixer!less"
      }
      {
        test: /\.css$/
        loader: "style!css!autoprefixer"
      }
      {
        test: /\.coffee/
        loader: "coffee"
      }
    ]

browserConfig = Object.create(config)
nodeConfig = Object.create(config)
browserConfig.target = "web"
browserConfig.output =
  path: path.resolve(__dirname, "<%- _outputBrowser %>")
  filename: "[name].js"
  chunkFilename: "[chunkhash].js"

nodeConfig.target = "node"
nodeConfig.output =
  path: path.resolve(__dirname, "<%- _outputServer %>")
  filename: "[name].js"
  chunkFilename: "[chunkhash].js"

exports = module.exports =
  nodeConfig: nodeConfig
  browserConfig: browserConfig
  watch: (nodeConfig, browserConfig, options) ->
    options = {}  unless options
    options.runFn = "run"
    exports.exec nodeConfig, browserConfig, options
    return

  build: (nodeConfig, browserConfig, options) ->
    options = {}  unless options
    options.runFn = "watch"
    exports.exec nodeConfig, browserConfig, options
    return

  exec: (nodeConfig, browserConfig, options) ->
    options = {}  unless options
    options.watchInterval = options.watchInterval or 100
    options.cb = options.cb or (err, info) ->
      if err
        console.error "Error packing:", err.message or err
      else if info and not options.silent
        console.log info.toString()
      else console.log "Ignored"  unless options.silent
      return

    webpack(nodeConfig).watch options.watchInterval, options.cb  if nodeConfig
    webpack(nodeConfig).watch options.watchInterval, options.cb  if browserConfig
    return


# bootstap webpack if run as a command
if process.argv[1] is __filename

  # because the environment is unknown we add paths to find webpack modules/loaders
  # and the path to the node_modules used while packing the file
  config.resolveLoader.root = [
    __dirname
    pelletPath
  ]
  module.exports.watch module.exports.nodeConfig, module.exports.browserConfig