#!/usr/local/bin/python2.5

import sys
import os
import socket
# fix symbolic link
#sys.path.insert(0,os.path.dirname(os.path.abspath(sys.argv[0])))
try:
    from optparse import OptionParser
except ImportError:
    from BitQueue.optparse import OptionParser
from BitQueue import version

parser = OptionParser(usage='btqueue.py [-r|--root_path path] scheduler|crawler|remote|query|add [args...]',
                      version=version)
parser.add_option("-r","--root",dest="root_path",default=None,
                  help="specify root path")
parser.add_option("-d","--debug_level",dest="debug_level",default=1,
                  type='int',
                  help="specify debug level")
parser.add_option("-t","--test_crawler",dest="test_crawler",default=0,
                  action='store_true',
                  help="turn on crawler test mode")

def usage():
    print 'btqueue.py [scheduler|crawler|daemon|remote|query|add] [args...]'
    sys.exit()

if __name__ == '__main__':
    (options,args) = parser.parse_args()

    if len(args) == 0:
        parser.error('incorrect number of arguments')

    command = args[0]

    if sys.platform == 'win32' or not os.environ.get('HOME'):
        root_path = os.path.dirname(os.path.abspath(sys.argv[0]))
    else:
        root_path = os.path.join(os.environ.get('HOME'),'.btqueue')

#    if len(sys.argv) == 3:
#        root_path = sys.argv[2]
    root_path = options.root_path or root_path

    from BitQueue import policy
    pol = policy.Policy(root_path)
    pol.set_default()
    if options.debug_level >= 0:
        pol.update('debug_level',options.debug_level)

    if command == 'scheduler':
        from BitQueue.manager import Console
        app = Console()
        try:
            app.mainloop()
        except SystemExit:
            pass
        except Exception,why:
            print why
    elif command == 'daemon':
        from BitQueue.manager import Daemon
        app = Daemon()
        try:
            app.daemonize()
        except SystemExit:
            pass
        except Exception,why:
            print why
    elif command == 'remote':
        from BitQueue.manager import XMLRPCConsole
        app = XMLRPCConsole()
        try:
            app.mainloop()
        except SystemExit:
            pass
        except Exception,why:
            print why
    elif command == 'crawler':
        from BitCrawler.crawler import Crawler
        from BitQueue import timeoutsocket
        timeoutsocket.setDefaultSocketTimeout(None)
        app = Crawler(options.test_crawler)
        app.process()
    else:
        from BitQueue.manager import XMLRPCConsole
        from BitQueue.xmlrpc import CommandResponse
        app = XMLRPCConsole()
        if sys.platform == 'win32' and command == 'add':
            from BitQueue import timeoutsocket
            timeoutsocket.setDefaultSocketTimeout(2)
        if command == 'query':
            command = 'list'
        elif command == 'add':
            command = 'add_metadata'
        line = ' '.join([command]+['"%s"' % a for a in args[1:]])
        timeout = 0
        try:
            res = app.onecmd(line)
        except socket.error,why:
            res = CommandResponse(error=why[1])
            timeout = 1
        from BitQueue import timeoutsocket
        timeoutsocket.setDefaultSocketTimeout(None)
        if sys.platform == 'win32' and command=='add' and \
           res and res.geterror():
            if timeout:
                from BitQueue.manager import Console
                app = Console()
                app.do_add(' '.join(args[1:]))
                app.mainloop()
            else:
                print res.geterror()
                raw_input()
        elif res and res.geterror():
            print res.geterror()
