#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Script for importing DroidStumbler .csv files
#
# Original by Dennis Wagenaar <d.wagenaar@gmail.com>
#
# Rick van der Zwet <info@rickvanderzwet.nl>
#
from django.core.management import setup_environ
from django.core.management.base import BaseCommand, CommandError
from optparse import OptionParser, make_option
from gheat.models import *
import csv
import datetime
import gzip
import os
import sys

def import_droidstumbler(location, meetrondje, gebruiker, email):

  g, created = Gebruiker.objects.get_or_create(naam=gebruiker , email=email)
  a, created = Apparatuur.objects.get_or_create(antenne='buildin' , kaart='mobilePhone')
  mr = MeetRondje.objects.create(datum=datetime.datetime.now() , naam=meetrondje , gebruiker=g , apparatuur=a)

  if location.endswith('.gz'):
    fh = gzip.open(location,'rb')
  else:
    fh = open(location,'rb')
  csvfile = csv.reader(fh, delimiter=',')
  count = 0
  ap_cache = {}
  print "#INFO: Importing points"
  for row in csvfile:
    try:
      epoch, msg_type, lat, lon, accuracy, ssid, bssid, level, frequency, capabilities = row
    except ValueError:
      print "# WARNING: Unable to parse line:%i '%s'" % (csvfile.line_num, row)
      continue
    if msg_type == "data" and lat and lon:
      if not ap_cache.has_key(bssid):
        ap_cache[bssid], created = Accespoint.objects.get_or_create(mac=bssid, ssid=ssid, encryptie=capabilities)

      m = Meting.objects.create(meetrondje=mr, accespoint=ap_cache[bssid], latitude=lat, longitude=lon, signaal=(100 + int(level)))
      # Give some feedback to the user
      count += 1
      if (count % 1000) == 0:
        sys.stdout.write(str(count))
      elif (count % 100) == 0:
        sys.stdout.write(".")
      sys.stdout.flush()

  sys.stdout.write("%s\n" % count)
  print "#INFO: All done, goodbye"


class Command(BaseCommand):
  args = '<csvfile>[.gz]'
  option_list = BaseCommand.option_list + (
    make_option('-m', '--meetrondje', dest='meetrondje', default='rondje'),
    make_option('-g', '--gebruiker', dest='gebruiker', default='username'),
    make_option('-e', '--email', dest='email', default='foo@bar.org'),
    )

  def handle(self, *args, **options):
    try:
      (csv_file,) = args
    except ValueError:
      self.print_help(sys.argv[0],sys.argv[1])
      raise CommandError("Not all arguments are provided")
    if not os.path.isfile(csv_file):
      raise CommandError("csv file '%s' does not exists" % csv_file)
    import_droidstumbler(csv_file,options['meetrondje'],options['gebruiker'],options['email'])
