#!/bin/bash

# -------------------------------------------------------------------------- #
# Copyright 2002-2021, OpenNebula Project, OpenNebula Systems                #
#                                                                            #
# 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.                                             #
#--------------------------------------------------------------------------- #

# mvds host:remote_system_ds/disk.i fe:SOURCE vmid dsid
#   - fe is the front-end hostname
#   - SOURCE is the path of the disk image in the form DS_BASE_PATH/disk
#   - host is the target host to deploy the VM
#   - remote_system_ds is the path for the system datastore in the host
#   - vmid is the id of the VM
#   - dsid is the target datastore (0 is the system datastore)

# Nothing to do

SRC=$1
DST=$2
VMID=$3

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

DRIVER_PATH=$(dirname $0)

. $TMCOMMON

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

DISK_ID=$(basename ${SRC} | cut -d. -f2)

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

unset i j XPATH_ELEMENTS

while IFS= read -r -d '' element; do
    XPATH_ELEMENTS[i++]="$element"
done < <(onevm show -x $VMID | $XPATH  \
                        /VM/LCM_STATE )

LCM_STATE="${XPATH_ELEMENTS[j++]}"

#-------------------------------------------------------------------------------
# Set dst path and dir
#-------------------------------------------------------------------------------

SRC_HOST=$(arg_host $SRC)
SRC_PATH=$(arg_path $SRC)
SRC_SNAP_DIR=${SRC_PATH}.snap
DST_PATH="$DST"
DST_FILE=$(basename $DST)
DST_SNAP_DIR=${DST_PATH}.snap
SRC_DIR=`dirname $SRC_PATH`
#-------------------------------------------------------------------------------
# Move the image back to the datastore
#-------------------------------------------------------------------------------

SEARCH_SNAP_XML=$(cat <<EOF

SNAP_PATH=("${SRC_DIR}/snap-"*.xml)
for path in "\${SNAP_PATH[@]}"; do
    if [ -e "\$path" ] && [[ "$LCM_STATE" =~ ^(34)$ ]]; then
        XML_PATH=(\$(xmllint --xpath '/domainsnapshot/domain/devices/disk/source/@file' "\$path" | grep -o 'file="[^"]*"' | sed 's/file="//;s/"//'))
        for disk_xml in "\${XML_PATH[@]}"; do
            if [ "\$disk_xml" == "$SRC_PATH" ]; then
                exit 1
            fi
        done
    fi
done

EOF
)

ssh_exec_and_log $SRC_HOST "$SEARCH_SNAP_XML" "Error remove disks, because $SRC_PATH exists in snap.xml"


ssh_exec_and_log "$SRC_HOST" "$RM -rf $SRC_PATH" "Error removing $SRC_PATH from $SRC_HOST"

exit 0
