#
# Copyright (c) 2011 Adam Tauno Williams <awilliam@whitemice.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

from coils.core  import *
import getopt, sys, os, time
from pprint import pprint

def usage():
    print """

    """
    return

def main(argv):

    def callback(uuid, source, target, data):
        return True

    object_id = None
    timeout = 100
    entity_types = [ ]
    log_scan = False

    # Process command line arguements
    if (len(argv) == 0):
        usage()
        sys.exit(2)
    try:
        opts, args = getopt.getopt(argv,
                                   "h:i:tecdl",
                                   ["help", "objectid=", 'tasks', 'enterprises', 'contacts', 'documents', 'log-scan' ])
    except getopt.GetoptError, e:
        print e
        usage()
        sys.exit(2)
    for opt, arg in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit(0)
        elif (opt in ('-i', '--objectid')):
            object_id = arg
        elif (opt in ('-t', '--tasks')):
            entity_types.append('tasks')
        elif (opt in ('-e', '--enterprises')):
            entity_types.append('enterprises')
        elif (opt in ('-c', '--contacts')):
            entity_types.append('contacts')
        elif (opt in ('-d', '--documents')):
            entity_types.append('documents')
        elif (opt in ('-l', '--log-scan')):            
            log_scan = True


    # Initialize COILs
    initialize_COILS({'log_file': '{0}/coils.log'.format(os.getenv('HOME'))})
    broker = Broker()
    broker.subscribe('ping.{0}'.format(os.getpid()), None)
    ctx = AnonymousContext(broker=broker)
    
    if log_scan:
        print('Requesting index/re-index based on audit log')
        ctx.send(None, 'coils.vista.index/log_scan', { }, callback=callback)
        sys.exit(0)
    
    if object_id:
        object_ids = [ object_id ]
    else:
        object_ids = [ ]
        if 'tasks' in entity_types:
            for record in ctx.db_session().query(Task.object_id).all():
                object_ids.append(record[0])
        if 'contacts' in entity_types:
            for record in ctx.db_session().query(Contact.object_id).all():
                object_ids.append(record[0])
        if 'enterprises' in entity_types:
            for record in ctx.db_session().query(Enterprise.object_id).all():
                object_ids.append(record[0])                
        if 'documents' in entity_types:
            for record in ctx.db_session().query(Document.object_id).all():
                object_ids.append(record[0])  
        if 'notes' in entity_types:
            for record in ctx.db_session().query(Note.object_id).all():
                object_ids.append(record[0])   
                
    for object_id in object_ids:
        print('Requesting index/re-index of objectId#{0}'.format(object_id))
        ctx.send(None, 'coils.vista.index/index:{0}'.format(object_id), { }, callback=callback)
        
    ctx.close()
    broker.close()
    sys.exit(0)

if __name__ == "__main__":
    main(sys.argv[1:])
