#!/bin/bash
# ------------------------------------------------------------------
# AUTHOR: [LucidLink Support]
# NAME: jit_users_script.sh
# VERSION: 1.0.1
# DESCRIPTION: Single sign-on user import script. Creates users
# and assigns Admin roles.
#
# THE SCRIPT IS PROVIDED “AS IS” AND “AS AVAILABLE” AND IS WITHOUT
# WARRANTY OF ANY KIND. PLEASE REVIEW ALL TERMS AND CONDITIONS.
# https://www.lucidlink.com/legal-documents
# ------------------------------------------------------------------

IFS=";"
USAGE="Usage: ./jit_users_script.sh <password>
Request failed with: Bad Request
Empty required parameter 'password' is not allowed!"

# --- Ensure password ---
if [ $# == 0 ] ; then
    echo "$USAGE"
    exit 1;
fi

# --- Loop through user backup substitute SSO provider and import ---
cat jit_users_backup.txt | cut -d ' ' -f 1 | grep -v 'lucid\\' | grep 'sso\\' > jit_users_import.txt
while read -r user; do
usrcreate=("lucid user --create '$user' --password '$1'")
echo "$usrcreate" >> jit_users_commands.txt
eval $usrcreate 2>&1 | tee -a jit_users_output.txt
done < jit_users_import.txt

# --- Loop through user backup assigning Admin roles to users ---
cat jit_users_backup.txt | awk '{print $1,$NF}' | sed 's/ user//g;s/ Administrator/;admin/g' | grep -v 'lucid\\' | grep ';admin' | grep 'sso\\' > jit_users_roles_import.txt
while read -r user role; do
usrcreate=("lucid user --set '$user' --add-role $role --password '$1'")
echo "$usrcreate" >> jit_users_roles_commands.txt
eval $usrcreate 2>&1 | tee -a jit_users_roles_output.txt
done < jit_users_roles_import.txt

exit