#!/bin/bash

BOOT_LOG=/var/log/boot.log
TMP_BOOT_LOG=/tmp/_boot.log
PID_FILE=/tmp/rcS.pid

/etc/init.d/mountsys

set -a
test -s /etc/environment && source /etc/environment
test -s /data/etc/environment && source /data/etc/environment
test -s /boot/etc/environment && source /boot/etc/environment
set +a

source /etc/init.d/base

function run_init() {
    echo "---- booting ${OS_NAME} ${OS_VERSION} ----"

    # Start all init scripts in /etc/init.d, executing them in numerical order.
    for i in /etc/init.d/S??* /etc/init.d/bootdone; do
        if ! [[ -x "${i}" ]]; then continue; fi
        if [[ -f /data/etc/no_$(basename ${i}) ]]; then continue; fi
        ${i} start
    done
}

(run_init & echo $! > ${PID_FILE}) | tee -a ${TMP_BOOT_LOG} &

pid=$(cat ${PID_FILE})
while kill -0 ${pid} &>/dev/null; do
    sleep 1
done

# Check for data partition availability and only then move the boot log
test -d $(dirname ${BOOT_LOG}) && cat ${TMP_BOOT_LOG} >> ${BOOT_LOG}
