 
 module.exports = new nodeio.Job
   input: false
-  run: (f) ->
-    startJob new CityImport(), () =>
-      startJob new NamesImport(), () =>
-        @emit "geonames import finished."
+  run: (f) =>
+    #startJob new CityImport(), () =>
+    start new NamesImport(), () =>
+      @emit "geoname import finished."
 
-startJob = (job, callback) ->
+start = (job, callback) ->
   nodeio.start job, {}, (
     (err) ->
       if err

        adminCodes[line[0].split(".")[1]] = line[3]
  run: (row) =>
    place = @parseValues(row,'\t')
    #population = data[14]
    #lon = data[4]
    #lat = data[5]
    if place[7] && place[7].match /PPL.*/
      redis.get "geonames:id:#{adminCodes[place[10]]}", (err, admin_key) ->
        if admin_key && admin_key.substring(0,2) == place[8] # consistency
          foreign_key = "geonames:id:#{place[0]}"     # geoname:id:1234567
          primary_key = "#{admin_key}:#{place[1]}"    # DE:Bavaria:München
          redis.sadd primary_key, foreign_key
          redis.set foreign_key, primary_key
          #console.log " + stored city: "+place[1]
        else
          console.log " - something Kompost here with "+place[1] +
            "! adminCode is "+place[10]+", Key is "+admin_key
    @exit
  output: false

class NamesImport extends nodeio.JobClass
  input: "/tmp/alternateNames.txt"
  description: "import of alternative names in #{COUNTRY}"
  run: (row) =>
    data = @parseValues(row,'\t')
    foreign_key = "geonames:id:#{data[1]}"
    redis.get foreign_key, (err, internal_key) =>
      if internal_key && (data[2] == "de" || data[2] == "") && data[4] == "1"  # prefered name
        better_internal_key = internal_key.match(/(.*:)[^:]+$/)[1]+data[3]
        redis.smembers internal_key, (err, foreign_keys) =>
          console.log "To update: "+foreign_keys.length
          redis.multi(["set", key, better_internal_key] for key in foreign_keys).exec()
          redis.multi(["sadd", better_internal_key, key] for key in foreign_keys)
            .exec (err, success) =>
              redis.del internal_key, (err, success) =>
                console.log " -/+ replaced internal key: "+internal_key +
                  " with a better internal key "+better_internal_key
                @emit true
      else if internal_key && data[2] != ""
        redis.set "geonames:alt:#{data[3]}", internal_key
        redis.sadd internal_key, "geonames:alt:#{data[3]}"
        console.log " + stored alt name: "+data[3] +
          " for internal key " + internal_key
        @emit true
  output: false


  ), false


class GeonameImport extends nodeio.JobClass
  findBetterName: (geoname_id) ->
    name = "foo" # as we want the best available name as key
    redis.smembers "alt:geoname:#{place[0]}", (err, alts) ->
      prefered = a for a in alts when a.match(/.*:1:.*/)
      console.log prefered.length
      console.log p for p in prefered
      if prefered.length > 1
        console.log "shorts:"
        short = a for a in alt when a.match(/^.*:1:1:.*/)
        console.log p for p in short
    name
  store: (primary_key, foreign_key) ->
    console.log "sure"

class AdminImport extends GeonameImport
  input: "/tmp/#{COUNTRY}.txt"
  description: "import of administrative divisions in #{COUNTRY}"
  run: (row) =>
    place = @parseValues(row,'\t')
    if place[7] && place[7].match /ADM1/
      name = @findBetterName(place[0])
      primary_key = "#{place[8]}:#{name}"   # DE:Thueringen
      foreign_key = "geoname:id:#{place[0]}"   # geoname:id:42
      redis.sadd primary_key, foreign_key
      redis.set foreign_key, primary_key
      console.log " + stored admin: "+place[1]
      @emit true
  output: false


adminCodes = {}

class CityImport extends GeonameImport
  input: "/tmp/#{COUNTRY}.txt"
  description: "import of cities in #{COUNTRY}"
  constructor: ->
