#!/bin/ash

# 默认使用关机命令
ACTION="poweroff"

# 获取shutdown参数
# Qemu-GA 默认调用 -h -P +0的方式
while [ -n "$1" ]; do
	case "$1" in
        -r)
        ACTION="reboot"
		shift
        ;;

        -k)
        ACTION="warning"
		shift
        ;;

        -h|-H)
        ACTION="halt"
		shift
        ;;

        -n)
        ACTION="poweroff"
		shift
        ;;

        -c)
        echo "Cancel shutdown, but not support the Openwrt!" > /dev/kmsg
        ACTION="stop"
		shift
        ;;

        -p|-P)
        ACTION="poweroff"
		shift
        ;;

        -*)
		shift
		;;

		*)
		break
		;;

	esac
done

if [ $# == 0 ]; then
    echo "Shutting down without any params" > /dev/kmsg
    /sbin/poweroff

elif [ "$ACTION" = "poweroff" ]; then
    /sbin/poweroff;

elif [ "$ACTION" = "reboot" ]; then
    /sbin/reboot

elif [ "$ACTION" = "warning" ]; then
    echo "关机警告">/dev/kmsg
    /sbin/poweroff;

elif [ "$ACTION" = "halt" ]; then
    /sbin/halt
fi
