#!/bin/bash

SHUTDOWN_TIMEOUT=30

vm_uuid=$1
vdi_uuid=$2

if [ "$vm_uuid" ]; then
    timeout $SHUTDOWN_TIMEOUT xe vm-shutdown uuid="$vm_uuid" &>/dev/null
    if [ $? -eq 124 ]; then
        xe vm-shutdown --force uuid="$vm_uuid" &>/dev/null
    fi
fi

if [ "$vdi_uuid" ]; then
    xe vdi-destroy uuid="$vdi_uuid"
fi

if [ "$vm_uuid" ]; then
    xe vm-destroy uuid="$vm_uuid"
fi
