set -eu
USER_STORE=/var/lib/dh-sysuser/user

home=%HOME%
username=%USERNAME%
package=%PACKAGE%

# Since according to POSIX, * glob expands to itself,
# if it fails to match anything, this function does not
# work properly, if $1 is empty.
count_files_in () {
	dir=$1
	set --
	for file in "$dir"/* ; do
		set -- "$@" "$file"
	done
	unset dir
	printf "%d" "$#"
}

store="$USER_STORE/$username"
mkdir -p "$store/_meta"
# -*- sh -*-

if [ "$1" = purge ] ; then
	rm -f "$store/$package"
	if [ $(count_files_in "$store") = 1 ] ; then
		deluser --remove-home "$username"
	fi
fi
