#!/bin/bash
# Copyright 2012-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

# Remember where here is.
here="$(pwd)"

# Move to the project root.
cd "$(dirname "$0")/../.."

# Start logging, if requested. Not using multilog here right now
# because there are race issues when restarting.
[ -z "${logdir:-}" ] || exec &>> "${logdir}/current"

# Configure the rack's UUID to match sampledata, and also use a high
# port for TFTP to match this branch's etc/services.
bin/maas-rack config \
    --uuid adfd3977-f251-4f2c-8d61-745dbd690bf2 \
    --tftp-port 5244

# Wait for the installation of the shared-secret.
until bin/maas-rack check-for-shared-secret; do sleep 2; done

# Exec the Provisioning Server.
script="$(readlink -f bin/twistd.rack)"

exec $(command -v authbind && echo --deep) \
    "${script}" --nodaemon --pidfile="" maas-rackd
