       data.latitude = latLon.latitude
       data.longitude = latLon.longitude
       data._id = data.TiplocCode
-      console.log(data)
       rows.push(data)
     ).on('end', ->
       getMongoDb (db) ->

      entry['_id'] = entry['TiplocV1']['tiploc_code']
      entries.push entry
    else if entry['JsonScheduleV1']
      id = entry['JsonScheduleV1']['CIF_train_uid']
      startDate = entry['JsonScheduleV1']['schedule_start_date']
      entry['JsonScheduleV1']['schedule_start_date'] \
        = moment(startDate, 'YYYY-MM-DD').toDate()
      endDate = entry['JsonScheduleV1']['schedule_end_date']
      entry['JsonScheduleV1']['schedule_end_date'] \
        = moment(endDate, 'YYYY-MM-DD').toDate()
      daysRun = entry['JsonScheduleV1']['schedule_days_runs']
      daysRun = daysRunToWeekArray(daysRun)
      entry['JsonScheduleV1']['schedule_days_runs'] = daysRun
      scheduleEntries.push entry

  console.log "#{entries.length} tiplocs to insert"
  console.log "#{scheduleEntries.length} schedules to insert"
  console.log 'inserting into db'
  getMongoDb (db) ->
    tiplocs = db.collection('tiplocs')
    tiplocs.insert entries, (error, docs) ->
      console.log 'inserted the entries'
      schedules = db.collection('schedules')
      async.each scheduleEntries, schedules.save.bind(schedules), (error) ->
        if error?
          console.log error
        console.log 'ALL DONE closing db'
        db.close ->
          console.log 'goodbye'

readRailReferenceCsv = (railReferenceCsv) ->
  console.log 'safa'
  rows = []
  csv(railReferenceCsv, headers: true)
    .on('data', (data) ->

          db.close()
  ).parse()


################################################################################
#
# main
#
################################################################################

main = ->
  if program.railreference
    console.log "loading rail reference form #{program.railreference}"
    readRailReferenceCsv(program.railreference)
  else if program.schedules
    for schedule in program.schedules
      console.log "loading schedule from #{schedule}"
      fs.readFile schedule, (error, data) ->
        if error
          throw error
        importSchedule(data)
  else
    for toc_code in config.datafeed.schedule_toc_codes
      do (toc_code) ->
        saveSchedule = (error, data) ->
          if error
            console.log error
          path = "/tmp/schedule-#{toc_code}.txt"
          fs.writeFile path, data, (error) ->
            if error
              console.log error
            else
              console.log "Saved to #{path}"
        getSchedule(saveSchedule, toc_code)

if require.main == module
  main()
