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

wait_for_user_to_press_n() {
    local key=
    until [ "${key}" == "n" ]; do
        read -s -p 'Press `n` to continue. ' -n 1 key
        echo
    done
    echo
}

cat <<'EOF'

This script assumes that `vdenv` has been started, and that `zimmer` is
running. It will then *destroy* your development/demo database, and
rebuild everything.

WARNING: Script is probably broken at the moment and may simply destroy
your database.

EOF
wait_for_user_to_press_n

# After setting up vdenv, do the following:

export DJANGO_SETTINGS_MODULE="maas.demo"

make distclean
make syncdb
make pserv-start
make txlongpoll-start
bin/maas createadmin \
    --username "${LOGNAME}" --password test \
    --email "${LOGNAME}@example.com"

# TODO: Set up virtual nodes.  We used to read them from Cobbler database
# here, as set up by vdenv.

make dbharness <<'EOF'
UPDATE maasserver_node
   SET owner_id = NULL, status = 4
 WHERE hostname LIKE 'odev-node%';
EOF

cat <<'EOF'

Next the MAAS server will be started, at which point you can Juju.

However, once MAAS is running, Juju needs to be pointed at it, and given
credentials with which to manipulate it:

  1. Copy an OAuth token from <http://localhost:5240/account/prefs/>.

  2. Interpolate it into the following:

     {{{
     juju: environments
     environments:
       maas:
         type: maas
         maas-server: 'http://localhost:5240'
         maas-oauth: '${OAUTH_TOKEN_FROM_STEP_1}'
         admin-secret: 'nothing'
         juju-origin: lp:maas
     }}}

  3. Customize `juju-origin` as desired.

  4. Put it into `~/.juju/environments.yaml`.

Be sure to check VNC output (via virsh). Go to vt7 for the boot log.

Good luck. You'll need it ;)

EOF
wait_for_user_to_press_n

make run
