coffee = require 'coffee-script'
path = require 'path'
yui = require 'yui-compressor'
fs = require 'fs'

FILENAME = 'jubiol'
FILES = [
  'kwing/lib/vector.coffee',
  'kwing/lib/array.coffee',
  'kwing/lib/timer.coffee',
  'kwing/lib/jukebox.coffee',
  'src/main.coffee',
  'src/sprite.coffee',
  'src/player.coffee',
  'src/state.coffee',
  'src/stage.coffee',
  'src/scene.coffee',
  'src/server_config.coffee'
]
HTMLFILE = 'index.html'

option "-o", "--output [DIR]", "set output directory"

task 'compile', 'compile and minify Osushi.', (options) ->
  for file, i in FILES
    console.log "load [#{i}] #{file}"
    FILES[i] = fs.readFileSync(file, "utf8")
  console.log "compile CoffeeScript -> JavaScript"
  js = coffee.compile FILES.join "\n"
  console.log "minify (yui-compressor)"
  yui.compile js, (err, res) ->
    if err?
      console.error err
    output = path.resolve "#{options.output or "."}/#{FILENAME}.min.js"
    fs.writeFile output, res, "utf8", ->
      console.log "complete"
