#!/bin/sh

<< MIT-LICENSE
Copyright (c) 2023 Jianjun Liu<jianjunliu@126.com> https://www.pkglinux.top

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
MIT-LICENSE

echo "Stopping all rc.d-boot services ..."
if [ -x /pkg/sbin/rc.d-boot ]; then
  /pkg/sbin/rc.d-boot stop
fi

# Run rc.shutdown after stopping rc.d-boot daemon
if [ -x /etc/rc.shutdown ]; then
	  /etc/rc.shutdown
fi

/etc/rc.d/rc ${@}

echo "Saving random seed ..."
/bin/dd if=/dev/urandom of=/var/tmp/random-seed count=1 &>/dev/null

echo "Sending all processes the TERM signal ..."
/sbin/killall5 -15
sleep 0.5

echo "Sending all processes the KILL signal ..."
/sbin/killall5 -9
sleep 0.5

echo "Deactivating swap partitions ..."
/sbin/swapoff -a

# Disable this to improve shutdown/reboot speed
#echo "Saving the system time to hardware clock ..."
#/sbin/hwclock --systohc

echo "Logging reboot/halt event ..."
case "$0" in
  *6)
    /sbin/reboot -w
    ;;
  *0)
    /sbin/halt -w
    ;;
esac

echo "Unmounting all other currently mounted file systems ..."
umount -a -d -r -t notmpfs,nosysfs,nodevtmpfs,noproc,nodevpts,noext4,nobtrfs

echo "Remounting root filesystems read-only ..."
/bin/mount -n -o remount,ro /
if mountpoint -q /bin; then
    echo "Found mountpoint /bin"
    /bin/mount -n -o remount,ro /bin
fi; if mountpoint -q /etc; then
    echo "Found mountpoint /etc"
    /bin/mount -n -o remount,ro /etc
fi; if mountpoint -q /lib; then
    echo "Found mountpoint /lib"
    /bin/mount -n -o remount,ro /lib
fi; if mountpoint -q /lib32; then
    echo "Found mountpoint /lib32"
    /bin/mount -n -o remount,ro /lib32
fi; if mountpoint -q /libx32; then
    echo "Found mountpoint /libx32"
    /bin/mount -n -o remount,ro /libx32
fi; if mountpoint -q /lib64; then
    echo "Found mountpoint /lib64"
    /bin/mount -n -o remount,ro /lib64
fi; if mountpoint -q /sbin; then
    echo "Found mountpoint /sbin"
    /bin/mount -n -o remount,ro /sbin
fi; if mountpoint -q /usr; then
    echo "Found mountpoint /usr"
    /bin/mount -n -o remount,ro /usr
fi

echo "Flushing filesystem buffers ..."
/bin/sync

case "$0" in
  *6)
    echo "Please stand by while rebooting ..."
    /sbin/reboot -d -f -i
    ;;
  *0)
    echo "Power off ..."
    /sbin/halt -d -f -i -p
    ;;
esac
