#!/usr/bin/env bash
# Copyright 2016 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

# Exit immediately if a command exits with a non-zero status.
set -o errexit
# Treat unset variables as an error when substituting.
set -o nounset

CONTAINER=$1
if [ "$CONTAINER" == "" ]; then
    echo "Required argument: LXD container name"
    exit 1
fi

t=0
while [ "$(lxc exec $CONTAINER -- systemctl is-system-running | \
        sed 's/degraded/running/')" != "running" ]; do 
    sleep 0.1
    let t=$t+1 || true
done
let q=$t%4 || true
let t=$t/10 || true
echo "(waited $t.$q seconds for container startup)"
