#!/bin/sh

echo -n "Switching / to tmpfs..."
mount -t proc proc /proc
size="$(grep rootfssize= < /proc/cmdline | \
	sed 's/.*rootfssize=\([0-9]*[kmg%]\).*/-o size=\1/')"
free=$(busybox free | busybox awk '/Mem:/ { print int(($4*100)/$3) }')
umount /proc
[ -n "$size" ] || size="-o size=90%"
if [ $free -lt 100 ] || ! mount -t tmpfs $size tmpfs /mnt; then
	echo -e "\\033[70G[ \\033[1;33mSkipped\\033[0;39m]"
	exec /sbin/init
fi
for i in $(ls -a /); do
	case "$i" in
	.|..)	;;
	mnt)	mkdir /mnt/mnt;;
	*)	if ! cp -a /$i /mnt 2> /dev/null; then
			echo -e "\\033[70G[ \\033[1;31mFailed\\033[0;39m ]"
			umount /mnt
			exec /sbin/init
		fi;;
	esac
done
echo -e "\\033[70G[ \\033[1;33mOK\\033[0;39m ]"
exec /sbin/switch_root mnt /sbin/init
