#!/bin/bash

Usage() {
   cat <<EOF
Usage: ${0##*/} config
  Test ec2-upload-bundle, ec2-delete-bundle

  Requires ec2-api-tools
EOF
}
[ "$1" = "-h" -o "$1" = "--help" ] && { Usage; exit 0; }

my_d=$(dirname $(readlink -f "${0##*/}"))
. "${my_d}/common" || { echo "failed to find 'common'"; exit 1; }

[ -f "$1" ] || { Usage 1>&2; fail "must give argument for config"; }

config "$1"

BUNDLE_D="$TEMP_D/bundle"
UNBUNDLE_D="$TEMP_D/unbundle"
tmpf="$TEMP_D/tmp"

run "clean tempdirs" rm -Rf "$BUNDLE_D" "$UNBUNDLE_D"
run "make dirs" mkdir -p "$BUNDLE_D" "$UNBUNDLE_D"
run "bundle-image $IMAGE" \
   ec2-bundle-image $KEY_ARGS --user $EC2_USER_ID \
   --destination "$BUNDLE_D" --arch "$ARCH" --image "$IMAGE"

last_region=""

# == Upload a bundle to multiple regions ==
for region in ${REGIONS}; do
  error "=== testing $region ==="
  loc_arg="--location=$region"
  [ "$region" = "us-east-1" ] && loc_arg="--location=US"
  [ "$region" = "eu-west-1" ] && loc_arg="--location=EU"
  bucket="${BUCKET_BASE}-$region"
  manifest="${IMAGE##*/}.manifest.xml"

  run "uploading to $bucket in $region with $loc_arg" \
    ec2-upload-bundle $SECRET_ARGS $loc_arg --bucket $bucket \
    --manifest "$BUNDLE_D/$manifest"
  CLEAN_DELETE_BUNDLE="$bucket $BUNDLE_D/$manifest"

  run "downloading bundle from $bucket" \
    ec2-download-bundle $SECRET_ARGS --privatekey=$EC2_PRIVATE_KEY \
      --bucket $bucket --manifest $manifest --directory "$UNBUNDLE_D"

  run "unbundling $manifest in $UNBUNDLE_D" \
    ec2-unbundle --privatekey $EC2_PRIVATE_KEY --manifest "$UNBUNDLE_D/$manifest" \
      --source "$UNBUNDLE_D" --destination "$UNBUNDLE_D"

  run "compare $IMAGE and $UNBUNDLE_D/${IMAGE##*/}" \
    cmp "$IMAGE" "$UNBUNDLE_D/${IMAGE##*/}"

  run "registering $bucket/$manifest in region $region" \
    ec2-register --region $region $XKEY_ARGS "$bucket/$manifest" > "$tmpf"
  read a ami_id < "$tmpf"
  CLEAN_DEREGISTER="$region $ami_id"

  error "registered as $ami_id"

  ##
  ## Below tests migrate-bundle, but that is failing for me running on precise
  ##  similar to as seen at https://forums.aws.amazon.com/thread.jspa?threadID=54372
  ## This is replaced by ec2-migrate-image, though
  ##
  #if [ -n "$last_region" ]; then
  #   error "testing migration $region to $last_region"
  #  
  #  lbucket="${BUCKET_BASE}-${last_region}"
  #  run "migrate-bundle $region to $last_region" \
  #    ec2-migrate-bundle $KEY_ARGS $SECRET_ARGS \
  #      --no-mapping \
  #      "--bucket=$bucket" \
  #      "--destination-bucket=$lbucket" $last_loc_arg \
  #      "--manifest=$manifest"
  #   CLEAN_DELETE_BUNDLE_2="$lbucket $BUNDLE_D/$manifest"
  #
  #  run "deleting migrated bundle $lbucket/$manifest" \
  #    ec2-delete-bundle $SECRET_ARGS --bucket "$lbucket" \
  #    --manifest "$BUNDLE_D/$manifest" --batch --yes --clear
  #fi

  run "deregister $ami_id $bucket/$manifest" \
    ec2-deregister --region $region $XKEY_ARGS "$ami_id"
  CLEAN_DEREGISTER=""

  run "deleting bundle for $bucket/$manifest" \
    ec2-delete-bundle $SECRET_ARGS --bucket $bucket \
    --manifest "$BUNDLE_D/$manifest" --batch --yes --clear
  CLEAN_DELETE_BUNDLE=""

  last_region="$region"
  last_loc_arg="${loc_arg}"

done
