#!/bin/sh -x

stop_service() {
    TIME_ELAPSED=0
    MAX_WAIT=10
    SLEEP_INTERVAL=2
    sv d "$1"
    while sv s "$1" | grep -q "^run" && [ "$TIME_ELAPSED" -lt "$MAX_WAIT" ]; do
        sleep "$SLEEP_INTERVAL"
        TIME_ELAPSED=`expr $TIME_ELAPSED + $SLEEP_INTERVAL`
    done
}

# Allow 2 seconds for the system to settle
sleep 2

stop_service "htc"
stop_service "startup-supervisor"
settings-reset
shutdown -r now
