#!/bin/sh
# This part of code is dedicated to the on target upgrade problem.
	# It's known that if we don't make appropriate symlinks before update-alternatives calls,
	# there will be errors indicating missing commands such as 'sed'.
	# These symlinks will later be updated by update-alternatives calls.
	test -n 2 > /dev/null || alias test='busybox test'
	if test "x$D" = "x"; then
		# Remove busybox.nosuid if it's a symlink, because this situation indicates
		# that we're installing or upgrading to a one-binary busybox.
		if test -h /bin/busybox.nosuid; then
			rm -f /bin/busybox.nosuid
		fi
		for suffix in "" ".nosuid" ".suid"; do
			if test -e /etc/busybox.links$suffix; then
				while read link; do
					if test ! -e "$link"; then
						case "$link" in
							/*/*/*)
								to="../../bin/busybox$suffix"
								;;
							/bin/*)
								to="busybox$suffix"
								;;
							/*/*)
								to="../bin/busybox$suffix"
								;;
						esac
						# we can use busybox here because even if we are using splitted busybox
						# we've made a symlink from /bin/busybox to /bin/busybox.nosuid.
						busybox rm -f $link
						busybox ln -s $to $link
					fi
				done < /etc/busybox.links$suffix
			fi
		done
	fi
[ -z "$D" ] && PID=`pidof xinetd` && kill -HUP $PID || true
	update-alternatives --install /usr/bin/bunzip2 bunzip2 /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/bzcat bzcat /bin/busybox.nosuid 50
	update-alternatives --install /bin/date date /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/groups groups /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/id id /bin/busybox.nosuid 50
	update-alternatives --install /bin/touch touch /bin/busybox.nosuid 50
	update-alternatives --install /bin/base64 base64 /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/users users /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/who who /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/patch patch /bin/busybox.nosuid 50
	update-alternatives --install /bin/vi vi /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/find find /bin/busybox.nosuid 50
	update-alternatives --install /bin/grep grep /bin/busybox.nosuid 50
	update-alternatives --install /bin/egrep egrep /bin/busybox.nosuid 50
	update-alternatives --install /bin/fgrep fgrep /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/xargs xargs /bin/busybox.nosuid 50
	update-alternatives --install /bin/ash ash /bin/busybox.nosuid 50
	update-alternatives --install /bin/sh sh /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/[ lbracket /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/[[ [[ /bin/busybox.nosuid 50
	update-alternatives --install /usr/sbin/addgroup addgroup /bin/busybox.nosuid 50
	update-alternatives --install /usr/sbin/adduser adduser /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/ar ar /bin/busybox.nosuid 50
	update-alternatives --install /sbin/arp arp /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/awk awk /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/basename basename /bin/busybox.nosuid 50
	update-alternatives --install /usr/sbin/brctl brctl /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/bzip2 bzip2 /bin/busybox.nosuid 50
	update-alternatives --install /bin/cat cat /bin/busybox.nosuid 50
	update-alternatives --install /bin/chgrp chgrp /bin/busybox.nosuid 50
	update-alternatives --install /bin/chmod chmod /bin/busybox.nosuid 50
	update-alternatives --install /bin/chown chown /bin/busybox.nosuid 50
	update-alternatives --install /usr/sbin/chroot chroot /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/chvt chvt /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/clear clear /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/cmp cmp /bin/busybox.nosuid 50
	update-alternatives --install /bin/cp cp /bin/busybox.nosuid 50
	update-alternatives --install /bin/cpio cpio /bin/busybox.nosuid 50
	update-alternatives --install /usr/sbin/crond crond /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/cut cut /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/dc dc /bin/busybox.nosuid 50
	update-alternatives --install /bin/dd dd /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/deallocvt deallocvt /bin/busybox.nosuid 50
	update-alternatives --install /usr/sbin/delgroup delgroup /bin/busybox.nosuid 50
	update-alternatives --install /usr/sbin/deluser deluser /bin/busybox.nosuid 50
	update-alternatives --install /bin/df df /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/diff diff /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/dirname dirname /bin/busybox.nosuid 50
	update-alternatives --install /bin/dmesg dmesg /bin/busybox.nosuid 50
	update-alternatives --install /bin/dnsdomainname dnsdomainname /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/dos2unix dos2unix /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/du du /bin/busybox.nosuid 50
	update-alternatives --install /bin/dumpkmap dumpkmap /bin/busybox.nosuid 50
	update-alternatives --install /bin/echo echo /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/eject eject /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/env env /bin/busybox.nosuid 50
	update-alternatives --install /usr/sbin/ether-wake ether-wake /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/expr expr /bin/busybox.nosuid 50
	update-alternatives --install /bin/false false /bin/busybox.nosuid 50
	update-alternatives --install /usr/sbin/fbset fbset /bin/busybox.nosuid 50
	update-alternatives --install /sbin/fdisk fdisk /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/flock flock /bin/busybox.nosuid 50
	update-alternatives --install /bin/getopt getopt /bin/busybox.nosuid 50
	update-alternatives --install /sbin/getty getty /bin/busybox.nosuid 50
	update-alternatives --install /bin/gunzip gunzip /bin/busybox.nosuid 50
	update-alternatives --install /bin/gzip gzip /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/hd hd /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/head head /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/hexdump hexdump /bin/busybox.nosuid 50
	update-alternatives --install /bin/hostname hostname /bin/busybox.nosuid 50
	update-alternatives --install /sbin/ifconfig ifconfig /bin/busybox.nosuid 50
	update-alternatives --install /sbin/ifdown ifdown /bin/busybox.nosuid 50
	update-alternatives --install /sbin/ifup ifup /bin/busybox.nosuid 50
	update-alternatives --install /sbin/inotifyd inotifyd /bin/busybox.nosuid 50
	update-alternatives --install /bin/ionice ionice /bin/busybox.nosuid 50
	update-alternatives --install /sbin/ip ip /bin/busybox.nosuid 50
	update-alternatives --install /sbin/klogd klogd /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/less less /bin/busybox.nosuid 50
	update-alternatives --install /bin/ln ln /bin/busybox.nosuid 50
	update-alternatives --install /usr/sbin/loadfont loadfont /bin/busybox.nosuid 50
	update-alternatives --install /sbin/loadkmap loadkmap /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/logger logger /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/logname logname /bin/busybox.nosuid 50
	update-alternatives --install /sbin/losetup losetup /bin/busybox.nosuid 50
	update-alternatives --install /bin/ls ls /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/lspci lspci /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/lsusb lsusb /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/lzcat lzcat /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/lzma lzma /bin/busybox.nosuid 50
	update-alternatives --install /bin/lzop lzop /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/lzopcat lzopcat /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/md5sum md5sum /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/microcom microcom /bin/busybox.nosuid 50
	update-alternatives --install /bin/mkdir mkdir /bin/busybox.nosuid 50
	update-alternatives --install /sbin/mkdosfs mkdosfs /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/mkfifo mkfifo /bin/busybox.nosuid 50
	update-alternatives --install /sbin/mkfs.vfat mkfs.vfat /bin/busybox.nosuid 50
	update-alternatives --install /bin/mknod mknod /bin/busybox.nosuid 50
	update-alternatives --install /sbin/mkswap mkswap /bin/busybox.nosuid 50
	update-alternatives --install /bin/mktemp mktemp /bin/busybox.nosuid 50
	update-alternatives --install /bin/more more /bin/busybox.nosuid 50
	update-alternatives --install /bin/mountpoint mountpoint /bin/busybox.nosuid 50
	update-alternatives --install /bin/mv mv /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/nc nc /bin/busybox.nosuid 50
	update-alternatives --install /bin/netstat netstat /bin/busybox.nosuid 50
	update-alternatives --install /bin/nice nice /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/nohup nohup /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/nslookup nslookup /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/od od /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/openvt openvt /bin/busybox.nosuid 50
	update-alternatives --install /bin/pidof pidof /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/printf printf /bin/busybox.nosuid 50
	update-alternatives --install /bin/pwd pwd /bin/busybox.nosuid 50
	update-alternatives --install /usr/sbin/rdate rdate /bin/busybox.nosuid 50
	update-alternatives --install /usr/sbin/readahead readahead /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/readlink readlink /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/realpath realpath /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/renice renice /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/reset reset /bin/busybox.nosuid 50
	update-alternatives --install /usr/sbin/rfkill rfkill /bin/busybox.nosuid 50
	update-alternatives --install /bin/rm rm /bin/busybox.nosuid 50
	update-alternatives --install /bin/rmdir rmdir /bin/busybox.nosuid 50
	update-alternatives --install /sbin/route route /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/run-parts run-parts /bin/busybox.nosuid 50
	update-alternatives --install /bin/sed sed /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/seq seq /bin/busybox.nosuid 50
	update-alternatives --install /sbin/setconsole setconsole /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/setsid setsid /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/sha1sum sha1sum /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/sha3sum sha3sum /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/sha256sum sha256sum /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/sha512sum sha512sum /bin/busybox.nosuid 50
	update-alternatives --install /bin/sleep sleep /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/sort sort /bin/busybox.nosuid 50
	update-alternatives --install /sbin/start-stop-daemon start-stop-daemon /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/stat stat /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/strings strings /bin/busybox.nosuid 50
	update-alternatives --install /bin/stty stty /bin/busybox.nosuid 50
	update-alternatives --install /sbin/sulogin sulogin /bin/busybox.nosuid 50
	update-alternatives --install /sbin/swapoff swapoff /bin/busybox.nosuid 50
	update-alternatives --install /sbin/swapon swapon /bin/busybox.nosuid 50
	update-alternatives --install /sbin/switch_root switch_root /bin/busybox.nosuid 50
	update-alternatives --install /bin/sync sync /bin/busybox.nosuid 50
	update-alternatives --install /sbin/syslogd syslogd /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/tail tail /bin/busybox.nosuid 50
	update-alternatives --install /bin/tar tar /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/taskset taskset /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/tee tee /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/telnet telnet /bin/busybox.nosuid 50
	update-alternatives --install /usr/sbin/telnetd telnetd /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/test test /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/tftp tftp /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/time time /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/timeout timeout /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/tr tr /bin/busybox.nosuid 50
	update-alternatives --install /bin/true true /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/tty tty /bin/busybox.nosuid 50
	update-alternatives --install /sbin/tunctl tunctl /bin/busybox.nosuid 50
	update-alternatives --install /sbin/udhcpc udhcpc /bin/busybox.nosuid 50
	update-alternatives --install /bin/umount umount /bin/busybox.nosuid 50
	update-alternatives --install /bin/uname uname /bin/busybox.nosuid 50
	update-alternatives --install /bin/uncompress uncompress /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/uniq uniq /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/unix2dos unix2dos /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/unxz unxz /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/unlzma unlzma /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/unlzop unlzop /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/unzip unzip /bin/busybox.nosuid 50
	update-alternatives --install /bin/usleep usleep /bin/busybox.nosuid 50
	update-alternatives --install /sbin/vconfig vconfig /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/wc wc /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/wget wget /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/which which /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/whoami whoami /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/xzcat xzcat /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/xz xz /bin/busybox.nosuid 50
	update-alternatives --install /usr/bin/yes yes /bin/busybox.nosuid 50
	update-alternatives --install /bin/zcat zcat /bin/busybox.nosuid 50
	update-alternatives --install /bin/ping ping /bin/busybox.suid 50
	update-alternatives --install /bin/ping6 ping6 /bin/busybox.suid 50
	update-alternatives --install /usr/bin/crontab crontab /bin/busybox.suid 50
	update-alternatives --install /bin/login login /bin/busybox.suid 50
	update-alternatives --install /usr/bin/passwd passwd /bin/busybox.suid 50
	update-alternatives --install /bin/su su /bin/busybox.suid 50
	update-alternatives --install /usr/bin/traceroute traceroute /bin/busybox.suid 50
	update-alternatives --install /usr/bin/traceroute6 traceroute6 /bin/busybox.suid 50
OPTS=""

if [ -n "$D" ]; then
    OPTS="--root=$D"
fi

if type systemctl >/dev/null 2>/dev/null; then
	systemctl $OPTS enable busybox-telnetd.socket

	if [ -z "$D" -a "enable" = "enable" ]; then
		systemctl restart busybox-telnetd.socket
	fi
fi
