#!/bin/sh
### BEGIN INIT INFO
# Provides:          umountfs
# Required-Start:
# Required-Stop:
# Default-Start:
# Default-Stop:      0 6
# Short-Description: Turn off swap and unmount all local file systems.
# Description:
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin

echo "Deactivating swap..."
swapoff -a

#read values from /data/reboot-cookie before unmount to check reboot condition
REBOOT_COOKIE=`cat /data/reboot-cookie`

# We leave /proc mounted.
echo "Unmounting local filesystems..."
grep -q /mnt/ram /proc/mounts && mount -o remount,ro /mnt/ram
sync
mount -o remount,ro /
echo "Remounted / as RO..."
sync
umount -f -a -r > /dev/null 2>&1
mount -t sysfs none /sys
echo "Running ubidetach commands..."
ubidetach -d 1
umount /sys -f
#if we are going for reboot, check reboot condition here
if [[ $REBOOT_COOKIE = 1 ]]; then
    sys_reboot bootloader
elif [[ $REBOOT_COOKIE = 2 ]]; then
    sys_reboot recovery
fi
: exit 0
