#!/usr/bin/env python

import getopt
import os
import string
import sys

#DEBUG = True
DEBUG = False
basename = os.path.basename(sys.argv[0])

def to_list(arg):
    """
    example: 
    input to function: "1.1.1.1,  2.2.2.2"
    output from function:  ['1.1.1.1', '2.2.2.2']
    """
    list = arg.split(",")
    out_list = []
    for param in list:
        param = string.lstrip(param)
        param = string.strip(param)
        out_list.append(param)
    return out_list

def usage(quiet_mode):
    """
    """
    if quiet_mode: return
    print >> sys.stderr, "\n"
    print >> sys.stderr, "usage:",basename,"-U username -P password -C CreateMcGroupOnVolume -i 'svm1-IP,svm2-IP' -n '[host1,[host2,...,[hostn]]' -g group_name -j job_name -s source_volume_name -t target_volume_name -p pool_name -l #[|n/N] [-c 'comment'] [-v environment_variable] -q -h"
    print >> sys.stderr, "\n"
    print >> sys.stderr, "example: "+basename+" -U rbarak -PpasswoRD -C CreateMcGroupOnVolume --SVMs_IPs '10.1.1.1 , 10.1.1.2' -n \"host1,host2\" -g gn -j jn -s svn -t tvn -p pool1 -ln -c 'a comment' -v '$LUN_NUMBER'"
    print >> sys.stderr, "\n"
    print >> sys.stderr, "Note:    arguments that expect more than one datum, e.g. -n or -i - should be quoted. Separate by commas."
    print >> sys.stderr, "\n"
    print >> sys.stderr, "-C, --command  "
    print >> sys.stderr, "         only 'CreateMcGroupOnVolume' is currently implemented\n"
    print >> sys.stderr, "-c, --comment  "
    print >> sys.stderr, "         optional comment\n"
    print >> sys.stderr, "-g, --group_name  "
    print >> sys.stderr, "         group_name should be unique. If not, duplicate error is returned\n"
    print >> sys.stderr, "-h, --help  "
    print >> sys.stderr, "         displays this help screen\n"
    print >> sys.stderr, "-i, --SVMs_IPs  "
    print >> sys.stderr, "         SVMs IP adddresses. If not, a connection error is returned\n"
    print >> sys.stderr, "-j, --job_name  "
    print >> sys.stderr, "         job_name should be unique. If not, duplicate error is returned\n"
    print >> sys.stderr, "-l, --LUN  "
    print >> sys.stderr, "         LUN. If a number is entered, this number will be the LUN number on all hosts."
    print >> sys.stderr, "         If 'n' or 'N' is entered, the next common free LUN on all hosts will be used\n"
    print >> sys.stderr, "-n, --host_names  "
    print >> sys.stderr, "         hosts list. If no hosts specified no permissions is given\n"
    print >> sys.stderr, "-P, --password  "
    print >> sys.stderr, "         password\n"
    print >> sys.stderr, "-p, --pool_name  "
    print >> sys.stderr, "         pool_name. If does not exist an error is returned\n"
    print >> sys.stderr, "-q, --quiet  "
    print >> sys.stderr, "         quiet mode. Will not display anything to the screen in case of error. Check return code\n"
    print >> sys.stderr, "-s, --source_volume_name  "
    print >> sys.stderr, "         source_volume_name. If does not exist an error is returned\n"
    print >> sys.stderr, "-t, --target_volume_name  "
    print >> sys.stderr, "         target_volume_name should be unique. If not, a duplicate error is returned\n"
    print >> sys.stderr, "-U, --username  "
    print >> sys.stderr, "         username\n"
    print >> sys.stderr, "-v, --environmet_variable  "
    print >> sys.stderr, "         optional. LUN number will be returned in this environment variable"
    print >> sys.stderr, "\n"
    print >> sys.stderr, "return codes:"
    print >> sys.stderr, '4   "unexpected command line argument"' 
    print >> sys.stderr, '5   "no command line parameters given"'
    print >> sys.stderr, '6   "unknown error occured. Possibilities: unknown argument given"'
    print >> sys.stderr, '11  "no username given"'
    print >> sys.stderr, '12  "no password given"'
    print >> sys.stderr, '13  "no command chosen"'
    print >> sys.stderr, '14  "no SVM IPs given"'
    print >> sys.stderr, '15  "no hosts given"'
    print >> sys.stderr, '16  "no group name given"'
    print >> sys.stderr, '17  "no job name given"'
    print >> sys.stderr, '18  "no source volume name given"'
    print >> sys.stderr, '19  "no target volume name given"'
    print >> sys.stderr, '20  "no pool name given"'
    print >> sys.stderr, '21  "no LUN given"'
    print >> sys.stderr, "\n"
 
def process_opt(opt, argument, name, return_code, params):
    """
    """
    if DEBUG: print "opt:",opt
    params[name] = argument

def main():
    """
    """
    params = {}
    return_code = 0
    quite_mode = False
         
    # Command line argument parsing
    try:                                
        opts, args = getopt.gnu_getopt(sys.argv[1:], "U:P:C:i:n:g:j:s:t:p:l:c:v:qh", ["username=", "password=", "command=", "SVMs_IPs=", "host_names=", "group_name=", "job_name=", "source_volume_name=", "target_volume_name=", "pool_name=", "LUN=", "comment=", "environmet_variable=","quiet","help"]) 
    except getopt.GetoptError, e:           
        if DEBUG: print "error:",e
        usage(quite_mode)                          
        return_code = 4 # "unexpected command line argument"
        return return_code, params

    for opt, arg in opts:                
        if opt in ("-q", "--quite"):
            quite_mode = True
        elif opt in ("-h", "--help"):
            usage(quite_mode)
            sys.exit(True)
        elif opt in ("-U", "--username"):
            return_code = process_opt(opt, arg, "username", -11, params)  # "no username given"
        elif opt in ("-P", "--password"):
            return_code = process_opt(opt, arg, "password", -12, params)  # "no password given"
        elif opt in ("-C", "--command"):
            return_code = process_opt(opt, arg, "command", -13, params)  # "no command chosen"
        elif opt in ("-i", "--SVMs_IPs"):
            if DEBUG: print "arg:",arg
            return_code = process_opt(opt, to_list(arg), "SVMs_IPs", -14, params)  # "no SVM IPs given"
        elif opt in ("-n", "--host_names"):
            return_code = process_opt(opt, to_list(arg), "hosts", -15, params)  # "no hosts given"
        elif opt in ("-g", "--group_name"):
            return_code = process_opt(opt, arg, "group_name", -16, params)  # "no group name given"
        elif opt in ("-j", "--job_name"):
            return_code = process_opt(opt, arg, "job_name", -17, params)  # "no job name given"
        elif opt in ("-s", "--source_volume_name"):
            return_code = process_opt(opt, arg, "source_volume_name", -18, params)  # "no source volume name given"
        elif opt in ("-t", "--target_volume_name"):
            return_code = process_opt(opt, arg, "target_volume_name", -19, params)  # "no target volume name given"
        elif opt in ("-p", "--pool_name"):
            return_code = process_opt(opt, arg, "pool_name", -20, params)  # "no pool name given"
        elif opt in ("-l", "--LUN"):
            return_code = process_opt(opt, arg, "LUN", -21, params)  # "no LUN given"
        elif opt in ("-c", "--comment"):
            return_code = process_opt(opt, arg, "comment", -22, params)  
            """    
        elif opt in ("-v", "--environmet_variable"):
            return_code = process_opt(opt, arg, "comment", -23, params)  
            """    
        else:
            usage(quite_mode)                          
            return_code = 6 # "unknown error occured"
            return return_code, params
     
    if DEBUG: print "params:",params
    if params == {}:
        usage(quite_mode)                          
        return_code = 5 # "no command line parameters given"
    elif not params.has_key("username"):
        usage(quite_mode)                          
        return_code = 11
    elif not params.has_key("password"):
        usage(quite_mode)                          
        return_code = 12
    elif not params.has_key("command"):
        usage(quite_mode)                          
        return_code = 13
    elif not params.has_key("SVMs_IPs"):
        usage(quite_mode)                          
        return_code = 14
    elif not params.has_key("hosts"):
        usage(quite_mode)                          
        return_code = 15
    elif not params.has_key("group_name"):
        usage(quite_mode)                          
        return_code = 16
    elif not params.has_key("job_name"):
        usage(quite_mode)                          
        return_code = 17
    elif not params.has_key("source_volume_name"):
        usage(quite_mode)                          
        return_code = 18
    elif not params.has_key("target_volume_name"):
        usage(quite_mode)                          
        return_code = 19
    elif not params.has_key("pool_name"):
        usage(quite_mode)                          
        return_code = 20
    elif not params.has_key("LUN"):
        usage(quite_mode)                          
        return_code = 21
    elif not params.has_key("comment"):
        return_code = process_opt("-c", "", "comment", 22, params)  
        return_code = 0
        """    
    elif not params.has_key("environment_variable"):
        return_code = process_opt("-v", "", "environment_variable", 23, params)  
        return_code = 0
        """    
    else:
        return_code = 0

    # Caller should continue processing only if return_code > -1
    return return_code, params

if __name__ == "__main__":
    return_code, params = main()
    print "return_code:",return_code,"; params:",params
    sys.exit(return_code)
