#!/bin/sh
# It's very important to set user/group correctly.

mastodon_dir='/var/lib/mastodon'

if ! getent group mastodon 1>/dev/null; then
	echo '* Creating group mastodon' 1>&2

	addgroup -S mastodon
fi

if ! id mastodon 2>/dev/null 1>&2; then
	echo '* Creating user mastodon' 1>&2

	adduser -DHS -G mastodon -h "$mastodon_dir" -s /bin/sh  \
		-g "added by apk for mastodon" mastodon
	passwd -u mastodon 1>/dev/null  # unlock
fi

if ! id -Gn mastodon | grep -Fq redis; then
	echo '* Adding user mastodon to group redis' 1>&2

	addgroup mastodon redis
fi

if [ "$(id -gn mastodon)" != 'mastodon' ]; then
	cat >&2 <<-EOF
	!!
	!! User mastodon has primary group $(id -gn mastodon). We strongly recommend to change
	!! mastodon's primary group to mastodon.
	!!
	EOF

	# Add it at least as a supplementary group.
	adduser mastodon mastodon
fi

user_home="$(getent passwd mastodon | cut -d: -f6)"

if [ "$user_home" != "$mastodon_dir" ]; then
	cat >&2 <<-EOF
	!!
	!! User mastodon has home directory in $user_home, but this package assumes
	!! $mastodon_dir. Although it's possible to use a different directory,
	!! it's really not easy.
	!!
	!! Please change mastodon's home directory to $mastodon_dir, or adjust settings
	!! and move files yourself. Otherwise Mastodon will not work!
	!!
	EOF
fi

exit 0