#!/bin/bash -eu
#
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

function usage() {
  echo "Usage:"
  echo -e "$0 --image=<image-name> \\"
  echo -e "  --target_project=<target-project> \\"
  echo -e "  --source_project=<source-project> \\"
  echo -e "  --zone=<zone-name> \\"
  echo -e "- image-name:       name of the image to be copied"
  echo -e "- target-project:   name of the project to copy the image to"
  echo -e "- [source-project]: name of the project to copy the image from"
  echo -e "                    (default: gcloud config get-value project)"
  echo -e "- [zone-name]:      name of the zone to use for temp disk creation"
  echo -e "                    (default: gcloud config get-value compute/zone,"
  echo -e "                    us-central1-a if gcloud config value is missing)"
}

# set parameters
while [[ "${#}" -gt 0 ]] ; do
  case "${1}" in
    -i | --image)
      image="${2}"
      shift
      ;;
    -t | --target_project)
      dst_project="${2}"
      shift
      ;;
    -s | --source_project)
      src_project="${2}"
      shift
      ;;
    -z | --zone)
      zone="${2}"
      shift
      ;;
    -h | --help)
      usage
      exit 0
      ;;
  esac
  shift
done

# check required parameters
if [[ -z "${image:-}" || -z "${dst_project:-}" ]]; then
  echo "ERROR: One of the required parameters is missing."
  usage
  exit 1
fi

src_project="${src_project:-$(gcloud config get-value project)}"
if [[ -z "${src_project:-}" ]]; then
  echo "ERROR: source project could not be evaluated. Please use"
  echo "       --source_project flag or set a default project in gcloud."
  usage
  exit 1
fi

zone="${zone:-$(gcloud config get-value compute/zone)}"
zone="${zone:-us-central1-a}"

echo "Copying image \"${image}\" from project \"${src_project}\" \
to project \"${dst_project}\"..."

# check if image in source project exists
num_src_images_found=$(gcloud compute images list --filter="name ~ ^${image}\$" --format="get(name)" --project "${src_project}" | wc -l )
if [[ ${num_src_images_found} != 1 ]]; then
  echo "Error: image \"${image}\" could not be found in project \"${src_project}\"."
  exit 1
fi

# check if image in destination project does not exist
num_dst_images_found=$(gcloud compute images list --filter="name ~ ^${image}\$" --format="get(name)" --project "${dst_project}" | wc -l )
if [[ ${num_dst_images_found} != 0 ]]; then
  echo "Error: image \"${image}\" already exists in project \"${dst_project}\"."
  exit 1
fi

# create image copy
disk="image-copy-${image}-$(date +%Y%m%d-%H%M%S)"

echo "Creating temporary disk..."
gcloud compute disks create "${disk}" \
  --project="${dst_project}" \
  --zone "${zone}" \
  --image="https://www.googleapis.com/compute/v1/projects/${src_project}/global/images/${image}"

echo "Creating new copy of the image..."
gcloud compute images create "${image}" \
  --project="${dst_project}" \
  --source-disk="https://www.googleapis.com/compute/v1/projects/${dst_project}/zones/${zone}/disks/${disk}"

echo "Deleting temporary disk..."
gcloud compute disks delete "${disk}" \
  --project="${dst_project}" \
  --zone="${zone}" \
  --quiet

echo "Image copy creation finished."
