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

# list all running containers (or by arg list)
list_all()
{
    if [ -n "$1" ] ; then
        echo "$*"
        return
    fi

    a= docmd vboxmanage list runningvms | sed -e "s|.*{\(.*\)}|\1|"
    # | line_filter
}

# list all containers (or by arg list)
list_ALL()
{
    if [ -n "$1" ] ; then
        echo "$*"
        return
    fi

    a= vboxmanage list vms | sed -e "s|.*{\(.*\)}|\1|"
    # | line_filter
}

list_ALL_names()
{
    #a= vboxmanage list vms | sed -e "s| {.*||"
    return
}

like_id()
{
    # 8-4-4-4-12
    # 707b0509-d9f6-4b33-833b-4af1a2e12eee
    echo "$1" | grep -q -E "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
}


todo()
{
    warning "$@"
}

# TODO: add all support for all cases

evz_vboxmanage()
{
CMD=$1
shift
case $CMD in
    off)                             # HELPCMD: stop virtual machine(s) and disable start on boot
	LIST="$(get_list "$@")"
	for i in $LIST ; do
	    #info "Stopping $i ..."
	    todo docmd docker stop $i
	    todo docmd docker update --restart no $i
	done
	;;
    on)                              # HELPCMD: enable start on boot and start virtual machine(s)
	for i in "$@" ; do
	    #info "Starting $i ..."
	    # TODO: check if enabled
	    todo docmd docker update --restart always $i
	    todo docmd docker start $i
	done
	;;
    status)                          # HELPCMD: print virtual machine(s) status
	LIST="$(get_list "$@")"
	#info "Do $CMD for $LIST ..."
	for i in $LIST ; do
	    todo docmd docker stats --no-stream --no-trunc $i
	done
	;;
    compact)                         # HELPCMD: do named operation on virtual machine(s)
	LIST="$(get_list "$@")"
	for i in $LIST ; do
	    info "Do $CMD on $i ..."
	    todo vzctl $CMD $i
	done
	;;
    ports)                           # HELPCMD: show public ports
	LIST="$(get_list "$@")"
	for i in $LIST ; do
	    docmd vboxmanage showvminfo $i | grep Rule
	done
	;;
    set)                         # HELPCMD: set param. Use with --option param
	fill_args_list "$@"
	# show help if empty
	[ -n "$LIST" ] || docmd vboxmanage modifyvm
	for i in $LIST ; do
	    docmdeval vboxmanage modifyvm $i "$QUOTEDARGS"
	done
	;;
    ubc|resources|show)                         # HELPCMD: print resource using by virtual machine(s)
	LIST="$(get_list "$@")"
	for i in $LIST ; do
	    docmd vboxmanage showvminfo $i
	done
	;;
    stop)                            # HELPCMD: stop virtual machine(s)
	LIST="$(get_list "$@")"
	for i in $LIST ; do
	    info "Stopping $i ..."
	    if [ -n "$force" ] ; then
	        docmd vboxmanage controlvm $i poweroff
	        continue
	    fi
	    docmd vboxmanage controlvm $i acpipowerbutton
	done
	;;
    start)                           # HELPCMD: start virtual machine(s)
	LIST="$(get_list "$@")"
	for i in $LIST ; do
	    info "Starting $i ..."
	    docmd vboxmanage startvm --type headless $i
	done
	;;
    restart)                         # HELPCMD: restart virtual machine(s)
	LIST="$(get_list "$@")"
	for i in $LIST ; do
	    info "Restarting $i ..."
	    todo docmd ker restart $i
	done
	;;
    suspend|sleep)                   # HELPCMD: sleep (suspend) virtual machine(s)
	LIST="$(get_list "$@")"
	for i in $LIST ; do
	    info "Sleeping $i ..."
	    docmd vboxmanage controlvm $i savestate
	done
	;;
    resume)                          # HELPCMD: resume virtual machine(s)
	LIST="$(get_list "$@")"
	for i in $LIST ; do
	    info "Resuming $i ..."
	    docmd vboxmanage controlvm $i resume
	done
	;;
    list|ps)                            # HELPCMD: list available virtual machine(s) (use -q|-1 for list only ID, list -a for list ever stopped virtual machines)
	# 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 vboxmanage list vms
	else
		docmd vboxmanage list runningvms
	fi
	;;
    exec)                            # HELPCMD: execute command by list (all for all virtual machines)
	fill_args_list "$@"
	for i in $LIST ; do
	    todo showcmd docker exec -ti $SETLANG "$i" "$INCMD"
	    todo a= docker exec -ti $SETLANG "$i" "$INCMD"
	done
	;;
    enter)                            # HELPCMD: enter in a virtual machine with ID
	todo showcmd docker exec -ti $SETLANG "$1" bash
	todo a= docker exec -ti $SETLANG "$1" bash
	;;
    log|logs)                         # HELPCMD: print virtual machine log
	todo docmd docker logs "$1"
	;;
    info)                            # HELPCMD: print virtual machines(s) info (vzlist like)
        #docmd vboxmanage list systemproperties
	LIST="$(get_list "$@")"
	for i in $LIST ; do
	    docmd vboxmanage showvminfo $i
	done
	;;
    load)                            # HELPCMD: print load average for virtual machine(s) by list
	todo LOAD
        #for id in $(get_list "$@") ; do
        #    HN=$(a= docker inspect --format '{{.Name}}' $id)
        #    CPU=$(a= docker stats --no-stream --format " {{.CPUPerc}}" $id)
        #    printf "%4s (%30s): %6s\n" $id $HN $CPU
        #done
        ;;
    destroy)                         # HELPCMD: destroy virtual machine(s) by list
        echo "You request to destroy follow virtual machines:"
        # don't support all/ALL
	#LIST="$(get_list "$@")"
	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
		docmd vboxmanage unregistervm $i --delete
	done
	;;
    *)
	fatal "Unknow command '$CMD'. Use --help to get help."
	;;
esac
}
