#!/bin/sh
# nit-kernel-compat by Faheem Pervez <trippin1@gmail.com> 
# Mostly stolen from nitboot by Otto Solares <solca@guug.org> (http://guug.org/nit/nitboot/)
#
# Launcher script to boot nit's required proprietary software.

# nitboot configuration
NITBOOTDIR=
MNTDIR=${NITBOOTDIR}/mnt
BIN=${NITBOOTDIR}/bin
USRBIN=${NITBOOTDIR}/usr/bin
INITFS=${MNTDIR}/initfs
ROOTFS=${NITBOOTDIR}/rootfs
TRACE="${USRBIN}/strace -ff -F -tt -s 200 -o /tmp/trace"

# environment
PATH=/bin:/sbin:/usr/bin:/usr/sbin
LD_LIBRARY_PATH=/lib:/usr/lib
export PATH LD_LIBRARY_PATH

# power
mount -t sysfs sysfs /sys
echo 1 >/sys/power/sleep_while_idle
echo 1 >/sys/power/clocks_off_while_idle
echo ondemand >/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor

mkdir -p ${INITFS}
mount -t jffs2 -o ro /dev/mtdblock3 ${INITFS}
if [ "${?}" -ne "0" ]; then
	echo "nitboot: fail to mount initfs r/o."
	exit 1
fi

#mkdir -p ${ROOTFS}
#mount -t jffs2 -o ro /dev/block/mtdblock4 ${ROOTFS}
#if [ "${?}" -ne "0" ]; then
#	echo "nitboot: fail to mount rootfs r/o."
#	exit 1
#fi

# ls ${INITFS}

chroot ${INITFS} /bin/mount -n -t proc none /proc
if [ "${?}" -ne "0" ]; then
	echo "nitboot: fail to mount /proc on initfs."
	exit 1
fi

chroot ${INITFS} /bin/mount -n -t sysfs none /sys
if [ "${?}" -ne "0" ]; then
	echo "nitboot: fail to mount /sys on initfs."
	exit 1
fi

chroot ${INITFS} /bin/mount -n -t tmpfs none /tmp
if [ "${?}" -ne "0" ]; then
	echo "nitboot: fail to mount /tmp on initfs."
	exit 1
fi

chroot ${INITFS} /bin/mkdir -p /tmp/dev
if [ "${?}" -ne "0" ]; then
	echo "nitboot: fail to create /tmp/dev on initfs."
	exit 1
fi

chroot ${INITFS} /bin/mknod /tmp/dev/retu c 10 62
if [ "${?}" -ne "0" ]; then
	echo "nitboot: fail to create /tmp/dev/retu node on initfs."
	exit 1
fi

chroot ${INITFS} /bin/mknod /tmp/dev/tahvo c 10 63
if [ "${?}" -ne "0" ]; then
	echo "nitboot: fail to create /tmp/dev/tahvo node on initfs."
	exit 1
fi

PRODUCT=$(chroot ${INITFS} /bin/sh -c "/bin/grep product /proc/component_version | /usr/bin/cut -c 13-")
if [ "${?}" -ne "0" ]; then
	unset PRODUCT
	echo "nitboot: fail to determine nit product."
	exit 1
fi

chroot ${INITFS} /sbin/dsme -d -v 4 -p /usr/lib/dsme/libstartup.so
if [ "${?}" -ne "0" ]; then
	echo "nitboot: fail to launch dsme."
	exit 1
fi

chroot ${INITFS} /usr/sbin/waitfordsme
if [ "${?}" -ne "0" ]; then
	echo "nitboot: fail to wait for dsme."
	exit 1
fi

if ! [ -z "${PRODUCT}" ]; then
	chroot ${INITFS} /usr/sbin/dsmetool -o /usr/bin/bme_${PRODUCT}
	if [ "${?}" -ne "0" ]; then
		echo "nitboot: fail to launch bme."
		exit 1
	fi
fi

chroot ${INITFS} /usr/sbin/dsmetool -o /usr/sbin/kicker
if [ "${?}" -ne "0" ]; then
	echo "nitboot: fail to launch watchdog kicker."
	exit 1
fi

chroot ${INITFS} /usr/sbin/dsmetool --root-mounted

REASON=$(chroot ${INITFS} /usr/sbin/bootstate)
if [ "${?}" -ne "0" ]; then
	unset REASON
	echo "nitboot: fail to determine bootstate."
fi

if ! [ -z "${REASON}" ]; then
	chroot ${INITFS} /usr/bin/bt-cal "${REASON}"
	if [ "${?}" -ne "0" ]; then
		echo "nitboot: fail to calibrate bluetooth."
	fi
fi

chroot ${INITFS} /usr/bin/retutime -i
if [ "${?}" -ne "0" ]; then
	echo "nitboot: fail to load the time from retu-rtc."
fi

mount --bind ${INITFS}/tmp /tmp
if [ "${?}" -ne "0" ]; then
	echo "nitboot: failed to bind initfs's tmp to Mer's tmp."
fi

exec /sbin/init 2
