#!/usr/bin/env bash
#
# Copyright 2013 Red Hat
# All Rights Reserved.
#
# 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.

SCRIPT_NAME=$(basename $0)

function show_options() {
    echo "Usage: $SCRIPT_NAME"
    echo
    echo "Cleanup vm state left behind by previous runs"
    exit 1
}

[ $# -gt 0 ] && show_options

for NAME in $(sudo virsh list --name --all | grep "^\(seed\(.*\|\)\|baremetal\(\|brbm.*\)_.*\)$"); do
  sudo virsh destroy $NAME
  sudo virsh undefine --managed-save --remove-all-storage $NAME
done

for NAME in $(sudo virsh vol-list default 2>/dev/null | grep /var/ | awk '{print $1}' | grep "^\(seed\(.*\|\)|baremetal\(\|brbm.*\)-\)" ); do
  sudo virsh vol-delete --pool default $NAME
done
