#! /bin/dash

### BEGIN INIT INFO
# Short-Description:    LXD - container startup/shutdown
# Description:          LXD - container startup/shutdown
# Provides:             lxd-containers
# Required-Start:       lxd
# Required-Stop:        lxd
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
### END INIT INFO

# NOTE -- This sysvinit script isn't as well tested as its systemd counterpart,
#         but was verified to work on a bookworm/sid system in September 2022.
#         The host system must have a cgroup v2 hierarchy mounted.

DAEMON=/usr/bin/lxd
DESC="LXD - container startup/shutdown"

. /lib/lsb/init-functions

test -f ${DAEMON} || exit 0

set -e

case "$1" in
    start)
        echo -n "Starting $DESC: "
        if ${DAEMON} activateifneeded >/dev/null 2>&1 ; then
            echo "(done)."
        else
            echo "(failed)."
            exit 1
        fi

        exit 0
    ;;

    stop)
        echo -n "Stopping $DESC: "
        if /usr/libexec/lxd/shutdown >/dev/null 2>&1 ; then
            echo "(done)."
        else
            echo "(failed)."
            exit 1
        fi

        exit 0
    ;;

    status)
        echo "There is no long-running daemon for this service, so there is no status."
        exit 0
    ;;

    restart|force-reload)
        $0 stop
        exec $0 start
    ;;

    *)
        echo "Usage: $0 {start|stop|restart|force-reload}" 1>&2
        exit 1
    ;;
esac
