#! /usr/bin/env python2
# -*- coding: utf-8 -*-

import argparse
import os
import string
import time
import sys
import ConfigParser
import platform 

import ontology_prefixes
import tools

# all ontology modules
import ncal
import nmm
import nco
import nfo
import mfo
import mto
import nmo
import mlo
import slo
import tracker

def recent_enough_python ():
  """
  True if it is 2.6 or more recent
  """
  print "Running generate with python", platform.python_version ()
  version = platform.python_version_tuple ()
  return (int(version[0]) >= 2 and int(version[1]) >= 6)

####################################################################################

def argument_parser():
  parser = argparse.ArgumentParser(description="Nepomuk test data generator")
  parser.add_argument('config_file', nargs=1)
  parser.add_argument('output_dir', nargs='?')
  return parser


args = argument_parser().parse_args()

config = ConfigParser.RawConfigParser()
try:
  loaded_files = config.read(args.config_file)
  # config.read 
  #   in 2.3 return None
  #   in 2.6+ returns a list of loaded files
  if recent_enough_python ():
    if (len (loaded_files) != 1):
      print "Cannot open %s" % (args.config_file)
      sys.exit (-1)
except Exception, e:
  print "Failed to read configuration file %s (%s)" % (args.config_file, e)
  sys.exit (-1)

if args.output_dir:
  if not os.path.exists(args.output_dir):
    os.makedirs(args.output_dir)


def get_counter (section, option):
  if config.has_option (section, option):
    return config.getint (section, option)
  else:
    return 1

# set up all known types
tools.addType( 'nco#EmailAddress', 10 )
tools.addType( 'nco#PostalAddress', 11 )
tools.addType( 'nco#PhoneNumber', 12 )
tools.addType( 'nco#IMAddress', 13 )
tools.addType( 'nco#ContactEmail', 14 )
tools.addType( 'nco#ContactCall', 15 )
tools.addType( 'nco#ContactIM', 16 )
tools.addType( 'nco#PersonContact', 18 )
tools.addType( 'slo#GeoLocation', 20 )
tools.addType( 'slo#Landmark', 21 )
tools.addType( 'mlo#GeoPoint', 26 )
tools.addType( 'mlo#LocationBoundingBox', 27 )
tools.addType( 'mlo#GeoLocation', 28 )
tools.addType( 'mlo#Landmark', 29 )
tools.addType( 'nmo#MailAccount', 30 )
tools.addType( 'nmo#MailFolder', 31 )
tools.addType( 'nmo#Email', 32 )
tools.addType( 'nmo#CommunicationChannel', 35 )
tools.addType( 'nmo#IMMessage', 36 )
tools.addType( 'nmo#SMSMessage', 37 )
tools.addType( 'nmo#Call', 38 )
tools.addType( 'nmm#Artist', 40 )
tools.addType( 'nmm#MusicAlbumDisc', 40)
tools.addType( 'nmm#MusicAlbum', 41 )
tools.addType( 'nmm#MusicPiece', 42 )
tools.addType( 'nfo#Equipment', 44 )
tools.addType( 'nmm#Photo', 45 )
tools.addType( 'nmm#Video', 46 )
tools.addType( 'tracker#Volume', 50 )
tools.addType( 'nfo#PlainTextDocument', 51 )
tools.addType( 'nfo#SoftwareCategory', 60 )
tools.addType( 'nfo#SoftwareApplication', 61 )
tools.addType( 'nfo#WebHistory', 65 )
tools.addType( 'ncal#Alarm', 70 )
tools.addType( 'ncal#Calendar', 71 )
tools.addType( 'ncal#Event', 72 )
tools.addType( 'ncal#Todo', 73 )
tools.addType( 'mfo#FeedChannel', 80 )
tools.addType( 'mfo#FeedMessage', 81 )
tools.addType( 'mto#TransferElement', 90 )
tools.addType( 'mto#UploadTransfer', 91 )

print "Generating Contacts",
count_contacts = get_counter('counts','contacts')
for contact in xrange(1, count_contacts+1):
  if (contact % 10 == 0):
    sys.stdout.write('.')
    sys.stdout.flush()
  nco.generateEmailAddress( contact )
  nco.generateContactEmail( contact )
  nco.generatePostalAddress( contact )
  nco.generatePhoneNumber( contact )
  nco.generateContactCall( contact )
  nco.generateIMAddress( contact )
  nco.generateContactIM( contact )
  nco.generatePersonContact( contact )
print "Done"

print "Generating Locations and landmarks",
count_locations = get_counter('counts','locations')
for location in xrange(1, count_locations+1):
  if (location % 10 == 0):
    sys.stdout.write('.')
    sys.stdout.flush()
  slo.generateGeoLocation( location )
  slo.generateLandmark( location )
count_locations = get_counter('counts','locations')
for location in xrange(1, count_locations+1):
  if (location % 10 == 0):
    sys.stdout.write('.')
    sys.stdout.flush()
  mlo.generateGeoPoint( location )
  mlo.generateLocationBoundingBox( location )
  mlo.generateGeoLocation( location )
  mlo.generateLandmark( location )
print "Done"

print "Generate Emails",
count_accounts = get_counter('counts','accounts')
count_folders = get_counter('counts','folders')
count_emails = get_counter('counts','emails')
for account in xrange(1, count_accounts+1):
  sys.stdout.write('.')
  sys.stdout.flush()
  nmo.generateMailAccount( account*count_folders*count_emails )
  for folder in xrange(1, count_folders+1):
    nmo.generateMailFolder( account*count_folders*count_emails+folder*count_emails )
    for email in xrange(1, count_emails+1):
      nmo.generateEmail( account*count_folders*count_emails+folder*count_emails+email )
print "Done"

print "Generate IM messages",
count_comchans = get_counter('counts','comchans')
count_ims = get_counter('counts','ims')
for comchannel in xrange(1, count_comchans+1):
  sys.stdout.write('.')
  sys.stdout.flush()
  nmo.generateCommunicationChannel( comchannel )
  for im in xrange(1, count_ims+1):
    nmo.generateIMMessage( comchannel*count_ims+im )
print "Done"

print "Generate SMS messages",
count_sms = get_counter('counts','sms')
for sms in xrange(1, count_sms+1):
  if (sms % 10 == 0):
    sys.stdout.write('.')
    sys.stdout.flush()
  nmo.generateSMSMessage( sms )
print "Done"

print "Generate calls",
count_calls = get_counter('counts','calls')
for call in xrange(1, count_calls+1):
  if (call % 10 == 0):
    sys.stdout.write('.')
    sys.stdout.flush()
  nmo.generateCall( call )
print "Done"

print "* Starting with file system based content"

print "Generate volumes",
count_volumes = get_counter('counts','volumes')
for volume in xrange(1, count_volumes+1):
  sys.stdout.write('.')
  sys.stdout.flush()
  tracker.generateVolume( volume )
print "Done"

print "Generating Music",
count_artists = get_counter('counts','artists')
count_albums = get_counter('counts','albums')
count_songs = get_counter('counts','songs')
count_discs = get_counter('counts','discs')
song_index = 0
for artist in xrange(1, count_artists+1):
  sys.stdout.write('.')
  sys.stdout.flush()
  nmm.generateArtist( artist*count_albums*count_songs )
  for album in xrange(1, count_albums+1):
    nmm.generateAlbum( artist*count_albums*count_songs+album )
    for disc in xrange(1, count_discs+1):
       discUri = nmm.generateMusicAlbumDisc( artist*count_albums*count_songs+album+disc )
       for song in xrange(1, count_songs+1):
          nmm.generateMusicPiece( song_index, discUri )
          song_index = song_index + 1
print "Done"

print "Generate Equipment",
count_equipment = get_counter('counts','equipment')
nmm.generateOwnEquipment ()
for equipment in xrange(1, count_equipment):
  if (equipment % 2 == 0):
    sys.stdout.write('.')
    sys.stdout.flush()
  nmm.generateEquipment( equipment )
print "Done"

print "Generate Photos",
count_images = get_counter('counts','images')
for photo in xrange(1, count_images+1):
  if (photo % 10 == 0):
    sys.stdout.write('.')
    sys.stdout.flush()
  nmm.generatePhoto( photo )
print "Done"

print "Generate Videos",
count_videos = get_counter('counts','videos')
for video in xrange(1, count_videos+1):
  if (video % 10 == 0):
    sys.stdout.write('.')
    sys.stdout.flush()
  nmm.generateVideo( video )
print "Done"

print "Generate plain text documents",
count_docs = get_counter('counts','docs')
for doc in xrange(1, count_docs+1):
  if (doc % 10 == 0):
    sys.stdout.write('.')
    sys.stdout.flush()
  nfo.generatePlainTextDocument( doc )
print "Done"

print "Generate feeds",
count_fchans = get_counter('counts','fchans')
count_fms = get_counter('counts','fms')
for fchan in xrange(1, count_fchans+1):
  sys.stdout.write('.')
  sys.stdout.flush()
  mfo.generateFeedChannel( fchan*count_fms )
  for fm in xrange(1, count_fms+1):
    mfo.generateFeedMessage( fchan*count_fms+fm )
print "Done"

print "Generate software",
count_softcats = get_counter('counts','softcats')
count_softapps = get_counter('counts','softapps')
for softcat in xrange(1, count_softcats+1):
  sys.stdout.write('.')
  sys.stdout.flush()
  nfo.generateSoftwareCategory( softcat*count_softapps )
  for softapp in xrange(1, count_softapps+1):
    nfo.generateSoftwareApplication( softcat*count_softapps+softapp )
print "Done"

print "Generate something for the rest",
count_others = get_counter('counts','others')
for index in xrange(1,count_others+1):
  if (index % 10 == 0):
    sys.stdout.write('.')
    sys.stdout.flush()
  nfo.generateWebHistory( index )
  ncal.generateAlarm( index )
  ncal.generateCalendar( index )
  ncal.generateEvent( index )
  ncal.generateTodo( index )
  mto.generateTransferElement( index )
  mto.generateUploadTransfer( index )
print "Done"

# dump all files
tools.saveResult(output_dir=args.output_dir)
