#!/bin/bash

# -------------------------------------------------------------------------- #
# Copyright 2002-2016, 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.                                             #
#--------------------------------------------------------------------------- #

DRIVER_PATH=$(dirname $0)

source $DRIVER_PATH/kvmrc
source $DRIVER_PATH/../../scripts_common.sh

FILE=$1
HOST=$2
DEPLOY_ID=$3

VM_ID=$4
DS_ID=$5

FILE_XML=${FILE}.xml

#-------------------------------------------------------------------------------
# Handle DRV_MESSAGE coming from stdin
#-------------------------------------------------------------------------------

if [ ! -t 0 ]; then
    # There is data in stdin, read it
    DRV_MESSAGE=$(cat)

    # The data is the driver message. Extracting the System DS TM_MAD
    XPATH="${DRIVER_PATH}/../../datastore/xpath.rb --stdin"
    TM_MAD=$(echo "$DRV_MESSAGE" | $XPATH /VMM_DRIVER_ACTION_DATA/DATASTORE/TM_MAD)

    # If there is a specific hook for this TM_MAD call it:
    RESTORE_TM_FILE="${DRIVER_PATH}/restore.${TM_MAD}"

    if [ -x "$RESTORE_TM_FILE" ]; then
        echo "$DRV_MESSAGE" | $RESTORE_TM_FILE $@
    fi
fi

# Checkpoint file: /var/lib/one//datastores/<DS_ID>/<VM_ID>/checkpoint

DS_ID=$(basename $(dirname $(dirname $FILE)))
DS_LOCATION=$(dirname $(dirname $(dirname $FILE)))
DS_LOCATION_NON_DOUBLE_SLASH=$(echo "$DS_LOCATION" | sed 's|//|/|g')

RECALCULATE_CMD=$(cat <<EOF
set -e -o pipefail

# extract the xml from the checkpoint

virsh --connect $LIBVIRT_URI save-image-dumpxml $FILE > $FILE_XML

# Eeplace all occurrences of the DS_LOCATION/<DS_ID>/<VM_ID> with the specific
# DS_ID where the checkpoint is placed. This is done in case there was a
# system DS migration

sed -i "s%$DS_LOCATION/[0-9]\+/$VM_ID/%$DS_LOCATION/$DS_ID/$VM_ID/%g" $FILE_XML
sed -i "s%$DS_LOCATION_NON_DOUBLE_SLASH/[0-9]\+/$VMID/%$DS_LOCATION/$DS_ID/$VMID/%g" $FILE_XML
EOF
)

multiline_exec_and_log "$RECALCULATE_CMD" \
    "Could not recalculate paths in $FILE_XML"

exec_and_log "virsh --connect $LIBVIRT_URI restore $FILE --xml $FILE_XML" \
    "Could not restore from $FILE"

rm "$FILE"
rm "$FILE_XML"
