#!/bin/bash

IMAGE_DIR=/opt/solum/cedarish/image
BASE_IMAGE=

function check_docker () {
  sudo docker ps 2> /dev/null > /dev/null
  if [[ $? != 0 ]]; then
    echo "Cannot talk to Docker." >&2
    exit 1
  fi
}


function check_os_credentials () {
  if [[ -z "$OS_AUTH_TOKEN" ]]; then
    echo "OpenStack credentials not passed via ENV."
    exit 1
  fi
}

function check_glance_access () {
  glance index 2> /dev/null > /dev/null
  if [ $? != 0 ]; then
    echo "Cannot talk to Glance. Check your OpenStack credentials." >&2
    exit 1
  fi
}

function build_app () {
  echo "Building app..."
  local GIT_URL=$1
  local APP_DIR=$2

  mkdir -p $APP_DIR
  pushd $APP_DIR
    [[ -d build ]] && rm -rf build
    git clone $GIT_URL build
    if [[ ! -d build ]]; then
      echo "Git clone failed." >&2
      exit 1
    fi
    pushd build
      # Build the application slug
      local BUILD_ID=$(git archive master | sudo docker run -i -a stdin \
                       -v /opt/solum/cache:/tmp/cache:rw \
                       -v /opt/solum/buildpacks:/tmp/buildpacks:rw \
                       solum/slugbuilder)
      if [[ -z "$BUILD_ID" ]]; then
        echo "Docker build failed. Did not get a build ID." >&2
        exit 1
      fi
      sudo docker attach $BUILD_ID
    popd

    sudo docker cp $BUILD_ID:/tmp/slug.tgz $APP_DIR
    sudo docker rm $BUILD_ID
    rm -rf build
  popd
  echo "...done building."
}

function inject_app_into_image () {
  echo "Injecting app into image..."
  local APP_DIR=$1

  pushd $APP_DIR
    [[ -f image.qcow2 ]] || glance image-download --file image.qcow2 cedarish
    echo "Mounting image."
    mkdir -p mnt
    sudo guestmount -a image.qcow2 -i --rw mnt
    sudo mkdir -p mnt/app
    echo "Injecting app."
    sudo tar xzf slug.tgz -C mnt/app
    sudo umount mnt
  popd
  echo "...done injecting."
}

function upload_image_to_glance () {
  echo "Uploading to Glance..."
  local APP=$1
  local APP_DIR=$2

  glance image-delete $APP
  glance image-create --name $APP --disk-format qcow2 --container-format bare --file $APP_DIR/image.qcow2

  # TODO(asalkeld) use --property <key=value> to set the tags

  local IMAGE_ID=$(glance image-show $APP | grep " id " | cut -d"|" -f3 | tr -d " ")

  echo "created_image_id=$IMAGE_ID"
  echo "...done uploading."
}


function main () {
  local GIT_URL=$1
  shift
  local APP=$1
  shift
  local TENANT=$1
  shift
  BASE_IMAGE=$1
  shift

  local APP_DIR=/opt/solum/apps/$TENANT/$APP

  check_docker
  check_os_credentials
  check_glance_access
  build_app $GIT_URL $APP_DIR
  inject_app_into_image $APP_DIR
  upload_image_to_glance $APP $APP_DIR
}

if [[ -z "$1" ]] || [[ -z "$2" ]] || [[ -z "$3" ]]; then
  echo "Usage: $0 git_url appname tenant_id" >&2
  exit 1
fi

main $@
exit 0
