#!/bin/sh
if [ -z "$D" -a -f "/etc/init.d/avahi-daemon" -a -z "`systemctl is-enabled avahi-daemon 2>/dev/null`" ]; then
	/etc/init.d/avahi-daemon stop
fi
if type update-rc.d >/dev/null 2>/dev/null; then
	if [ -n "$D" ]; then
		OPT="-f -r $D"
	else
		OPT="-f"
	fi
	update-rc.d $OPT avahi-daemon remove
fi
bbnote () {
	echo "NOTE: $*"
}
bbwarn () {
	echo "WARNING: $*"
}
bbfatal () {
	echo "ERROR: $*"
	exit 1
}
perform_groupadd () {
	local rootdir="$1"
	local opts="$2"
	local retries="$3"
	bbnote "Performing groupadd with [$opts] and $retries times of retry"
	local groupname=`echo "$opts" | awk '{ print $NF }'`
	local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
	if test "x$group_exists" = "x"; then
		local count=0
		while true; do
			eval $PSEUDO groupadd $opts || true
			group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
			if test "x$group_exists" = "x"; then
				bbwarn "groupadd command did not succeed. Retrying..."
				sleep 1
			else
				break
			fi
			count=`expr $count + 1`
			if test $count = $retries; then
				bbfatal "Tried running groupadd command $retries times without scucess, giving up"
			fi
		done
	else
		bbwarn "group $groupname already exists, not re-creating it"
	fi
}
perform_useradd () {
	local rootdir="$1"
	local opts="$2"
	local retries="$3"
	bbnote "Performing useradd with [$opts] and $retries times of retry"
	local username=`echo "$opts" | awk '{ print $NF }'`
	local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
	if test "x$user_exists" = "x"; then
	       local count=0
	       while true; do
		       eval $PSEUDO useradd $opts || true
		       user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
		       if test "x$user_exists" = "x"; then
			       bbwarn "useradd command did not succeed. Retrying..."
			       sleep 1
		       else
			       break
		       fi
		       count=`expr $count + 1`
		       if test $count = $retries; then
				bbfatal "Tried running useradd command $retries times without scucess, giving up"
		       fi
	       done
	else
		bbwarn "user $username already exists, not re-creating it"
	fi
}
perform_groupmems () {
	local rootdir="$1"
	local opts="$2"
	local retries="$3"
	bbnote "Performing groupmems with [$opts] and $retries times of retry"
	local groupname=`echo "$opts" | awk '{ for (i = 1; i < NF; i++) if ($i == "-g" || $i == "--group") print $(i+1) }'`
	local username=`echo "$opts" | awk '{ for (i = 1; i < NF; i++) if ($i == "-a" || $i == "--add") print $(i+1) }'`
	bbnote "Running groupmems command with group $groupname and user $username"
	# groupmems fails if /etc/gshadow does not exist
	local gshadow=""
	if [ -f $rootdir/etc/gshadow ]; then
		gshadow="yes"
	else
		gshadow="no"
		touch $rootdir/etc/gshadow
	fi
	local mem_exists="`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*" $rootdir/etc/group || true`"
	if test "x$mem_exists" = "x"; then
		local count=0
		while true; do
			eval $PSEUDO groupmems $opts || true
			mem_exists="`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*" $rootdir/etc/group || true`"
			if test "x$mem_exists" = "x"; then
				bbwarn "groupmems command did not succeed. Retrying..."
				sleep 1
			else
				break
			fi
			count=`expr $count + 1`
			if test $count = $retries; then
				if test "x$gshadow" = "xno"; then
					rm -f $rootdir/etc/gshadow
					rm -f $rootdir/etc/gshadow-
				fi
				bbfatal "Tried running groupmems command $retries times without scucess, giving up"
			fi
		done
	else
		bbwarn "group $groupname already contains $username, not re-adding it"
	fi
	if test "x$gshadow" = "xno"; then
		rm -f $rootdir/etc/gshadow
		rm -f $rootdir/etc/gshadow-
	fi
}
OPT=""
SYSROOT=""

if test "x$D" != "x"; then
	# Installing into a sysroot
	SYSROOT="$D"
	OPT="--root $D"

	# Add groups and users defined for all recipe packages
	GROUPADD_PARAM=""
	USERADD_PARAM="--system --home /var/run/avahi-daemon                               --no-create-home --shell /bin/false                               --user-group avahi; --system --home /var/run/avahi-autoipd                               --no-create-home --shell /bin/false                               --user-group                               -c \"Avahi autoip daemon\"                               avahi-autoipd"
	GROUPMEMS_PARAM=""
else
	# Installing onto a target
	# Add groups and users defined only for this package
	GROUPADD_PARAM="${GROUPADD_PARAM}"
	USERADD_PARAM="--system --home /var/run/avahi-daemon                               --no-create-home --shell /bin/false                               --user-group avahi"
	GROUPMEMS_PARAM="${GROUPMEMS_PARAM}"
fi

# Perform group additions first, since user additions may depend
# on these groups existing
if test "x$GROUPADD_PARAM" != "x"; then
	echo "Running groupadd commands..."
	# Invoke multiple instances of groupadd for parameter lists
	# separated by ';'
	opts=`echo "$GROUPADD_PARAM" | cut -d ';' -f 1`
	remaining=`echo "$GROUPADD_PARAM" | cut -d ';' -f 2-`
	while test "x$opts" != "x"; do
		perform_groupadd "$SYSROOT" "$OPT $opts" 10
		if test "x$opts" = "x$remaining"; then
			break
		fi
		opts=`echo "$remaining" | cut -d ';' -f 1`
		remaining=`echo "$remaining" | cut -d ';' -f 2-`
	done
fi

if test "x$USERADD_PARAM" != "x"; then
	echo "Running useradd commands..."
	# Invoke multiple instances of useradd for parameter lists
	# separated by ';'
	opts=`echo "$USERADD_PARAM" | cut -d ';' -f 1`
	remaining=`echo "$USERADD_PARAM" | cut -d ';' -f 2-`
	while test "x$opts" != "x"; do
		perform_useradd "$SYSROOT" "$OPT $opts" 10
		if test "x$opts" = "x$remaining"; then
			break
		fi
		opts=`echo "$remaining" | cut -d ';' -f 1`
		remaining=`echo "$remaining" | cut -d ';' -f 2-`
	done
fi

if test "x$GROUPMEMS_PARAM" != "x"; then
	echo "Running groupmems commands..."
	# Invoke multiple instances of groupmems for parameter lists
	# separated by ';'
	opts=`echo "$GROUPMEMS_PARAM" | cut -d ';' -f 1`
	remaining=`echo "$GROUPMEMS_PARAM" | cut -d ';' -f 2-`
	while test "x$opts" != "x"; do
		perform_groupmems "$SYSROOT" "$OPT $opts" 10
		if test "x$opts" = "x$remaining"; then
			break
		fi
		opts=`echo "$remaining" | cut -d ';' -f 1`
		remaining=`echo "$remaining" | cut -d ';' -f 2-`
	done
fi
