#!/dis/sh -x -n
load std
x=$*
{~ $#x 1 && ~ ${hd $x} -h -r} || {
	echo usage: shutdown -h or shutdown -r >[1=2];
	exit usage
}
cd /

ns | grep '/n/rootdisk/dis /dis' &&
       unmount /n/rootdisk/dis /dis
unmount /n/rootdisk /
unmount /n/rootdisk

ftest -f /dis/disk/kfscmd.dis && {
	for(a in /chan/kfs.*.cmd){
		b=${hd ${tl ${split '.' $a}}}
		~ $"b '\*' || {
			disk/kfscmd -n $b sync # though there is no write buffering yet
			disk/kfscmd -n $b halt # only works for disk/kfs64
		}
	}
}
status=()

ns
if {~ $1 -r} {
	echo reboot >/dev/sysctl
} {~ $1 -h} {
	echo halt >/dev/sysctl
} {}
echo shutdown: failed: $status >[1=2]
exit failed
