#!/bin/sh
#
# Copyright (C) 2007-2009 Canonical, Ltd.
# Author: Jamie Strandboge <jamie@canonical.com>
# License: GPLv3
#
set -e

ustconf="$HOME/.uqt-vm-tools.conf"
if [ -s "$ustconf" ]; then
    . "$ustconf"
else
    echo "Could not find '$ustconf'"
    exit 1
fi

. $UQT_VM_TOOLS/libvm.sh
abort_if_root

help() {
    cat << EOM
USAGE:
  vm-remove [-f] <name1> <name2> ...
  vm-remove [-f] -p <PREFIX>

Specifying a 'PREFIX' will remove all machines created with vm-new with that
PREFIX that are in '$vm_release_list'. Eg:
$ vm-remove -f -p sec

Removes all sec-<release>-<arch> machines. Be careful.
EOM
}

if [ -z "$1" ]; then
    help
    exit 1
fi

force=
prefix=
while getopts "fhp:" opt
do
    case "$opt" in
        f) force="yes";;
        p) prefix="$OPTARG";;
        ?|h) help
           exit 1
           ;;
    esac
done
shift $(($OPTIND - 1))

remove_machine() {
    original="$1"
    # check if original
    if ! vm_exists $original ; then
        echo "'$original' does not exist. Skipping" >&2
        continue
    fi

    disks=`get_disks $original`
    if [ -z "$disks" ]; then
        echo "Could not find any disks for '$original'. Skipping" >&2
        continue
    fi

    # just grab the first disk for the path
    tmp=`echo "$disks" | awk '{ print $1 }'`
    path=`dirname "$tmp"`

    if [ "$force" != "yes" ]; then
        echo "Are you sure you want to destroy '$original', the following"
        echo "disks, and any snapshots or pristine images associated with them: "
        for i in $disks ; do
            echo "  $i"
        done
        echo -n '(y|N)? '
        read ans
        if [ "$ans" != "y" ] && [ "$ans" != "Y" ]; then
            echo "Aborting"
            exit 0
        fi
    fi

    if vm_running "$original" ; then
        echo "Destroying '$original'"
        virsh --connect ${vm_connect} destroy "$original"
        sleep 2
    fi

    echo "Undefining '$original'"
    virsh --connect ${vm_connect} undefine "$original"

    for i in $disks ; do
        dir=`dirname "$i"`
        bn=`basename "$i" .qcow2`
        pristine="$dir/$bn.pristine.qcow2"

        if [ -f "$pristine" ] && [ -f "$i" ]; then
            echo "Removing $pristine"
            rm -f "$pristine"
            echo "Removing $i"
            rm -f "$i"
        elif [ -f "$pristine" ]; then
            echo "Removing $pristine"
            rm -f "$pristine"
        elif [ -f "$i" ]; then
            echo "Removing $i"
            rm -f "$i"
        else
            echo "Couldn't find:" >&2
            echo "  $i" >&2
            echo "  $pristine" >&2
            echo "(skipping)" >&2
        fi
    done

    echo "Removing '$path'"
    rmdir "$path" || true

    # Get rid of old ssh key
    ssh-keygen -R $original
    ssh-keygen -R $original.
    ssh-keygen -R $original.local
}

if [ -z "$prefix" ]; then
    for original in "$@"; do
        remove_machine $original
    done
else
    for a in $vm_archs ; do
        for m in `vm_get_machines_by_prefix "${prefix}" "$a"` ; do
            remove_machine "$m"
        done
    done
fi
