       "
 
 
-
-getLogFiles = (globs, done) ->
-  async.map globs, glob, (err, files) ->
-    for f, i in files
-      files[i] = f[0]
-    done(err, files)
-
-
-
-
-cmdInfo = (globs..., options) ->
-  getLogFiles globs, (err, files) ->
-    for file in files
-      parseLogFile file, displayTrialInfo
-
-
-# cmdInfo = (globs..., options) ->
-#   async.map globs,
-#
-#   for g in globs
-#     glob g, null, (err, files) ->
-#       parseLogFile file, (err, data) ->
-#         if err
-#           console.error err
-#         else
-#           displayTrialInfo(data)
-
-
-
-
-program.version('0.0.1')
-
-program.command('info logfiles...')
-  .action(cmdInfo)
+##
+# Displays extracted event data.
+displayExtractedEvents = (data, filename, options) ->
+  events = extractEvents data, options
+
+  if options.json
+    for e, i in events
+      events[i] = JSON.stringify(e)
+
+  else if options.csv
+    if events.length > 0
+      labels = []
+      for k, v of events[0]
+        labels.push k
+      console.log labels.join ','
+      for e, i in events
+        evt = []
+        for k, v of events[i]
+          evt.push v
+        events[i] = evt.join ','
+
+  if options.count
+    console.log events.length
+  else
+    console.log events.join '\n'
+
+
+
+displayExperimentInfo = (data, filename, options) ->
+  times = collectExperimentTimes data, filename, options
+
+  displayHeader filename
+
+  for e of times
+    for input,v of times[e]
+      console.log mapExperimentNames(e), mapInputNames(v.input), moment.duration(v.duration).asSeconds()
+
+
+
+info = (files, options) ->
+  for file in files
+    if options.experiment
+      parseLogFile file, displayExperimentInfo, options
+    else
+      parseLogFile file, displayTrialInfo, options
+
+
+
+extract = (files, options) ->
+  for file in files
+    parseLogFile file, displayExtractedEvents, options
+
+
+
+parseArgs = (files..., options) ->
+
+  if options.info
+    info files, options
+
+  if options.display
+    extract files, options
+
+
+
+program
+  .version('0.0.1')
+  .usage('<log> [logs...]')
+  .option('-a, --analyze', 'perform log analysis')
+  .option('-i, --info', 'ouput formatted information')
+  .option('-D, --display', 'extract and display event information')
+  .option('-x, --experiment', 'include experiment events')
+  .option('-l, --load', 'include load events')
+  .option('-L, --unload', 'include unload events')
+  .option('-m, --mouse-move', 'include mouse move events')
+  .option('-d, --mouse-down', 'include mouse down events')
+  .option('-u, --mouse-up', 'include mouse up events')
+  .option('-c, --mouse-click', 'include mouse click events')
+  .option('-g, --gaze', 'include gaze events')
+  .option('-b, --blink', 'include blink events')
+  .option('-f, --fixation', 'include fixation events')
+  .option('-F, --fixation-end', 'include fixation-end events')
+  .option('-C, --count', 'prints the count of the cooresponding events')
+  .option('-j, --json', 'print output as JSON')
+  .option('-s, --csv', 'print output as CSV')
+  .action(parseArgs)
 
 program.parse(process.argv)


mapExperimentNames = (e) ->
  switch e
    when 'f' then 'Focus'
    when 'w' then 'Wikipedia'
    when 's' then 'Synthetic UI'
    when 'a' then 'Amazon'
    when 'n' then 'Natural Use'



extractEvents = (event, data) ->
  es = []
  for datum in data
    if datum.event and datum.event is event then es.push datum
  es


##
# Takes a logfile filename, reads the file and turns it into a javascript
# array of parsed JSON.
parseLogFile = (filename, done) ->
  fpath = path.resolve process.cwd(), filename
  async.waterfall [
    (cb) ->

        if datum is ''
          data.splice i, 1
      cb(null, data, filename)
  ], (err, data, filename) ->
    if err
      console.error err
      process.exit 1
    else
      done.call null, data, filename, options


collectExperimentTimes = (data, filename, options) ->
  times = {}
  events = extractEvents data, {unload: true, experiment: true}
  experimentStarts = {}
  experimentUnloads = {}
  currExp = null
  currInput = null

  for e in events
    if e.event is 'experiment start'
      currExp = e.experiment
      currInput = e.input

      if currExp is 'f' and e.fading is 'with-fading'
        currExp = 'ff'

      if currExp is 'f' and e.fading is 'without-fading'
        currExp = 'fw'

      experimentUnloads[currExp] ?= {}
      experimentStarts[currExp] ?= {}
      experimentUnloads[currExp][currInput] = []
      experimentStarts[currExp][currInput] = e.timestamp

    else if currExp
      experimentUnloads[currExp][currInput].push e

  for e of experimentUnloads
    for input, unloads of experimentUnloads[e]
      if unloads.length > 0
        t = unloads[unloads.length - 1].timestamp
      else
        t = Infinity
      times[e] ?= {}
      times[e][input] =
        experiment: e
        input: input
        start: experimentStarts[e][input]
        end: t
        duration: t - experimentStarts[e][input]

  return times


displayHeader = (filename) ->
  console.log "
    \n-------------------------------------------------------------------------
    \nFilename: #{filename}\n"


##
# Displays trial and survey info for the passed trial logs.
displayTrialInfo = (data, filename) ->
  info = extractEvent('participant end', data)
  displayHeader filename
  if info.length is 0
    console.error "
    \nNo participant information found.\n
    "
  else
