#!/bin/sh -e

destination="/${__object_id:?}"

# shellcheck disable=SC2012
group_gid=$(ls -ldn "${destination}" | awk '{ print $4 }')

# NOTE: +1 because $((notanum)) prints 0.
if test $((group_gid + 1)) -ge 0
then
	group_should=$(cat "${__object:?}/parameter/group")

	if expr "${group_should}" : '[0-9]*$' >/dev/null
	then
		printf '%u\n' "${group_gid}"
	else
		if command -v getent >/dev/null 2>&1
		then
			getent group "${group_gid}" | cut -d : -f 1
		else
			awk -F: -v gid="${group_gid}" '$3 == gid { print $1 }' /etc/group
		fi
	fi
fi
