#!/bin/bash

LOG=${SOLUM_BUILD_LOG:="/opt/stack/logs/solum_build.log"}
IMAGE_DIR=/opt/solum/cedarish/image

# TLOG, PRUN, etc. defined in common/utils
HERE=$(dirname $0)
source $HERE/../../common/utils

function check_docker () {
  PRUN silent docker ps
  [[ $? != 0 ]] && TLOG cannot talk to docker. && exit 1
}

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

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

function build_app () {
  TLOG "===>" 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
      TLOG Git clone failed.
      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
        TLOG Docker build failed. Did not get a build ID.
        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
  TLOG Done building
}

function inject_app_into_image () {
  TLOG Injecting app into image
  local APP_DIR=$1

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

function upload_image_to_glance () {
  TLOG 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 " ")

  TLOG created_image_id=$image_id

  # Need stdout for solum-worker to parse the image_id
  echo created_image_id=$image_id

  TLOG Done uploading
}


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

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

  check_docker
  check_os_credentials
  check_glance_access
  add_ssh_creds "$GIT_PRIVATE_KEY" "$APP_DIR"
  build_app $GIT_URL $APP_DIR
  remove_ssh_creds "$GIT_PRIVATE_KEY"
  inject_app_into_image $APP_DIR
  upload_image_to_glance $APP $APP_DIR
}

# Check command line arguments
if [ $# -lt 4 ]; then
  TLOG Usage: $0 git_url appname project_id base_image [git_private_key]
  exit 1
fi

main $@
exit 0
