#!/bin/bash

BOOT_LOG=/var/log/boot.log
PID_FILE=/tmp/rcK.pid

source /etc/init.d/base

function run_shutdown() {
    echo -e "\n---- shutting down ${OS_NAME} ${OS_VERSION} ----"

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

(run_shutdown & echo $! > ${PID_FILE}) | tee -a ${BOOT_LOG} &

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

/bin/umount -a -r
/sbin/swapoff -a
