# Get all entries between "permissions:" and "relay:" keys, remove the role part, remove commented parts, format it with newlines and clean whitespaces and double quotes.
allDefinedEntries=$(awk '/permissions:/{flag=1; next} /relay:/{flag=0} flag'"$install_dir/config.yaml"| sed "/: $role/d"| sed -r 's/: (admin|user|relay)//'| tr -d '[:blank:]'| sed '/^#/d'| tr -d '\"'| tr ',''\n')
# Delete everything from the corresponding role to insert the new defined values. This way we also handle deletion of users.
sed -i "/permissions:/,/relaybot:/{/: $role/d;}""$install_dir/config.yaml"
# Ensure that entries with value surrounded with quotes are deleted too. E.g. "users".
sed -i "/permissions:/,/relaybot:/{/: \"$role\"/d;}""$install_dir/config.yaml"
for user in "${usersArray[@]}"
do
if grep -q -x "${user}"<<<"$allDefinedEntries"
then
ynh_print_info "User $user already defined in another role."
else
sed -i "/permissions:/a \ \\\"$user\": $role""$install_dir/config.yaml"# Whitespaces are needed so that the file can be correctly parsed
fi
done
fi
set +o noglob
ynh_print_info "Users with role $role added in $install_dir/config.yaml"