#!/bin/ash

# create symlinks
/bin/busybox --list | \
while read applet
do
	[ ! -e /bin/$applet ] && /bin/busybox ln -s busybox /bin/$applet
done
[ -f "/bin/exfatfsck" ] && ln -s exfatfsck "/bin/fsck.exfat" 2>/dev/null
[ -f "/bin/mount.exfat-fuse" ] && ln -s mount.exfat-fuse "/bin/mount.exfat" 2>/dev/null
[ -f "/bin/ntfs-3g" ] && ln -s ntfs-3g "/bin/mount.ntfs" 2>/dev/null
[ -f "/bin/fsck.fat" ] && ln -s fsck.fat "/bin/fsck.vfat" 2>/dev/null
if [ -f "/bin/e2fsck" ] ; then 2>/dev/null
	ln -s e2fsck "/bin/fsck.ext2" 2>/dev/null
	ln -s e2fsck "/bin/fsck.ext3" 2>/dev/null
	ln -s e2fsck "/bin/fsck.ext4" 2>/dev/null
fi

# create essential dirs
mkdir -p /dev /mnt/tmpfs /proc /sys /tmp
mkdir -p /lib/modules
mkdir -p /pup_new /pup_a /pup_f /pup_y /pup_z /pup_ro1 /pup_ro2

# mount essential stuff
mount -t proc none /proc
mount -t sysfs none /sys
mount -t rootfs -o remount,rw rootfs /

mount -t devtmpfs devtmpfs /dev 2>/dev/null

# got a kernel panic in dpup stretch with debian kernel 4.9
# this solved the issue
sleep 0.5

# this is not required for huge kernels and actually fails
# but it's needed by other type of kernels for the usb stuff to work
mkdir -p /proc/bus/usb 2>/dev/null
mount -t usbfs none /proc/bus/usb 2>/dev/null

ln -s /proc/mounts /etc/mtab 2>/dev/null

# loop1-15
for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
	[ -e /dev/loop${i} ] && continue
	mknod /dev/loop${i} b 7 $i
done

# busybox 1.25 losetup somehow requires /dev/loop/X in the initrd to work..
# edit: only if CONFIG_FEATURE_DEVFS is enabled

### END ###
