#!/bin/sh
#
# Copyright (C) 2017, 2020  Etersoft
# Copyright (C) 2017, 2020  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/>.
#

# TODO: add all support for all cases

# TODO: add ports command support (via rooter/iptables)
# TODO: add cp support

list_all()
{
    a= vzlist -1 "$@" | line_filter
}

list_ALL()
{
    a= vzlist -1 -a "$@" | line_filter
}

list_ALL_names()
{
    a= vzlist -H -a -o name | grep -v "^-$"
}

like_id()
{
    echo "$1" | grep -q -E "^[0-9]+$"
}


evz_vzctl()
{
CMD=$1
shift
case $CMD in
    off)                             # HELPCMD: stop container(s) and disable start on boot
	LIST="$(get_list "$@")"
	for i in $LIST ; do
	    info "Stopping $i ..."
	    docmd vzctl stop $i
	    # TODO: check if enabled
	    docmd vzctl set $i --onboot no --save
	done
	;;
    on)                              # HELPCMD: enable start on boot and start container(s)
	for i in "$@" ; do
	    info "Starting $i ..."
	    # TODO: check if enabled
	    docmd vzctl set $i --onboot yes --save
	    docmd vzctl start $i
	done
	;;
    status)                          # HELPCMD: print container(s) status
	LIST="$(get_list "$@")"
	info "Do vzctl $CMD for $LIST ..."
	for i in $LIST ; do
	    a= vzctl $CMD $i
	done
	;;
    compact)                         # HELPCMD: do named operation on container(s)
	LIST="$(get_list "$@")"
	for i in $LIST ; do
	    #info "Do $CMD on $i ..."
	    docmd vzctl $CMD $i
	done
	;;
    set)                         # HELPCMD: set param. Use with --option param
	fill_args_list "$@"
	for i in $LIST ; do
	    docmdeval vzctl set $i "$QUOTEDARGS" --save
	done
	;;
    ubc|resources|show)                         # HELPCMD: print resource using via vzubc
	LIST="$(get_list "$@")"
	for i in $LIST ; do
	    #info "Do $CMD on $i ..."
	    a= vzubc $i
	done
	;;
    stop)                            # HELPCMD: stop container(s)
	LIST="$(get_list "$@")"
	for i in $LIST ; do
	    info "Stopping $i ..."
	    docmd vzctl stop $i
	done
	;;
    start)                           # HELPCMD: start container(s)
	LIST="$(get_list "$@")"
	for i in $LIST ; do
	    info "Starting $i ..."
	    docmd vzctl start $i
	done
	;;
    restart)                         # HELPCMD: restart container(s)
	LIST="$(get_list "$@")"
	for i in $LIST ; do
	    info "Restarting $i ..."
	    docmd vzctl restart $i
	done
	;;
    list|ps)                            # HELPCMD: list available container(s) (use -q|-1 for list only ID, list -a for list ever stopped containers)
	# if -q, just id list
	if [ -z "$verbose" ] || [ "$1" = "-1" ] || [ "$1" = "-q" ] ; then
		[ -z "$verbose" ] || shift
		get_list "$@"
		exit
	fi
	if option_all "$1" ; then
		docmd vzlist -a
	else
		docmd vzlist
	fi
	;;
    exec)                            # HELPCMD: execute command by list (all for all containers)
	fill_args_list "$@"
	for i in $LIST ; do
	    docmdeval vzctl exec $i "$QUOTEDARGS"
	done
	;;
    enter)                            # HELPCMD: enter in a container with ID
	showcmd exec vzctl enter "$1"
	a= exec vzctl enter "$1"
	;;
    info)                            # HELPCMD: print containers(s) info (vzlist like)
	LIST="$(get_list "$@")"
	#for i in $LIST ; do
	    #info "Executing on $i ..."
	    #printf "%3d: %s" $i "$(vzctl exec $i "$INCMD")"
	    #vzctl exec $i "$INCMD" | sed -e "s|^|$(printf "%3d: " $i)|g"
	    #vzctl exec $i "$INCMD"
	    # TODO: internal IP, red if internal hostname differs
	    docmd vzlist $LIST -o ctid,ip,hostname,name,diskspace
	#done
	;;
    load)                            # HELPCMD: print load average for container(s) by list
        for id in $(get_list "$@") ; do
            CONF=/etc/vz/conf/$id.conf
            test -r $CONF || continue
            # TODO: drop long part?
            eval `cat $CONF | grep ^HOSTNAME | sed -e "s|office.etersoft.ru||g"`
            printf "%4s (%30s):" $id $HOSTNAME
            a= vzctl exec $id uptime
        done
        #echo "---"
        #uptime
        ;;
    destroy)                         # HELPCMD: destroy container(s) by list
        echo "You request to destroy follow containers:"
        #vzlist -a "$@"
        LIST="$(get_list "$@")"
        echo "$LIST"
        #local response
        read -r -p "Are you sure? [Yes/No]" response
        [ "$response" = "Yes" ] || fatal "Have no receive your accept."

	for i in $LIST ; do
	    #info "Stopping $i ..."
	    docmd vzctl stop $i
	    docmd vzctl destroy $i
	done
	;;
    *)
	fatal "Unknow command '$CMD'. Use --help to get help."
	;;
esac
}
