#!/usr/bin/env python
'''Performs the vast majority of the actions need to add a new authorized user to the Operations E-log description.  Must be invoked by the add_new_elog_user.bash script.'''

#
# Author: 17-Apr-2015, Bob Hall
#
import sys
import elogutils

if __name__ == "__main__":
#
#   Get the arguments passed by the invoking add_new_elog_user.bash script.
#
    account_names_file = sys.argv[1]
    firstname = sys.argv[2]
    lastname_prefix = sys.argv[3]
    lastname = sys.argv[4]
    authorization_type = sys.argv[5]
    elog_authorized_users_html_dir = sys.argv[6]

#
#   If the lastname_prefix argument was set to the flag value "null",
#   the user's last name does not have an embedded space.  If the
#   the user's last name does have an embedded space, form the
#   complete last name string by concatenating the prefix part
#   to the name part with a separating space.
#
    if (lastname_prefix != 'null'):
        lastname = lastname_prefix + " " + lastname

#
#   Get the account name from the list of UNIX accounts having
#   the same last name as the supplied user's last name.
#
    account_name = elogutils.get_account_name(account_names_file)
    if not account_name:
        print "no selected account name"
        exit(1)

#
#   Insert rows into Operations E-log Oracle tables to add
#   a new authorized user if the user was not added previously.
#   If the user was added previously as an authorized user
#   there will already be a row in the Oracle elog_users table
#   with the selected account name and the function will return
#   a error status, causing this program to exit.
#
    status = elogutils.insert_into_oracle(account_name, firstname, lastname, authorization_type)
    if (status == 1):
        exit(1)

#
#   Generate files containing the changed pieces of the
#   elog_authorized_users.html documentcaused by the addition
#   of the name of the new authorized user.  The invoking
#   bash script add_new_elog_user.bash will concatenate
#   all of the pieces (the static ones as well as the ones
#   that can be changed by this function) to generate a
#   new version of this document.
#
    status = elogutils.add_update_elog_authorized_users_files(firstname, lastname, authorization_type, elog_authorized_users_html_dir)
    if (status == 1):
        exit(1)

    exit(0)
