             process.exit -1
 
           res.setEncoding 'utf8'
+          data = ''
+
           res.on 'data',(chunk) ->
-            console.log 'shortener response: '+chunk
-            jres = JSON.parse chunk
+            #console.log 'shortener response: '+chunk
+            data = data+chunk
+
+          res.on 'end',()->
+            jres = JSON.parse data
             su.shorturl = jres.id
+            console.log 'Shortened to '+su.shorturl
             # recurse
             fix_shorturls shorturls,i+1
 

# coffee-script version of cache builder
https = require 'https'
fs = require 'fs'
xml2js = require 'xml2js'

if process.argv.length<3 
  console.log 'usage: coffee cb.coffee <KIOSK-ATOM-FILE>'

  # un-needed files in cache?
  for oldfile in oldfiles when oldfile.path? and not oldfile.needed 
    cache.files.push oldfile

# get local cache path for an URL - like java cacheing,
# maps domain name elements and path elements to folders
get_cache_path = (url) ->
  url = parse_url url
  # host, port, path (includes query), hash
  hs = if url.host? then url.host.split '.' else []
  # reverse non-IP order
  number = /^[0-9]+$/
  ns = 0
  for h in hs when number.test h
    ns++
  if ns != hs.length
    hs.reverse
  # normalise domain name to lower case
  hs = for h in hs
    String(h).toLowerCase()
  # ignore port for now!
  ps = if url.path? then url.path.split '/' else []
  # leading /?
  if ps.length>1 and ps[0]==''
    ps.shift()
  hs = ["cache"].concat hs,ps
  # make safe filenames
  hs = for h in hs
    if h=='' then '_' else encodeURIComponent h # is that enough??
  path = hs.join '/'
  return path

# parse file(s) and call worker(s)
parser.parseString data,(err,result) ->
