#!/usr/bin/python2.6

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

def usage():
    print """
    --help      Display this helpful messages
    --login=    Login value of the account to be modified [required].
    --password= Change the password of the specified account to the provided value.
    """
    return

def main(argv):

    login = password = None

    # Process command line arguements
    if (len(argv) == 0):
        usage()
        sys.exit(2)
    try:
        opts, args = getopt.getopt(argv,
                                   "hip",
                                  ["help", "login=", "password="])
    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 ('-u', '--login')):
            login = arg
        elif (opt in ('-p', '--password')):
            password = arg


    # Initialize COILs
    initialize_COILS({'log_file': '{0}/coils.log'.format(os.getenv('HOME'))})

    ctx = AdministrativeContext()

    user = ctx.run_command('account::get', login=login)
    if not user:
        print('Unable to retrieve user object for specified login "{0}"'.format(login))
        sys.exit(1)

    if password:
        ctx.run_command('account::set-password', login=login, password=password)
        ctx.commit()
        print('Password for login "{0}" changed.'.format(login))

    sys.exit(0)

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