#!/bin/bash
# ------------------------------------------------------------------
# AUTHOR: [LucidLink Support]
# NAME: user_script.sh
# VERSION: 2.0
# DESCRIPTION: Lists users from Filespace instance 1 and imports
# into Filespace instance 2, assigning 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: ./user_script.sh <fs1rootpwd> <fs2rootpwd>
Request failed with: Bad Request
Empty required parameter 'password' is not allowed!"

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

# --- List instance 1 users, loop through users and create new users in instance 2 ---
lucid2 --instance 1 user --password ''$1'' | grep 'lucid\\' | grep -v 'lucid\\root' | cut -d ' ' -f 1 > lucid_users.txt
while read -r user; do
PASSWD=$(LC_ALL=C tr -dc 'A-Z' </dev/urandom|head -c 2)$(LC_ALL=C tr -dc 'a-z' </dev/urandom|head -c 2)$(LC_ALL=C tr -dc '1-9' </dev/urandom|head -c 4)
usrcreate=("lucid2 --instance 2 user --create '$user' --user-password $PASSWD --user-force-pwd-change --password '$2'")
echo "$usrcreate" >> lucid_user_commands.txt
eval $usrcreate 2>&1 | tee -a lucid_user_output.txt
done < lucid_users.txt
cat lucid_user_commands.txt | sed 's/^.*create/User:/;s/\--user-password*/Password:/;s/\--user-force.*//g' | column -t >> users_passwords.txt

# --- List user Admin role assigments in instance 1 and assign accordingly within instance 2 ---
lucid2 --instance 1 user --password ''$1'' | awk '{print $1,$NF}' | sed 's/user//g;s/ Administrator/;admin/g' | grep 'lucid\\' | grep -v 'lucid\\root' | grep ';admin' > lucid_users_role.txt
while read -r user role; do
usrcreate=("lucid2 --instance 2 user --set '$user' --add-role $role --password '$2'")
echo "$usrcreate" >> lucid_user_roles_commands.txt
eval $usrcreate 2>&1 | tee -a lucid_users_role_output.txt
done < lucid_users_role.txt

exit
