#!/bin/bash

XFSTESTS_FLAVOR=gce
DIR=..
if test -n "$GCE_XFSTESTS_DIR"
then
    DIR="$GCE_XFSTESTS_DIR"
else
    DIR="$(dirname "$(dirname "$0")")"
fi
. "$DIR/util/get-config"
. "$DIR/util/arch-funcs"

if test -z "$GS_BUCKET" -o -z "$GCE_PROJECT" -o -z "$GCE_ZONE"
then
	echo "You must configure GS_BUCKET, GCE_PROJECT, and GCE_ZONE in"
	echo "your config file"
	exit 1
fi

if [ -n "$GCE_IMAGE_PROJECT_RELEASE" ]
then
    SRC_PROJECT="$GCE_PROJECT"
    DEST_PROJECT="$GCE_IMAGE_PROJECT_RELEASE"
else
    SRC_PROJECT="${GCE_IMAGE_PROJECT:-xfstests-cloud}"
    DEST_PROJECT="$GCE_PROJECT"
fi
if [ "$SRC_PROJECT" = "$DEST_PROJECT" ]
then
    # Hmm, we can't pick good defaults
    SRC_PROJECT=
    DEST_PROJECT=
fi
SRC_FAMILY=__unspec
DEST_FAMILY=__unspec
SRC_IMAGE=
DEST_IMAGE=

while [ "$1" != "" ];
do
    case $1 in
	--arch) shift
	    ARCH="$1"
	    ;;
	--arch=*)
	    ARCH=$(echo "$1" | sed 's/--arch=//')
	    ;;
	--src_project)
	    shift
	    SRC_PROJECT="$1"
	    ;;
	--src-image) shift
	    SRC_IMAGE="$1"
	    SRC_FAMILY=
	    ;;
	--src-family) shift
	    SRC_FAMILY="$1"
	    ;;
	--dest_project) shift
	    DEST_PROJECT="$1"
	    ;;
	--dest-image) shift
	    DEST_IMAGE="$1"
	    ;;
	--dest-family) shift
	    if [ "$1" = "none" ]
	    then
		DEST_FAMILY=
	    else
		DEST_FAMILY="$1"
	    fi
	    ;;
	--account) shift
	    ACCOUNT="--account $1"
	    ;;
	--no-action)
	    NOACTION=echo
	    ;;
	*)
	    break
	    ;;
	-*)
	    echo "unknown option: $1"
	    exit 1
	    ;;
    esac
    shift
done

set_canonicalized_arch "$ARCH"

if [ "$SRC_FAMILY" = "__unspec" ]
then
    SRC_FAMILY="xfstests-$ARCH"
fi

if [ "$DEST_FAMILY" = "__unspec" ]
then
    DEST_FAMILY="xfstests-$ARCH"
fi

if [ -z "$SRC_PROJECT" ]
then
    echo "Source project not specified"
    exit 1
fi

if [ -z "$DEST_PROJECT" ]
then
    echo "Source project not specified"
    exit 1
fi

if [ -z "$SRC_IMAGE" ]
then
    if [ -z "$SRC_FAMILY" ]
    then
	echo "No source image or image family available"
	exit 1
    fi
    SRC_IMAGE=$(run_gcloud_prj compute images describe-from-family "$SRC_FAMILY" \
		       --project "$SRC_PROJECT" --format="value(name)")
fi

if [ -z "$DEST_IMAGE" ]
then
    DEST_IMAGE="$SRC_IMAGE"
fi

TEMP_DISK="temp-$DEST_IMAGE"

if run_gcloud_prj compute images describe "$DEST_IMAGE" \
	  --project "$DEST_PROJECT" >& /dev/null
then
    echo -e "Error: image $DEST_PROJECT/$DEST_IMAGE already exists.\n"
    exit 1;
fi

if run_gcloud_prj compute disks describe "TEMP_DISK" \
	  --project "$DEST_PROJECT" >& /dev/null
then
    echo -e "Error: disk $DEST_PROJECT/$TEMP_DISK already exists.\n"
    exit 1;
fi

echo "About to copy $SRC_PROJECT/$SRC_IMAGE to $DEST_PROJECT/$DEST_IMAGE..."
if [ -n "$DEST_FAMILY" ]
then
   echo "Destination family: $DEST_FAMILY"
fi

if [ -z "$NOACTION" ]
then
    sleep 3
fi

json=$(run_gcloud_prj compute images describe "$SRC_IMAGE" --project "$SRC_PROJECT" \
	      --format=json)
DESCRIPTION=$(echo $json | jq -r ".description")
keys=$(echo $json | jq -r ".labels | keys[]")

for k in $keys
do
    v=$(echo $json | jq -r ".labels[\"$k\"]")
    if [ -n "$LABELS" ] ; then
	LABELS="$LABELS,$k=$v"
    else
	LABELS="$k=$v"
    fi
done

if [ -n "$DEST_FAMILY" ]
then
    FAMILY="--family $DEST_FAMILY"
fi

if [ -n "$LABELS" ]
then
    LABELS="--labels $LABELS"
fi

echo -e "\nCreating disk $TEMP_DISK..."

NO_ACTION="$NOACTION"
run_gcloud_prj compute disks create "$TEMP_DISK" \
	  --project "$DEST_PROJECT"  --image-project "$SRC_PROJECT" \
	  --image $SRC_IMAGE --zone "$GCE_ZONE" $ACCOUNT

echo -e "\nCreating image $DEST_IMAGE..."

run_gcloud_prj compute --project "$DEST_PROJECT" -q images create \
	  "$DEST_IMAGE" --description "$DESCRIPTION" \
	  --source-disk "$TEMP_DISK" --source-disk-zone "$GCE_ZONE" \
	   $FAMILY $LABELS $ACCOUNT

echo -e "\nDeleting disk $TEMP_DISK..."
run_gcloud_prj compute --project "$DEST_PROJECT" -q disks delete \
	  "$TEMP_DISK" --zone "$GCE_ZONE" $ACCOUNT >& /dev/null &

if [ -n "$DEST_FAMILY" ]
then
    echo -e "\nListing images in $DEST_FAMILY..."
    run_gcloud_prj compute --project "$DEST_PROJECT" -q images list \
	      --filter="family=$DEST_FAMILY"
fi
