#!/bin/sh
#
# Copyright (C) 2017  Etersoft
# Copyright (C) 2017  Vitaly Lipatov <lav@etersoft.ru>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

PROGDIR=$(dirname $0)
[ "$PROGDIR" = "." ] && PROGDIR=$(pwd)

fatal()
{
    echo "$*" >&2
    exit 1
}

info()
{
    echo "$*"
}

CMD=$1
shift
case $CMD in
    off)
	for i in "$@" ; do
	    info "Stopping $i ..."
	    vzctl stop $i
	    # TODO: check if enabled
	    vzctl set $i --onboot no --save
	done
	;;
    on)
	for i in "$@" ; do
	    info "Starting $i ..."
	    # TODO: check if enabled
	    vzctl set $i --onboot yes --save
	    vzctl start $i
	done
	;;
    stop)
	for i in "$@" ; do
	    info "Stopping $i ..."
	    vzctl stop $i
	done
	;;
    start)
	for i in "$@" ; do
	    info "Starting $i ..."
	    vzctl start $i
	done
	;;
    destroy)
        echo "You request to destroy follow containers:"
        vzlist "$@"
        #local response
        read -r -p "Are you sure? [Yes/No]" response
        [ "$response" = "Yes" ] || fatal "Have no receive your accept."

	for i in "$@" ; do
	    info "Stopping $i ..."
	    vzctl stop $i
	    vzctl destroy $i
	done
	;;
    *)
	fatal "Unknow command '$CMD'"
	;;
esac
