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

line_filter()
{
    # https://stackoverflow.com/questions/1251999/how-can-i-replace-a-newline-n-using-sed
    sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/ /g' -e "s| \+| |g" -e "s|^ ||g" -e "s| $||g"
}


get_list()
{
    if [ "$1" = "ALL" ] ; then
        list_ALL
        return
    fi
    if [ "$1" = "all" ] ; then
        list_all
        return
    fi
    if [ -n "$1" ] ; then
        list_ALL "$@"
        return
    fi
    list_all
}

arg_is_id()
{
    [ -n "$1" ] || return 1
    echo " $(list_ALL) " | grep -q " $1 " && return 0
    echo " $(list_ALL_names) " | grep -q " $1 " && return 0
    like_id "$1" && fatal "ID $1 is missed on the host. Check # evz list -a output"
    return 1
}

arg_is_all()
{
    [ "$1" = "ALL" ] && return
    [ "$1" = "all" ] && return
    return 1
}

option_all()
{
    [ "$1" = "-a" ] && return
    [ "$1" = "--all" ] && return
    return 1
}

# quote with \' all args with spaces
quote_args()
{
    local i
    for i in "$@" ; do
        [ "${i// /_}" = "$i" ] && echo -n " $i" && continue
        echo -n " \'$i\'"
    done
}