 timeout = null
 packs = null
 hostname = null
+excludeFlows = null
 perf = false
 date = null
 buildId = null

printUsageAndExit = (message) ->
  console.log "*** #{message} ***"
  console.log 'Usage: phantomjs headless-test.js [--host ip:port] [--timeout seconds] [--packs foo:bar:baz] \
                                                 [--perf date buildId gitHash gitBranch ncpu os jobName outputDir]'
  console.log '    ip:port  defaults to localhost:54321'
  console.log '    timeout  defaults to 3600'
  console.log '    packs    defaults to examples'
  console.log '    perf     performance of individual tests will be recorded in perf.csv in the output directory'
  phantom.exit 1

parseOpts = (args) ->

                  (Flow.Async.iterate tasks) go

            runFlow = (packName, flowName, go) ->
              doFlow = (flowName, excludeFlowNames) ->
                for f in excludeFlowNames
                  return false if flowName is f
                true

              if doFlow flowName, window._excludeFlowNames
                flowTitle = "#{packName} - #{flowName}"
                window._phantom_test_summary_[flowTitle] = 'FAILED'
                console.log "Fetching flow document: #{packName} - #{flowName}..."
                context.requestFlow packName, flowName, (error, flow) ->
                  if error
                    console.log "*** ERROR *** Failed fetching flow #{flowTitle}"
                    go new Error "Failed fetching flow #{flowTitle}", error
                  else
                    console.log "Opening flow #{flowTitle}..."

                    window._phantom_running_ = yes

                    # open flow
                    context.open flowTitle, flow

                    waitForFlow = ->
                      if window._phantom_running_
                        console.log 'ACK'
                        setTimeout waitForFlow, 2000
                      else
                        console.log 'Flow completed!'
                        errors = window._phantom_errors_
                        # delete all keys from the k/v store
                        context.requestRemoveAll ->
                          go if errors then errors else null

                    console.log 'Running flow...'
                    window._startTime = new Date().getTime() / 1000
                    context.executeAllCells yes, (status, errors) ->
                      window._endTime = new Date().getTime() / 1000
                      console.log "Flow finished with status: #{status}"
                      if status is 'failed'
                        window._pass = 0
                        window._phantom_errors_ = errors
                      else
                        window._pass = 1
                        window._phantom_test_summary_[flowTitle] = 'PASSED'
                      if window._perf
                        window.callPhantom "#{window._date}, #{window._buildId}, #{window._gitHash}, \
                                            #{window._gitBranch}, #{window._hostname}, #{flowName}, \
                                            #{window._startTime}, #{window._endTime}, #{window._pass}, \
                                            #{window._ncpu}, #{window._os}, #{window._jobName}\n"
                      window._phantom_running_ = no

                  setTimeout waitForFlow, 2000
              else
                console.log "Ignoring flow: #{flowName}"
                go null

            console.log 'Starting tests...'
            window._phantom_errors_ = null
