#!/bin/bash

#
# lxc: linux Container library

# Authors:
# Daniel Lezcano <daniel.lezcano@free.fr>

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

#
# This script allows to set or remove the capabilities on the lxc tools.
# When the capabilities are set, a non root user can manage the containers.
#

usage() {
    echo "usage: $0 -n <name>"
}

if [ "$(id -u)" != "0" ]; then
   echo "This command has to be run as root"
   exit 1
fi

shortoptions='n:'
longoptions='name:'
lxc_path=/usr/var/lib/lxc

getopt=$(getopt -o $shortoptions --longoptions  $longoptions -- "$@")
if [ $? != 0 ]; then
    usage $0
    exit 1;
fi

eval set -- "$getopt"

while true; do
        case "$1" in
	    -n|--name)
		shift
		lxc_name=$1
		shift
		;;
            --)
		shift
		break;;
            *)
		echo $1
		usage $0
		exit 1
		;;
        esac
done

if [ -z "$lxc_name" ]; then
    echo "no container name specified"
    usage $0
    exit 1
fi

if [ ! -d "$lxc_path/$lxc_name" ]; then
    echo "'$lxc_name' does not exist"
    exit 1
fi

# recursively remove the container to remove old container configuration
rm -rf --preserve-root $lxc_path/$lxc_name
