   res.end JSON.stringify {error: {message: message}}
 
 
-sendFileToSegmenter = (path,callback) ->
+sendFileToSegmenter = (stream,callback) ->
   callback_url = "http://192.168.201.196:3000/internal/add_segment"
   post_options = 
     host: 'vbox'

    destdir = "#{__dirname}/../public/segmented/"
    filename = "#{stream_id}-#{Math.floor(Math.random()*1000000)}.ts"
    dest = destdir + filename

    # Write out the file
    console.log 'pipe'
    console.log 'endpipe'
    console.log dest
    f = fs.createWriteStream dest
    f.on 'close', ->
      console.log "Wrote file #{dest}"

      # Add link to the live stream
      url = "/segmented/#{filename}"
      ls.addTs url

      # Close the stream
      res.end 'ok'


    # Write out file
    req.pipe(f)


    path: "/segment_ts/?callback_url=#{encodeURIComponent(callback_url)}"
    method: 'POST'

  #post_options.host = 'localhost'
  #post_options.port = 3000


  console.log "sending file to segment service:"
  console.log post_options
  post_request = http.request post_options, (res) ->
    if callback? then callback()

  # Pipe file into the post request
  stream.pipe post_request


module.exports = (app) ->
