#!/bin/bash

DIR=`dirname $0`

if [[ -z $OS_USERNAME ]]; then
  echo 'openstack credentials not passed via ENV. hunting for openrc.'
  [[ -f ./openrc ]] && . ./openrc
  [[ -f ~/devstack/openrc ]] && . ~/devstack/openrc
fi

glance image-list 2> /dev/null > /dev/null
if [ $? != 0 ]; then
  echo 'cannot talk to glance. check your openstack credentials'
  exit 1
fi

sudo mkdir -p /opt/solum/cedarish
sudo chown $USER /opt/solum/cedarish
IMAGE_DIR=/opt/solum/cedarish/image

mkdir -p $IMAGE_DIR

if [[ -f $IMAGE_DIR/cedarish.qcow2 ]]; then
  echo 'you already have the cedarish image.'
else
  echo 'downloading cedarish image.  this might take some time.'
  wget -O $IMAGE_DIR/cedarish.qcow2 \
    http://cc42a68525a2dda0151c-9a7653a0ba84bd9342f239dc5349667e.r38.cf1.rackcdn.com/cedarish.qcow2
fi

if [[ ! -f $IMAGE_DIR/cedarish.qcow2 ]]; then
  echo something went wrong building image
  exit 1
fi

echo image: $IMAGE_DIR/cedarish.qcow2
