#!/bin/bash
#This script uninstalls a machine config from the OS museum VM, deleting all
#snapshots. The main use of this is when an unreleased VM needs to be updated
#without incrementing the version number because something was broken

. "`dirname $0`/bootscriptfunctions.sh"

if ! is_os_museum_vm
then
	panic "This script can only be run on the OS Museum VM"
fi

source_emulator_functions _snapshot

if [ "$UID" != 0 ]
then
	exec sudo "$0" "$@"
fi

if [ x"$1" = x ]
then
	echo "usage: $0 <base machine config name (without the _config suffix>" >&2
	exit 1
fi

config_snap_root="$OSM_SNAP_ROOT/$1"

if [ ! -d "$config_snap_root" ]
then
	panic "no snapshot directory for machine config $1"
fi

for snap_dir in "$config_snap_root"/[0-9]*
do
	if [ -d "$snap_dir" ]
	then
		btrfs subvolume delete "$snap_dir"
	fi
done

rm -rf "$config_snap_root"

apt-get -y remove "os-museum-machine-`echo $1 | tr _ -`-info"
