#!/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

    # TODO
    #a= pct list | grep "[0-9]\+" | sed -e "s| *\([0-9]\+\) .*|\1|" | line_filter
}

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

    # TODO
    #a= pct list | grep "[0-9]\+" | sed -e "s| *\([0-9]\+\) .*|\1|" | line_filter
}

list_ALL_names()
{
    # TODO
    return
}

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

todo()
{
    warning "FIXME: $@"
}

# TODO: add all support for all cases

evz_pct()
{
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 pct stop $i
	    docmd pct set $i -onboot 0
	done
	;;
    on)                              # HELPCMD: enable start on boot and start container(s)
	for i in "$@" ; do
	    #info "Starting $i ..."
	    # TODO: check if enabled
	    docmd pct set $i -onboot 1
	    docmd pct start $i
	done
	;;
    status)                          # HELPCMD: print container(s) status
	LIST="$(get_list "$@")"
	#info "Do $CMD for $LIST ..."
	for i in $LIST ; do
	    docmd pct status $i
	done
	;;
    compact)                         # HELPCMD: do named operation on container(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
	    #info "Do $CMD on $i ..."
	    todo docmd docker port $i
	done
	;;
    set)                         # HELPCMD: set param. Use with --option param
	###################################
	# the same code like in set)
	# get all args until ids or all/ALL
	LIST=""
	if arg_is_all "$1" ; then
		LIST="$(get_list "$1")"
		shift
	else
		while [ -n "$1" ] ; do
			arg_is_id "$1" || break
			LIST="$LIST $1"
			shift
		done
	fi
	###################################

	for i in $LIST ; do
	    info "Do $CMD on $i ..."
	    docmd pct set $i "$@"
	done
	;;
    ubc|resources|show)                         # HELPCMD: print resource using
	LIST="$(get_list "$@")"
	todo showcmd docker stats --no-stream --no-trunc
	#for i in $LIST ; do
	#    a= docker stats --no-stream --no-trunc $i
	#done
	;;
    stop)                            # HELPCMD: stop container(s)
	LIST="$(get_list "$@")"
	for i in $LIST ; do
	    info "Stopping $i ..."
	    if [ -n "$force" ] ; then
	        docmd pct stop $i
	        continue
	    fi
	    docmd pct shutdown $i
	done
	;;
    start)                           # HELPCMD: start container(s)
	LIST="$(get_list "$@")"
	for i in $LIST ; do
	    info "Starting $i ..."
	    docmd pct start $i
	done
	;;
    restart)                         # HELPCMD: restart container(s)
	LIST="$(get_list "$@")"
	for i in $LIST ; do
	    info "Restarting $i ..."
	    docmd pct reboot $i
	done
	;;
    suspend|sleep)                   # HELPCMD: suspend container(s)
	LIST="$(get_list "$@")"
	for i in $LIST ; do
	    info "Suspening $i ..."
	    docmd pct suspend $i
	done
	;;
    resume)                           # HELPCMD: resume container(s)
	LIST="$(get_list "$@")"
	for i in $LIST ; do
	    info "Resuming $i ..."
	    docmd pct resume $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 pct list -a
	else
		docmd pct list
	fi
	#info "See evz ports ID for print ports"
	;;
    exec)                            # HELPCMD: execute command by list (all for all containers)
	###################################
	# the same code like in set)
	# get all args until ids or all/ALL
	LIST=""
	if arg_is_all "$1" ; then
		LIST="$(get_list "$1")"
		shift
	else
		while [ -n "$1" ] ; do
			arg_is_id "$1" || break
			LIST="$LIST $1"
			shift
		done
	fi
	###################################

	for i in $LIST ; do
	    docmd pct exec "$i" "$@"
	done
	;;
    enter)                            # HELPCMD: enter in a container with ID
	docmd pct enter "$1"
	#a= docker exec -ti $SETLANG "$1" bash
	;;
    log|logs)                         # HELPCMD: print container log
	todo docmd docker logs "$1"
	;;
    info)                            # HELPCMD: print containers(s) info (vzlist like)
	LIST="$(get_list "$@")"
	#showcmd docker ps --format 'table {{.ID}}\t{{.Image}}\t{{.Status}}\t{{.Names}}\t{{.Size}}'
	#a= docker ps --format 'table {{.ID}}\t{{.Image}}\t{{.Status}}\t{{.Names}}\t{{.Size}}' "$@"
	for i in $LIST ; do
	#    a= docker ps --format 'table {{.ID}}\t{{.Image}}\t{{.Status}}\t{{.Names}}\t{{.Size}}' "$@"
	    #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
	    #vzlist $LIST -o ctid,ip,hostname,diskspace
	    docmd pct config $i
	done
	#info "See evz ports ID for print ports"
	;;
    load)                            # HELPCMD: print load average for container(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
        ;;
    check)                           # HELPCMD: check system
        todo pveversion -v
        ;;
    destroy)                         # HELPCMD: destroy container(s) by list
        echo "You request to destroy follow containers:"
        # 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
	    info "Stopping $i ..."
	    #docmd qm stop $i
	    docmd pct destroy $i
	done
	;;
    *)
	fatal "Unknow command '$CMD'. Use --help to get help."
	;;
esac
}