#!/usr/bin/env python

import os
import stat
import sys
import argparse
import re

import sojourner.utils.constants as C
from sojourner.utils.cli import *

# +----------------------------------------------------------------------+
def menu ():

        parser = argparse.ArgumentParser(description="Sojourner")
        parser.add_argument("-d", "--debug", help="debug on",action='store_true')

        subparsers = parser.add_subparsers()

        parser_show = subparsers.add_parser('show',help="show's all the host")
        parser_show.add_argument("-m", "--machine", help="host name to search for")
        parser_show.add_argument("-r", "--role", help="Role of the machine to search for.")
        parser_show.add_argument("-p", "--product", help="product of the machine to search for.")
        parser_show.add_argument("-o", "--owner", help="owner of the machine to search for.")
        parser_show.set_defaults(func=show)

        parser_reap  = subparsers.add_parser('reap', help="Delete the host from Inventory")
        parser_reap.add_argument("-m", "--machine", help="host name to reap",required=True)
        parser_reap.set_defaults(func=reap)

        parser_add  = subparsers.add_parser('assign', help="Assigns the host and runs the playbook for the assigned project")
        parser_add.add_argument("-d", "--debug", help="debug on",action='store_true')
        parser_add.add_argument("-m", "--machine", help="host name to reap",required=True)
        parser_add.add_argument("-p", "--product", help="Product",required=True)
        parser_add.add_argument("-r", "--role", help="Role",required=True)
        parser_add.set_defaults(func=assign)

        parser_listroles  = subparsers.add_parser('list-roles', help="list-roles available in Sojourner Deployer")
        parser_listroles.add_argument("-p", "--product", help="Product",required=True)
	parser_listroles.set_defaults(func=listroles)

        parser_listproducts  = subparsers.add_parser('list-products', help="list-products available in Sojourner Deployer")
	parser_listproducts.set_defaults(func=listproducts)

        args = parser.parse_args()

        args.func(args)
        return 0
# +----------------------------------------------------------------------+
def main():
	init ()
	if len(sys.argv) == 2 and (sys.argv[1] == '--list'):
        	grouplist()
	elif len(sys.argv) == 3 and (sys.argv[1] == '--host'):
        	hostinfo(sys.argv[2])
	else :
        	menu()

# +----------------------------------------------------------------------+
if __name__ == "__main__":
        sys.exit(main())
# +----------------------------------------------------------------------+
