#!/bin/bash
# ------------------------------------------------------------------
# AUTHOR: [LucidLink Support]
# NAME: user_group_script.sh
# VERSION: 1.0.2
# DESCRIPTION: Lists users group assignments in Filespace instance 1
# and assign accordingly in Filespace instance 2
#
# 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
# ------------------------------------------------------------------

USAGE="Usage: ./user_group_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 and their groups, recreate and assign instance 2 ---
lucid2 --instance 1 group --password ''$1'' | tr -s " " | column -t | awk '{gsub(/,/,ORS $1 OFS)} 1' | grep 'lucid\\' > lucid_groups_users.txt
while read -r group user; do
groupassign=("lucid2 --instance 2 user --set '$user' --add-group '$group' --password '$2'")
echo "$groupassign" >> lucid_user_group_commands.txt
eval $groupassign 2>&1 | tee -a lucid_user_groups_output.txt
done < lucid_groups_users.txt

exit
