#!/usr/bin/env python
# Copyright (c) 2012-2013 Cloudena Taiwan Co.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# AUTHOR : Hugo Kuo

import signal

from optparse import OptionParser, SUPPRESS_HELP
from ConfigParser import ConfigParser
from sys import path, argv
from Queue import Empty, Queue

#Append sys.path by Hugo
path.append("/opt/objgw/s3backer")
from attacher import mount, loop_map, lvm_pv_binding, lvm_vg_binding


gw_stat_help = "stat"
#def gw_stat():

gw_list_help = "list"
#def gw_list():

gw_create_help = "create"
#def gw_create():

gw_delete_help = "delete"
#def gw_delete():

gw_start_help = "start"
#def gw_start():

gw_stop_help = "stop"
#def gw_stop():

gw_auth_help = "auth"
#def gw_auth():





if __name__ == '__main__':
    parser = OptionParser(version='%prog 1.0', usage='''
Usage: %%prog <command> [options] [args]

Commands:
  %(gw_stat_help)s
  %(gw_list_help)s
  %(gw_create_help)s
  %(gw_delete_help)s
  %(gw_start_help)s
  %(gw_stop_help)s
  %(gw_auth_help)s

Example:
  %%prog stat $pool_name
'''.strip('\n') % globals())
    parser.add_option('-n', '--name', action='store_true', dest='name',
                      default=False, help='The name of new pool')
    parser.add_option('-v', '--verbose', action='count', dest='verbose',
                      default=1, help='print more info')
    parser.add_option('-q', '--quiet', action='store_const', dest='verbose',
                      const=0, default=1, help='Suppress Status output')
    parser.disable_interspersed_args()
    (options, args) = parser.parse_args()
    if not args:
        args = ['-h']
    (options, args) = parser.parse_args(args)
    parser.enable_interspersed_args()
    
    commands = ('auth', 'stat', 'list', 'create', 'delete', 'start', 'stop')
    if not args or args[0] not in commands:
        parser.print_usage()
        if args:
            exit('no such command: %s' % args[0])
        exit()

    #signal.signal(signal.SIGINT, attempt_graceful_exit)
    print_queue = Queue(10000)    
    
    def _print(item):
        if isinstance(item, unicode):
            item = item.encode('utf8')
        print item

    print_thread = QueueFunctionThread(print_queue, _print)
