#!/bin/busybox sh

# remove links that has been relocated or removed
# - generate a list of all symlinks in bin dirs and a list with all busybox
#   applets
# - sort the list and print all lines that are unique. This is the list of
#   symlinks that does not exist in the busybox applets list.
# - use stat to find the target of those symlinks printed as:
#     '/path/to/symlink' -> '/path/to/target'
#   The "2>/dev/null" is used to stop "can't stat" warnings appearing on
#   the console for each new command added by the new version of the
#   busybox package.
# - use awk to print the symlinks that points to '/bin/busybox'
# - use sed to strip the leading and training ' chars
# - finally pass the list to rm for removing the symlinks that points to
#   busybox but does not exist in the `busybox --list-all` list
{ find bin sbin usr/bin usr/sbin -maxdepth 1 -type l; busybox --list-all; } \
	| sort | uniq -u \
	| xargs -r stat -c "%N" 2>/dev/null \
	| awk "\$3 == \"'/bin/busybox'\" {print \$1}" \
	| sed "s/^'//; s/'$//" \
	| xargs -r rm

for link in /bin/ping /bin/ping6; do
	if [ -L "$link" ] && [ "$(readlink $link)" = "/bin/bbsuid" ]; then
		rm "$link"
	fi
done

# We need the symlinks early
/bin/busybox --install -s || exit 1

# can't be in pre-upgrade since adduser/group may not available then
addgroup -S klogd 2>/dev/null
adduser -S -D -H -h /dev/null -s /sbin/nologin -G klogd -g klogd klogd 2>/dev/null

exit 0
