#!/bin/bash
# ------------------------------------------------------------------
# AUTHOR: [LucidLink Support]
# NAME: remove_premissions_script.sh
# VERSION: 1.0
# DESCRIPTION: Remove all users and group permissions (or shares).
#
# 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: ./remove_premissions_script.sh <fsrootpwd>
Request failed with: Bad Request
Empty required parameter 'password' is not allowed!"

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

# --- Identify Filespace format ---
PERMISSIONVER="2.2"
FSFORMAT="$(lucid2 status | grep "Filespace format:" | cut -f 2 -d \: | sed "s/ //g")"
if [ $(echo "$PERMISSIONVER > $FSFORMAT"|bc -l) -eq 1 ];
then COMMAND="share"; COLUMN="FSPATH;USERGROUP;TYPE;SHAREDAS;PERMISSIONS"; OPTION="--permissions";
else COMMAND="permission"; COLUMN="FSPATH;USERGROUP;TYPE;PERMISSIONS"; OPTION="--access"
fi

# --- Check if lucidpermissions.txt lucid_permissions_commands.txt lucid_permissions_output.txt exist ---
FILES="lucid_permissions.txt;lucid_permissions_commands.txt;lucid_permissions_output.txt"
for F in $FILES
do
if [ -f "$F" ]; then
echo -e "\e[31mError: $F exists. Please clean up any existing files.\e[0m"
exit
fi
done

# --- List permissions to temporary file ---
lucid2 $COMMAND --configured --password ''$1'' | sed -e 's/ \{2,\}/;/g;s/\[user\]/;user/g;s/\[group\]/;group/g;s/ ;/;/g;s/read, write/read,write/g' | tail -n +3 | head -n -1 > lucid_permissions.txt

# --- Delete permissions ---
while read -r $COLUMN; do
userassign=("lucid2 $COMMAND --delete '$FSPATH' --$TYPE '$USERGROUP' --password '$1'")
echo "$userassign" >> lucid_permissions_commands.txt
eval $userassign 2>&1 | tee -a lucid_permissions_output.txt
done < lucid_permissions.txt

exit 0