#!/bin/sh
#
# * $applet_list is generated by busybox_static.petbuild
#   after compiling a busybox compatible with the os arch
# * This script can be called from anywhere and will
#   always work in the directory it is located in
# * First try to use actual applet list, otherwise fall back to $applet_list
#
# Arguments:
# $1 = busybox binary to use
#

applet_list='[ [[ ash awk basename beep blkid bootchartd bzcat cat chgrp chmod chown chroot chrt clear cp cpio cttyhack cut date dc dd depmod df diff dirname dmesg dos2unix du echo eject elspci env expand expr false fbset fdisk find findfs flock free freeramdisk fuser guess_fstype gunzip gzip halt head hwclock init insmod iostat kill killall klogd less linux32 linux64 linuxrc ln loadfont loadkmap losetup ls lsmod lsof lspci lsusb lzcat lzopcat makedevs md5sum mdev mkdir mkdosfs mke2fs mkfifo mknod mkswap mktemp modinfo modprobe more mount mountpoint mpstat mv nice nmeter nohup nsenter pgrep pidof pivot_root pkill pmap poweroff printenv printf ps pwd raidautorun rdev readahead readlink realpath reboot reset resize rev rm rmdir rmmod sed seq setarch setconsole setfont sh sleep sort split stat stty swapoff swapon switch_root sync sysctl syslogd tac tail tar tee test time timeout top touch tr true tty ttysize uevent umount uname unexpand uniq unix2dos unlink unlzma unlzop unshare unxz uptime usleep waitmax watch wc which whoami xargs xz xzcat yes zcat '

sdir=$(dirname "$0")

if [ -x "$1" ] ; then
	# busybox path has been specified, not in initrd
	# ex: [chroot $NEWROOT] [$PATH_TO/]bb-create-symlinks /bin/busybox
	echo "Creating symlinks to $1 in $sdir"
	alist=$($1 --list)
	[ "x$alist" = "x" ] && alist="$applet_list"
	for a in $alist ; do
		[ ! -e "$a" ] && ln -s $1 "$sdir/$a"
	done
	exit 0
fi

#------------------------------------------------------
#                      initrd
#------------------------------------------------------

if [ -f "$sdir/busybox" ] ; then
	alist=$($sdir/busybox --list)
	[ "x$alist" = "x" ] && alist="$applet_list"
	for a in $alist ; do
		[ ! -e "$a" ] && ln -s busybox "$sdir/$a"
	done
fi

if [ -f "$sdir/exfatfsck" ] ; then
	ln -s exfatfsck "$sdir/fsck.exfat"
fi

if [ -f "$sdir/mount.exfat-fuse" ] ; then
	ln -s mount.exfat-fuse "$sdir/mount.exfat"
fi

if [ -f "$sdir/ntfs-3g" ] ; then
	ln -s ntfs-3g "$sdir/mount.ntfs"
fi

if [ -f "$sdir/fsck.fat" ] ; then
	ln -s fsck.fat "$sdir/fsck.vfat"
	ln -s fsck.fat "$sdir/fsck.msdos"
fi

if [ -f "$sdir/e2fsck" ] ; then
	ln -s e2fsck "$sdir/fsck.ext2"
	ln -s e2fsck "$sdir/fsck.ext3"
	ln -s e2fsck "$sdir/fsck.ext4"
	ln -s e2fsck "$sdir/fsck.ext4dev"
fi

if [ -f "$sdir/nano" ] ; then
	ln -sf nano "$sdir/pico"
	ln -sf nano "$sdir/mp"
	ln -sf nano "$sdir/ed"
fi

if [ -f "$sdir/kmod" ] ; then
	rm -f $sdir/{depmod,insmod,lsmod,modinfo,modprobe,rmmod} 2>/dev/null
	ln -sf kmod $sdir/depmod
	ln -sf kmod $sdir/insmod
	ln -sf kmod $sdir/lsmod
	ln -sf kmod $sdir/modinfo
	ln -sf kmod $sdir/modprobe
	ln -sf kmod $sdir/rmmod
fi

### END ###
