#!/bin/bash

# Vitastor OpenNebula driver
# Copyright (c) Vitaliy Filippov, 2024+
# License: Apache-2.0 http://www.apache.org/licenses/LICENSE-2.0

# DELETE <host:remote_system_ds/disk.i|host:remote_system_ds/>
#   - host is the target host to deploy the VM
#   - remote_system_ds is the path for the system datastore in the host

DST=$1
VM_ID=$2
DS_ID=$3

#--------------------------------------------------------------------------------

if [ -z "${ONE_LOCATION}" ]; then
    TMCOMMON=/var/lib/one/remotes/tm/tm_common.sh
    LIB_LOCATION=/usr/lib/one
else
    TMCOMMON=$ONE_LOCATION/var/remotes/tm/tm_common.sh
    LIB_LOCATION=$ONE_LOCATION/lib
fi

DRIVER_PATH=$(dirname $0)

source $TMCOMMON
source ${DRIVER_PATH}/../../datastore/libfs.sh

#-------------------------------------------------------------------------------
# Process destination
#-------------------------------------------------------------------------------

DST_PATH=`arg_path $DST`
DST_HOST=`arg_host $DST`

XPATH="${DRIVER_PATH}/../../datastore/xpath.rb --stdin"

#-------------------------------------------------------------------------------
# Delete and exit if directory
#-------------------------------------------------------------------------------

if [ `is_disk $DST_PATH` -eq 0 ]; then
    # Directory: delete checkpoint and directory

    unset i j XPATH_ELEMENTS

    while IFS= read -r -d '' element; do
        XPATH_ELEMENTS[i++]="$element"
    done < <(onedatastore show -x $DS_ID | $XPATH \
        /DATASTORE/TEMPLATE/SOURCE \
        /DATASTORE/TEMPLATE/CLONE \
        /DATASTORE/TEMPLATE/VITASTOR_CONF \
        /DATASTORE/TEMPLATE/IMAGE_PREFIX \
        /DATASTORE/TEMPLATE/POOL_NAME)

    SRC="${XPATH_ELEMENTS[j++]}"
    CLONE="${XPATH_ELEMENTS[j++]}"
    VITASTOR_CONF="${XPATH_ELEMENTS[j++]}"
    IMAGE_PREFIX="${XPATH_ELEMENTS[j++]:-one}"
    POOL_NAME="${XPATH_ELEMENTS[j++]}"

    CLI=vitastor-cli
    if [ -n "$VITASTOR_CONF" ]; then
        CLI="$CLI --config_path ${VITASTOR_CONF}"
    fi

    SRC_CHECKPOINT="${IMAGE_PREFIX}-sys-${VM_ID}-checkpoint"

    ssh_exec_and_log "$DST_HOST" "$CLI rm $SRC_CHECKPOINT 2>/dev/null || exit 0" \
        "Error deleting $SRC_CHECKPOINT in $DST_HOST"

    log "Deleting $DST_PATH"
    ssh_exec_and_log "$DST_HOST" "rm -rf $DST_PATH" "Error deleting $DST_PATH"
    exit 0
fi

#-------------------------------------------------------------------------------
# Get Image information
#-------------------------------------------------------------------------------

DISK_ID=$(echo "$DST_PATH" | $AWK -F. '{print $NF}')

# Reads the disk parameters -- taken from image datastore
unset i j XPATH_ELEMENTS
while IFS= read -r -d '' element; do
    XPATH_ELEMENTS[i++]="$element"
done < <(onevm show -x $VM_ID | $XPATH \
    /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/SOURCE \
    /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/CLONE \
    /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/VITASTOR_CONF \
    /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/IMAGE_PREFIX \
    /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/POOL_NAME)

SRC="${XPATH_ELEMENTS[j++]}"
CLONE="${XPATH_ELEMENTS[j++]}"
VITASTOR_CONF="${XPATH_ELEMENTS[j++]}"
IMAGE_PREFIX="${XPATH_ELEMENTS[j++]:-one}"
POOL_NAME="${XPATH_ELEMENTS[j++]}"

if is_undeployed "$VM_ID" "$DST_HOST"; then
    # get BRIDGE_LIST from datastore
    XPATH="${DRIVER_PATH}/../../datastore/xpath.rb --stdin"
    IFS= read -r -d '' BRIDGE_LIST < <(onedatastore show -x "$DS_ID" \
        | $XPATH /DATASTORE/TEMPLATE/BRIDGE_LIST )

    if [ -n "$BRIDGE_LIST" ]; then
        DST_HOST=$(get_destination_host)
    fi
fi

# No need to delete not cloned images
if [ "$CLONE" = "NO" ]; then
    exit 0
fi

CLI=vitastor-cli
if [ -n "$VITASTOR_CONF" ]; then
    CLI="$CLI --config_path ${VITASTOR_CONF}"
fi

if [ -n "$SRC" ]; then
    # cloned, so the name will be "one-<imageid>-<vmid>-<diskid>"
    SRC_IMAGE="${SRC}-${VM_ID}-${DISK_ID}"
else
    # volatile
    SRC_IMAGE="${IMAGE_PREFIX}-sys-${VM_ID}-${DISK_ID}"
fi

# Delete the image

log "Deleting $DST_PATH"

DELETE_CMD=$(cat <<EOF
    $CLI rm $SRC_IMAGE
EOF
)

ssh_exec_and_log "$DST_HOST" "$DELETE_CMD" "Error deleting $SRC_IMAGE in $DST_HOST"
