#!/bin/bash
#==============================================================
#
# Name: add_new_elog_user.bash
#
# Abs:  This script adds a new authorized Operations E-log user
#       if the user was not previously one.  A user must be
#       added as an authorized user in order to make entries
#       into Operations E-log logbooks.
#
# Use:  Is only allowed to run under the "laci" account.
#       See "Usage" information output in the code below
#       regarding command line arguments.
#
# Auth:  17-Apr-2015, Bob Hall
#
#==============================================================
# Mods:
#        20-May-2015, Bob Hall
#           Convert the first character of user entered
#           first and last names to uppercase, allowing
#           the user to enter first and last names in
#           all lowercase for convenience.
#
#==============================================================
#
if [ "$USER" != laci ]; then
    echo " "
    echo "This script may only be run using the\"laci\" account-- exiting."
    echo " "
    exit 1
fi

source ${LCLS_ROOT}/tools/oracle/oracleSetup_11g.bash

export TNS_ADMIN=${TOOLS}/oracle/wallets/elog_owner

echo "This script is run to add a new 'authorized' Operations E-log system"
echo "user so that the user can make entries into logbooks for this system."
echo " "
echo "Usage:"
echo "   add_new_elog_user.bash firstname lastname authorization_type"
echo "     where firstname is the user's first name"
echo "       lastname is the user's last name (some last names contain an embedded space)"
echo "       authorization_type is one of the following:"
echo "         nonrestricted - user can only make entries into"
echo "                         to nonrestricted logbooks"
echo "         mcc - user can make entries into the restricted MCC"
echo "               logbook as well as nonrestricted logbooks"
echo "         spear3 - user can make entries into the restricted SPEAR3"
echo "                  logbook as well as nonrestricted logbooks"
echo "         ssrl-bldo - user can make entries into the restricted SSRL-BLDO"
echo "                     logbook as well as nonrestricted logbooks" 
echo " "
echo "   example: add_new_elog_user.bash Jane Doe nonrestricted"
echo " "
echo "This script can be used to allow a user to make entries to"
echo "only one restricted logbook.  If there is a need to allow"
echo "the user to make entries to additional restricted logbooks"
echo "run the script 'add_to_restricted_logbook.bash'."
echo " "

account_names_file=/tmp/elog_lastname_unix_account_names_info.txt
elog_authorized_users_html_dir=/afs/slac/g/cd/soft/html/elog/users/
elog_authorized_users_html_prev_file=elog_authorized_users.html.prev
elog_authorized_users_html_file=elog_authorized_users.html
users_piece_1_file=users_piece_1.html
mcc_users_piece_file=mcc_users_piece.html
users_piece_2_file=users_piece_2.html
spear3_users_piece_file=spear3_users_piece.html
users_piece_3_file=users_piece_3.html
ssrl_bldo_users_piece_file=ssrl_bldo_users_piece.html
users_piece_4_file=users_piece_4.html
all_users_piece_file=all_users_piece.html
users_piece_5_file=users_piece_5.html
tools_script_dir=/afs/slac/g/lcls/tools/script

num_arguments=$#
if [ $num_arguments -ne 3 ] && [ $num_arguments -ne 4 ]; then
    echo " "
    echo "Requires three arguments (or four if the user's last name contains an embedded space)-- see \"Usage\"."
    echo " "
    exit 1
fi

#
# If there are three arguments, this is the usual case where the
# user has a last name without an embedded space so set the
# "lastname_prefix" variable to a special flag value, "null",
# that can be checked for.  If there four arguments, the
# second and third arguments comprise the full last name.
# For instance, if the user's last name is "Van Dyke" the
# "lastname_prefix" variable would be set to "Van" and
# the "lastname" variable would be set to "Dyke".
#
if [ $num_arguments -eq 3 ]; then
    firstname=$1
    lastname_prefix="null"
    lastname=$2
    auth_type=$3
else
    firstname=$1
    lastname_prefix=$2
    lastname=$3
    auth_type=$4
fi

#
# Convert the first character of user entered first and last names to 
# uppercase, allowing the user to enter first and last names in all
# lowercase for convenience.
#
firstname="$(tr '[:lower:]' '[:upper:]' <<< ${firstname:0:1})${firstname:1}"
lastname="$(tr '[:lower:]' '[:upper:]' <<< ${lastname:0:1})${lastname:1}"

#
# Validate the supplied authorization type and set the variable
# to its value (with the upper/lower case set as expected later).
#
case $auth_type in
    nonrestricted|NONRESTRICTED) authorization_type='nonrestricted';
                                 ;;
    mcc|MCC) authorization_type='MCC';
                                 ;;
    spear3|SPEAR3) authorization_type='SPEAR3';
                                 ;;
    ssrl-bldo|SSRL-BLDO) authorization_type='SSRL-BLDO';
                                 ;;
    *) echo "Invalid authorization type-- see \"Usage\"."
       exit 1
       ;;
esac

#
# Run the command "ypcat passwd" to get information for all of the UNIX
# accounts and filter it to output only those lines matching the
# supplied last name (with a colon following, to exclude lines
# having this string as a substring within a longer name).  The
# result is output to a temporary file that will be processed
# immediately by the add_new_elog_user.py script.
#
ypcat passwd | grep "$lastname:" > $account_names_file
exit_code=$?
if [ $exit_code != 0 ]; then
    echo "There were no UNIX accounts for the supplied last name.  If there was no typographical error in"
    echo "the supplied last name, the user does not have a UNIX account."
    echo "The user must have a UNIX account before they can be added as an Operations E-log authorized user."
    echo "Email the user informing him/her of this and have the user contact you again after"
    echo "the user has had a UNIX account created for him/her. Applications for UNIX accounts are"
    echo "made through the SCCS Accounts Services office in building 50."
    exit $exit_code
fi

#
# Execute the script add_new_elog_user.py to perform the actions needed
# to add a new authorized Operations E-log user if the user was not already
# one. 
#
$tools_script_dir/add_new_elog_user.py $account_names_file $firstname $lastname_prefix $lastname $authorization_type $elog_authorized_users_html_dir
exit_code=$?
if [ $exit_code != 0 ]; then
    exit $exit_code
fi

#
# Create a new version of the elog_authorized_users.html document containing
# one or more instances of the user's name if the script add_new_elog_user.py
# added a new authorized Operations E-log user successfully.  The user's
# name will be added to the "all authorized users" section of this document
# and, if it was specified that the user be authorized to make entries into
# one of the restricted logbooks, the section of this document for that
# restricted logbook listing the users authorized to make entries into that
# logbook.
#
# If the script add_new_elog_user.py added a new authorized Operations E-log
# user successfully, it will have generated the all_users_piece.html file
# from the all_users.txt file it modified to add the new user.  It will
# have also generated a <logbook>_users_piece.html file from the
# <logbook>_users.txt file if the supplied authorization type specified
# that the user should be authorized to add entries to one of the restricted
# logbooks.  The "*.txt" files in the directory where the
# elog_authorized_users.html is stored are used in later invocations
# of add_new_elog_user.bash when new versions of the all_users_piece.html
# or <logbook>_users_piece.html files need to be generated.
#
# The "user_piece_n.html" files (where n is an integer 1-5) are static
# pieces of the elog_authorized_users.html document used in generating a
# new version of this document from pieces that may have been modified by
# the add_new_elog_user.py script. 
#
cd $elog_authorized_users_html_dir
cp $elog_authorized_users_html_file $elog_authorized_users_html_prev_file
rm -f $elog_authorized_users_html_file

cat $users_piece_1_file $mcc_users_piece_file $users_piece_2_file $spear3_users_piece_file $users_piece_3_file $ssrl_bldo_users_piece_file \
    $users_piece_4_file $all_users_piece_file $users_piece_5_file > $elog_authorized_users_html_file
exit_code=$?
if [ $exit_code != 0 ]; then
    echo "Failed to update "$elog_authorized_users_html_dir$elog_authorized_users_html_file"."
    exit $exit_code
fi

echo "Successfully added a new authorized Operations E-log user."

if [ $authorization_type == nonrestricted ]; then
    echo " "
    echo "Send email to the user notifying the user that he/she"
    echo "can now make entries into any nonrestricted logbook"
    echo "(any logbook other than MCC, SPEAR3, and SSRL-BLDO)."
    echo " "
elif [ $authorization_type == MCC ]; then
    echo " "
    echo "Send email to the user notifying the user that he/she"
    echo "can now make entries into any nonrestricted logbook"
    echo "and the restricted MCC logbook (but not the restricted"
    echo "SPEAR3 and SSRL-BLDO logbooks)."
    echo "Copy Mike Stanek and Peter Schuh in the email since"
    echo "they are the 'gatekeepers' of the MCC logbook and"
    echo "it is possible that they might question this user"
    echo "being allowed to make entries into the MCC logbook."
    echo " "
elif [ $authorization_type == SPEAR3 ]; then
    echo " "
    echo "Send email to the user notifying the user that he/she"
    echo "can now make entries into any nonrestricted logbook"
    echo "and the restricted SPEAR3 logbook (but not the restricted"
    echo "MCC and SSRL-BLDO logbooks)."
    echo "Copy Ed Guerra in the email since he is the 'gatekeeper'"
    echo "SPEAR3 logbook and it is possible that he might"
    echo "might question this user being allowed to make entries"
    echo "into the SPEAR3 logbook."
    echo " "
elif [ $authorization_type == SSRL-BLDO ]; then
    echo " "
    echo "Send email to the user notifying the user that he/she"
    echo "can now make entries into any nonrestricted logbook"
    echo "and the restricted SSRL-BLDO logbook (but not the restricted"
    echo "MCC and SPEAR3 logbooks)."
    echo "Copy Bart Johnson in the email since he is the 'gatekeeper'"
    echo "SSRL-BLDO logbook and it is possible that he might"
    echo "might question this user being allowed to make entries"
    echo "into the SSRL-BLDO logbook."
    echo " "
fi
