#!/bin/bash

. /usr/local/lib/gce-funcs
. /root/test-config
. /run/test-env

logger -i "Entered setup scratch"

set -vx
SCRATCH_DSK=$instance-scratch
CREATED_SCRATCH_DEV=/dev/disk/by-id/google-scratch

# Use local SSD if available
if test -e /dev/disk/by-id/google-local-ssd-0
then
    CREATED_SCRATCH_DEV=/dev/disk/by-id/google-local-ssd-0
elif test -e /dev/disk/by-id/google-local-nvme-ssd-0
then
    CREATED_SCRATCH_DEV=/dev/disk/by-id/google-local-nvme-ssd-0
fi

if ! test -e $CREATED_SCRATCH_DEV
then
    SCOPE=zonal
    TYPE=pd-ssd
    SIZE=100

    SPEC_ARRAY=(${DISK_SPEC//\// })

    if test "${#SPEC_ARRAY[@]}" -ge 3 -o "${SPEC_ARRAY[0]}" = zonal -o \
       "${SPEC_ARRAY[0]}" = regional ; then
	case "${SPEC_ARRAY[0]}" in
	    zonal|regional)
		SCOPE="${SPEC_ARRAY[0]}"
		;;
	    *)
		;;
	esac
	SPEC_ARRAY=("${SPEC_ARRAY[@]:1}")
    fi

    if test -n "${SPEC_ARRAY[0]}" ; then
	case "${SPEC_ARRAY[0]}" in
	    balanced|extreme|ssd|standard)
		TYPE="pd-${SPEC_ARRAY[0]}"
		;;
	    pd-balanced|pd-extreme|pd-ssd|pd-standard)
		TYPE="${SPEC_ARRAY[0]}"
		;;
	    *)
		;;
	esac
    fi

    if test -n "${SPEC_ARRAY[1]}" ; then
	num="${SPEC_ARRAY[1]}"
	if [[ "$num" =~ ^[0-9]+$ ]] || [[ "$num" =~ ^[0-9]+[GT]B$ ]] ; then
	    SIZE="$num"
	fi
    fi

    if test "$SCOPE" = "regional" ; then
	if test -n "$GCE_ZONE2" -a "$ZONE" != "$GCE_ZONE" ; then
	    Z2="$GCE_ZONE"
	elif test -n "$GCE_ZONE2" -a "$ZONE" != "$GCE_ZONE2" ; then
	    Z2="$GCE_ZONE2"
	else
	    for i in $(gcloud compute zones list \
			      --filter="name~^${REGION} AND status=UP" \
			      --format="value(name)") ; do
		if test "$ZONE" != "$i" ; then
		   Z2="$i"
		   break
		fi
	    done
	fi
	if test -n "$Z2" ; then
	    CREATE_FLAG="--region $REGION --replica-zones=${ZONE},${Z2}"
	else
	    SCOPE=zonal
	fi
    fi

    if test "$SCOPE" = "zonal" ; then
	CREATE_FLAG="--zone $ZONE"
    fi

    gcloud compute -q disks create "$SCRATCH_DSK" \
	--size "${SIZE}" --type "${TYPE}" $CREATE_FLAG
    gcloud compute -q instances attach-disk ${instance} \
	--disk "$SCRATCH_DSK" \
	--device-name scratch --zone "$ZONE" --disk-scope "$SCOPE"
    gcloud compute -q instances set-disk-auto-delete ${instance} \
	--auto-delete --device-name scratch --zone "$ZONE" &
fi

if ! test -e $CREATED_SCRATCH_DEV
then
    logger -s "Couldn't create or find the scratch disk!"
    exit 1
fi

if test -f "/run/do_blktests" ; then
    wait
    exit 0
fi

if ! vgs xt >& /dev/null
then
    pvcreate $CREATED_SCRATCH_DEV
    vgcreate xt $CREATED_SCRATCH_DEV

    if [ "$VDB_SIZE" != '0g' ]
    then
	lvcreate -L "${VDB_SIZE:-5g}"  -n vdb xt
	mke2fs -t ext4 -q /dev/mapper/xt-vdb
    fi
    if [ "$VDC_SIZE" != '0g' ]
    then
	lvcreate -L "${VDC_SIZE:-5g}"  -n vdc xt
    fi
    if [ "$VDD_SIZE" != '0g' ]
    then
	lvcreate -L "${VDD_SIZE:-5g}"  -n vdd xt
    fi
    if [ "$VDE_SIZE" != '0g' ]
    then
	lvcreate -L "${VDE_SIZE:-20g}"  -n vde xt
    fi
    if [ "$VDF_SIZE" != '0g' ]
    then
	lvcreate -L "${VDF_SIZE:-20g}"  -n vdf xt
    fi
    if [ "$VDI_SIZE" != '0g' ]
    then
	lvcreate -L "${VDI_SIZE:-1g}"  -n vdi xt
    fi
    if [ "$VDJ_SIZE" != '0g' ]
    then
	lvcreate -L "${VDJ_SIZE:-1g}"  -n vdj xt
    fi
fi
wait
exit 0
