serv.sh 66.1 KB
Newer Older
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1 2
#!/bin/sh
#
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3 4
# Copyright (C) 2012-2013, 2016, 2020, 2021  Etersoft
# Copyright (C) 2012-2013, 2016, 2020, 2021  Vitaly Lipatov <lav@etersoft.ru>
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#
# 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)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
21 22
[ "$PROGDIR" = "." ] && PROGDIR=$(pwd)

Vitaly Lipatov's avatar
Vitaly Lipatov committed
23 24 25 26 27 28 29 30 31 32 33 34 35
# will replaced to /usr/share/eepm during install
SHAREDIR=$(dirname $0)

load_helper()
{
    local CMD="$SHAREDIR/$1"
    [ -r "$CMD" ] || fatal "Have no $CMD helper file"
    . $CMD
}


# File bin/epm-sh-functions:

Vitaly Lipatov's avatar
Vitaly Lipatov committed
36

Vitaly Lipatov's avatar
Vitaly Lipatov committed
37

Vitaly Lipatov's avatar
Vitaly Lipatov committed
38 39
inputisatty()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
40 41 42
    # check stdin
    #tty -s 2>/dev/null
    test -t 0
Vitaly Lipatov's avatar
Vitaly Lipatov committed
43 44
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
45 46
isatty()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
47 48
    # check stdout
    test -t 1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
49 50
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
51 52
isatty2()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
53 54
    # check stderr
    test -t 2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
55 56
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
57 58
check_tty()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
59
    isatty2 || return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
60

Vitaly Lipatov's avatar
Vitaly Lipatov committed
61 62 63
    # Set a sane TERM required for tput
    [ -n "$TERM" ] || TERM=dumb
    export TERM
Vitaly Lipatov's avatar
Vitaly Lipatov committed
64

Vitaly Lipatov's avatar
Vitaly Lipatov committed
65
    check_core_commands
Vitaly Lipatov's avatar
Vitaly Lipatov committed
66

Vitaly Lipatov's avatar
Vitaly Lipatov committed
67 68 69 70 71
    # grep -E from busybox may not --color
    # grep -E from MacOS print help to stderr
    if grep -E --help 2>&1 | grep -q -- "--color" ; then
        export EGREPCOLOR="--color"
    fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
72

Vitaly Lipatov's avatar
Vitaly Lipatov committed
73 74 75 76
    is_command tput || return
    # FreeBSD does not support tput -S
    echo | a= tput -S >/dev/null 2>/dev/null || return
    USETTY="tput -S"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
77 78 79 80 81 82
}

: ${BLACK:=0} ${RED:=1} ${GREEN:=2} ${YELLOW:=3} ${BLUE:=4} ${MAGENTA:=5} ${CYAN:=6} ${WHITE:=7}

set_boldcolor()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
83 84 85 86 87
    [ -n "$USETTY" ] || return
    {
        echo bold
        echo setaf $1
    } | $USETTY
Vitaly Lipatov's avatar
Vitaly Lipatov committed
88 89
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
90 91
set_color()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
92 93 94 95
    [ -n "$USETTY" ] || return
    {
        echo setaf $1
    } | $USETTY
Vitaly Lipatov's avatar
Vitaly Lipatov committed
96 97
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
98 99
restore_color()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
100 101 102 103 104
    [ -n "$USETTY" ] || return
    {
        echo op; # set Original color Pair.
        echo sgr0; # turn off all special graphics mode (bold in our case).
    } | $USETTY
Vitaly Lipatov's avatar
Vitaly Lipatov committed
105 106 107 108
}

echover()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
109
    [ -z "$verbose" ] && return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
110 111 112
    echo "$*" >&2
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
113 114
echon()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
115 116
    # default /bin/sh on MacOS does not recognize -n
    echo -n "$*" 2>/dev/null || a= /bin/echo -n "$*"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
117 118 119
}


Vitaly Lipatov's avatar
Vitaly Lipatov committed
120 121
set_target_pkg_env()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
122 123 124 125 126 127
    [ -n "$DISTRNAME" ] || fatal "Missing DISTRNAME in set_target_pkg_env."
    local ver="$DISTRVERSION"
    [ -n "$ver" ] && ver="/$ver"
    PKGFORMAT=$($DISTRVENDOR -p "$DISTRNAME$ver")
    PKGVENDOR=$($DISTRVENDOR -s "$DISTRNAME$ver")
    RPMVENDOR=$($DISTRVENDOR -n "$DISTRNAME$ver")
Vitaly Lipatov's avatar
Vitaly Lipatov committed
128 129 130 131
}

showcmd()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
132 133 134 135 136 137 138
    if [ -z "$quiet" ] ; then
        set_boldcolor $GREEN
        local PROMTSIG="\$"
        is_root && PROMTSIG="#"
        echo " $PROMTSIG $*"
        restore_color
    fi >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
139 140
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
141 142
echocmd()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
143 144 145 146 147
    set_boldcolor $GREEN
    local PROMTSIG="\$"
    is_root && PROMTSIG="#"
    echo -n "$PROMTSIG $*"
    restore_color
Vitaly Lipatov's avatar
Vitaly Lipatov committed
148 149
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
150 151
docmd()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
152 153
    showcmd "$*$EXTRA_SHOWDOCMD"
    "$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
154 155 156 157
}

docmd_foreach()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
158 159 160 161 162 163 164
    local cmd pkg
    cmd="$1"
    #showcmd "$@"
    shift
    for pkg in "$@" ; do
        docmd $cmd $pkg
    done
Vitaly Lipatov's avatar
Vitaly Lipatov committed
165 166
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
167 168
sudorun()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
169 170 171 172 173 174
    set_sudo
    if [ -z "$SUDO" ] ; then
        "$@"
        return
    fi
    $SUDO "$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
175 176
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
177 178
sudocmd()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
179 180 181
    set_sudo
    [ -n "$SUDO" ] && showcmd "$SUDO $*" || showcmd "$*"
    sudorun "$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
182 183
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
184 185
sudocmd_foreach()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
186 187 188 189 190 191 192 193
    local cmd pkg
    cmd="$1"
    #showcmd "$@"
    shift
    for pkg in "$@" ; do
        # don't quote $cmd here: it can be a command with an args
        sudocmd $cmd $pkg || return
    done
Vitaly Lipatov's avatar
Vitaly Lipatov committed
194 195
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
196 197
make_filepath()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
198 199 200 201 202 203
    local i
    for i in "$@" ; do
        [ -f "$i" ] || continue
        echo "$i" | grep -q "/" && echo "$i" && continue
        echo "./$i"
    done
Vitaly Lipatov's avatar
Vitaly Lipatov committed
204 205
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
206 207
get_firstarg()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
208
    echon "$1"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
209 210 211 212
}

get_lastarg()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
213 214 215
    local lastarg
    eval "lastarg=\${$#}"
    echon "$lastarg"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
216 217
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
218 219
isnumber()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
220
    echo "$*" | filter_strip_spaces | grep -q "^[0-9]\+$"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
221 222
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
223 224
rhas()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
225
    echo "$1" | grep -E -q -- "$2"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
226 227 228 229 230 231 232 233
}

is_dirpath()
{
    [ "$1" = "." ] && return $?
    rhas "$1" "/"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
234 235 236 237 238 239 240 241 242 243 244 245 246
filter_strip_spaces()
{
        # possible use just
        #xargs echo
        sed -e "s| \+| |g" | \
                sed -e "s|^ ||" | sed -e "s| \$||"
}

strip_spaces()
{
        echo "$*" | filter_strip_spaces
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
247 248 249

sed_escape()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
250
    echo "$*" | sed -e 's/[]()$*.^|[]/\\&/g'
Vitaly Lipatov's avatar
Vitaly Lipatov committed
251 252 253
}


Vitaly Lipatov's avatar
Vitaly Lipatov committed
254 255
subst_option()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
256
    eval "[ -n \"\$$1\" ]" && echo "$2" || echo "$3"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
257 258 259 260 261 262
}

store_output()
{
    # use make_temp_file from etersoft-build-utils
    RC_STDOUT=$(mktemp)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
263 264
    local CMDSTATUS=$RC_STDOUT.pipestatus
    echo 1 >$CMDSTATUS
Vitaly Lipatov's avatar
Vitaly Lipatov committed
265
    #RC_STDERR=$(mktemp)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
266
    ( LANG=C $@ 2>&1 ; echo $? >$CMDSTATUS ) | tee $RC_STDOUT
Vitaly Lipatov's avatar
Vitaly Lipatov committed
267
    return "$(cat $CMDSTATUS)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
268
    # bashism
Vitaly Lipatov's avatar
Vitaly Lipatov committed
269
    # http://tldp.org/LDP/abs/html/bashver3.html#PIPEFAILREF
Vitaly Lipatov's avatar
Vitaly Lipatov committed
270
    #return $PIPESTATUS
Vitaly Lipatov's avatar
Vitaly Lipatov committed
271 272
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
273 274 275 276 277 278
showcmd_store_output()
{
    showcmd "$@"
    store_output "$@"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
279 280
clean_store_output()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
281
    rm -f $RC_STDOUT $RC_STDOUT.pipestatus
Vitaly Lipatov's avatar
Vitaly Lipatov committed
282 283
}

284 285
epm()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
286
    if [ -n "$PROGNAME" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
287

Vitaly Lipatov's avatar
Vitaly Lipatov committed
288 289
        local bashopt=''
        [ -n "$verbose" ] && bashopt='-x'
Vitaly Lipatov's avatar
Vitaly Lipatov committed
290

Vitaly Lipatov's avatar
Vitaly Lipatov committed
291 292 293 294
        $CMDSHELL $bashopt $PROGDIR/$PROGNAME --inscript "$@"
    else
        epm_main --inscript "$@"
    fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
295 296 297 298
}

sudoepm()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
299
    [ -n "$PROGNAME" ] || fatal "Can't use sudo epm call from the piped script"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
300

Vitaly Lipatov's avatar
Vitaly Lipatov committed
301 302
    local bashopt=''
    [ -n "$verbose" ] && bashopt='-x'
Vitaly Lipatov's avatar
Vitaly Lipatov committed
303

Vitaly Lipatov's avatar
Vitaly Lipatov committed
304
    sudorun $CMDSHELL $bashopt $PROGDIR/$PROGNAME --inscript "$@"
305
}
Vitaly Lipatov's avatar
Vitaly Lipatov committed
306 307 308

fatal()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
309 310 311 312
    if [ -z "$TEXTDOMAIN" ] ; then
        echo "Error: $*  (you can discuss the problem in Telegram: https://t.me/useepm)" >&2
    fi
    exit 1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
313 314
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
315 316
warning()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
317 318 319
    if [ -z "$TEXTDOMAIN" ] ; then
        echo "Warning: $*" >&2
    fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
320 321
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
322 323
info()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
324
    [ -n "$quiet" ] && return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
325

Vitaly Lipatov's avatar
Vitaly Lipatov committed
326 327 328 329 330 331 332
    # print message to stderr if stderr forwarded to (a file)
    if isatty2 ; then
        isatty || return 0
        echo "$*"
    else
        echo "$*" >&2
    fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
333 334
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
335 336
SUDO_TESTED=''
SUDO_CMD='sudo'
Vitaly Lipatov's avatar
Vitaly Lipatov committed
337 338
set_sudo()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
    local nofail="$1"

    # cache the result
    [ -n "$SUDO_TESTED" ] && return "$SUDO_TESTED"
    SUDO_TESTED="0"

    SUDO=""
    # skip SUDO if disabled
    [ -n "$EPMNOSUDO" ] && return
    if [ "$DISTRNAME" = "Cygwin" ] || [ "$DISTRNAME" = "Windows" ] ; then
        # skip sudo using on Windows
        return
    fi

    # if we are root, do not need sudo
    is_root && return

    # start error section
    SUDO_TESTED="1"

    if ! is_command $SUDO_CMD ; then
        [ "$nofail" = "nofail" ] || SUDO="fatal 'Can't find sudo. Please install and tune sudo ('# epm install sudo') or run epm under root.'"
        return "$SUDO_TESTED"
    fi

    # if input is a console
    if inputisatty && isatty && isatty2 ; then
        if ! $SUDO_CMD -l >/dev/null ; then
            [ "$nofail" = "nofail" ] || SUDO="fatal 'Can't use sudo (only passwordless sudo is supported in non interactive using). Please run epm under root.'"
            return "$SUDO_TESTED"
        fi
    else
        # use sudo if one is tuned and tuned without password
        if ! $SUDO_CMD -l -n >/dev/null 2>/dev/null ; then
            [ "$nofail" = "nofail" ] || SUDO="fatal 'Can't use sudo (only passwordless sudo is supported). Please run epm under root or check http://altlinux.org/sudo '"
            return "$SUDO_TESTED"
        fi
    fi

    SUDO_TESTED="0"
    # FIXME: does not work: sudo -- VARIABLE=some command
    SUDO="$SUDO_CMD"
    #SUDO="$SUDO_CMD --"
    # check for < 1.7 version which do not support -- (and --help possible too)
    #$SUDO_CMD -h 2>/dev/null | grep -q "  --" || SUDO="$SUDO_CMD"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
384

Vitaly Lipatov's avatar
Vitaly Lipatov committed
385 386
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
387 388
sudo_allowed()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
389
    set_sudo nofail
Vitaly Lipatov's avatar
Vitaly Lipatov committed
390 391
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
392 393
withtimeout()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
394 395 396 397 398 399 400 401 402
    local TO=$(print_command_path timeout || print_command_path gtimeout)
    if [ -x "$TO" ] ; then
        $TO "$@"
        return
    fi
    fatal "Possible indefinite wait due timeout command is missed"
    # fallback: drop time arg and run without timeout
    #shift
    #"$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
403 404
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
405 406
set_eatmydata()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
407 408 409 410 411 412 413 414 415 416 417
    # don't use eatmydata (useless)
    return 0
    # skip if disabled
    [ -n "$EPMNOEATMYDATA" ] && return
    # use if possible
    is_command eatmydata || return
    set_sudo
    # FIXME: check if SUDO already has eatmydata
    [ -n "$SUDO" ] && SUDO="$SUDO eatmydata" || SUDO="eatmydata"
    [ -n "$verbose" ] && info "Uwaga! eatmydata is installed, we will use it for disable all sync operations."
    return 0
Vitaly Lipatov's avatar
Vitaly Lipatov committed
418 419
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
420
__get_package_for_command()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
421
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
422 423 424 425 426 427 428 429
    case "$1" in
        equery|revdep-rebuild)
            echo 'gentoolkit'
            ;;
        update-kernel|remove-old-kernels)
            echo 'update-kernel'
            ;;
    esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
430 431
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
432 433 434 435 436 437 438 439 440 441 442 443 444 445
confirm() {
    local response
    # call with a prompt string or use a default
    read -r -p "${1:-Are you sure? [y/N]} " response
    case $response in
        [yY][eE][sS]|[yY])
            true
            ;;
        *)
            false
            ;;
    esac
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
446 447 448

confirm_info()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
449 450 451 452
    info "$*"
    if [ -z "$non_interactive" ] ; then
        confirm "Are you sure? [y/N]" || fatal "Exiting"
    fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
453 454 455 456

}


Vitaly Lipatov's avatar
Vitaly Lipatov committed
457 458
is_root()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
459 460
    local EFFUID="$(id -u)"
    [ "$EFFUID" = "0" ]
Vitaly Lipatov's avatar
Vitaly Lipatov committed
461 462
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
463 464
assure_root()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
465
    is_root || fatal "run me only under root"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
466 467 468 469
}

regexp_subst()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
470 471 472
    local expression="$1"
    shift
    sed -i -r -e "$expression" "$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
473 474
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
475 476
assure_exists()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
477 478 479 480 481
    local package="$2"
    local textpackage=
    [ -n "$package" ] || package="$(__get_package_for_command "$1")"
    [ -n "$3" ] && textpackage=" >= $3"
    ( direct='' epm_assure "$1" $package $3 ) || fatal "Can't assure in '$1' command from $package$textpackage package"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
482 483
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
484 485
assure_exists_erc()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
486 487
    local package="erc"
    ( direct='' epm_assure "$package" ) || epm ei erc || fatal "erc is not available to install."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
488 489
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
490 491
disabled_eget()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
492 493 494 495 496 497 498
    local EGET
    # use internal eget only if exists
    if [ -s $SHAREDIR/tools_eget ] ; then
        ( EGET_BACKEND=$eget_backend $SHAREDIR/tools_eget "$@" )
        return
    fi
    fatal "Internal error: missed tools_eget"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
499

Vitaly Lipatov's avatar
Vitaly Lipatov committed
500 501 502 503 504
    # FIXME: we need disable output here, eget can be used for get output
    assure_exists eget eget 3.3 >/dev/null
    # run external command, not the function
    EGET=$(print_command_path eget) || fatal "Missed command eget from installed package eget"
    $EGET "$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
505 506
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
507 508
disabled_erc()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
509 510 511 512 513 514 515
    local ERC
    # use internal eget only if exists
    if [ -s $SHAREDIR/tools_erc ] ; then
        $SHAREDIR/tools_erc "$@"
        return
    fi
    fatal "Internal error: missed tools_erc"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
516

Vitaly Lipatov's avatar
Vitaly Lipatov committed
517 518 519 520 521
    # FIXME: we need disable output here, ercat can be used for get output
    assure_exists_erc >/dev/null
    # run external command, not the function
    ERC=$(print_command_path erc) || fatal "Missed command erc from installed package erc"
    $ERC "$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
522 523 524 525
}

disabled_ercat()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
526 527 528 529 530 531 532
    local ERCAT
    # use internal eget only if exists
    if [ -s $SHAREDIR/tools_ercat ] ; then
        $SHAREDIR/tools_ercat "$@"
        return
    fi
    fatal "Internal error: missed tools_ercat"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
533

Vitaly Lipatov's avatar
Vitaly Lipatov committed
534 535 536 537 538
    # FIXME: we need disable output here, ercat can be used for get output
    assure_exists_erc >/dev/null
    # run external command, not the function
    ERCAT=$(print_command_path ercat) || fatal "Missed command ercat from installed package erc"
    $ERCAT "$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
539 540
}

541 542
disabled_estrlist()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
543 544 545 546 547
    if [ -s $SHAREDIR/tools_estrlist ] ; then
        $SHAREDIR/tools_estrlist "$@"
        return
    fi
    fatal "missed tools_estrlist"
548 549 550 551
}

estrlist()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
552
    internal_tools_estrlist "$@"
553 554
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
555 556
eget()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
557 558 559 560 561
    # check for both
    # we really need that cross here,
    is_command curl || assure_exists wget
    is_command wget || assure_exists curl
    internal_tools_eget "$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
562 563
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
564 565
get_package_type()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
    local i
    case $1 in
        *.deb)
            echo "deb"
            return
            ;;
        *.rpm)
            echo "rpm"
            return
            ;;
        *.txz)
            echo "txz"
            return
            ;;
        *.tbz)
            echo "tbz"
            return
            ;;
        *.exe)
            echo "exe"
            return
            ;;
        *.msi)
            echo "msi"
            return
            ;;
        *.AppImage)
            echo "AppImage"
            return
            ;;
        *)
            #fatal "Don't know type of $1"
            # return package name for info
            echo "$1"
            return 1
            ;;
    esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
603 604 605
}


Vitaly Lipatov's avatar
Vitaly Lipatov committed
606 607
get_help()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
608 609 610
    if [ "$0" = "/dev/stdin" ] || [ "$0" = "sh" ] ; then
        return
    fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
611
    local F="$0"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
612 613 614
    if [ -n "$2" ] ; then
        is_dirpath "$2" && F="$2" || F="$(dirname $0)/$2"
    fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
615 616 617 618 619 620 621 622

    cat "$F" | grep -- "# $1" | while read -r n ; do
        if echo "$n" | grep -q "# $1: PART: " ; then
            echo
            echo "$n" | sed -e "s|# $1: PART: ||"
            continue
        fi
        echo "$n" | grep -q "^ *#" && continue
Vitaly Lipatov's avatar
Vitaly Lipatov committed
623 624 625
        opt=`echo $n | sed -e "s|) # $1:.*||g" -e 's|"||g' -e 's@^|@@'`
        desc=`echo $n | sed -e "s|.*) # $1:||g"`
        printf "    %-20s %s\n" "$opt" "$desc"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
626 627 628
    done
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
629
set_distro_info()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
630
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
631 632 633 634
    # use external distro_info if internal one is missed
    DISTRVENDOR=internal_distr_info
    [ -x $DISTRVENDOR ] || DISTRVENDOR=internal_distr_info
    export DISTRVENDOR
Vitaly Lipatov's avatar
Vitaly Lipatov committed
635

Vitaly Lipatov's avatar
Vitaly Lipatov committed
636 637 638 639 640 641 642
    [ -n "$DISTRNAME" ] || DISTRNAME=$($DISTRVENDOR -d) || fatal "Can't get distro name."
    [ -n "$DISTRVERSION" ] || DISTRVERSION=$($DISTRVENDOR -v)
    if [ -z "$DISTRARCH" ] ; then
        DISTRARCH=$($DISTRVENDOR --distro-arch)
    fi
    DISTRCONTROL="$($DISTRVENDOR -y)"
    [ -n "$BASEDISTRNAME" ] || BASEDISTRNAME=$($DISTRVENDOR -s)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
643

Vitaly Lipatov's avatar
Vitaly Lipatov committed
644 645 646 647 648
    # TODO: improve BIGTMPDIR conception
    # https://bugzilla.mozilla.org/show_bug.cgi?id=69938
    # https://refspecs.linuxfoundation.org/FHS_3.0/fhs/ch05s15.html
    # https://geekpeach.net/ru/%D0%BA%D0%B0%D0%BA-systemd-tmpfiles-%D0%BE%D1%87%D0%B8%D1%89%D0%B0%D0%B5%D1%82-tmp-%D0%B8%D0%BB%D0%B8-var-tmp-%D0%B7%D0%B0%D0%BC%D0%B5%D0%BD%D0%B0-tmpwatch-%D0%B2-centos-rhel-7
    [ -n "$BIGTMPDIR" ] || [ -d "/var/tmp" ] && BIGTMPDIR="/var/tmp" || BIGTMPDIR="/tmp"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
649 650 651 652
}

set_pm_type()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
653 654 655
    local CMD
    set_distro_info
    set_target_pkg_env
Vitaly Lipatov's avatar
Vitaly Lipatov committed
656 657

if [ -n "$FORCEPM" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
658 659
    PMTYPE=$FORCEPM
    return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
660 661
fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
662
    PMTYPE="$($DISTRVENDOR -g $DISTRNAME/$DISTRVERSION)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
663 664
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
665 666
is_active_systemd()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
667
    [ "$DISTRCONTROL" = "systemd" ]
Vitaly Lipatov's avatar
Vitaly Lipatov committed
668 669
}

670 671
assure_distr()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
672 673 674
    local TEXT="this option"
    [ -n "$2" ] && TEXT="$2"
    [ "$DISTRNAME" = "$1" ] || fatal "$TEXT supported only for $1 distro"
675 676
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
677 678 679
get_pkg_name_delimiter()
{
   local pkgtype="$1"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
680
   [ -n "$pkgtype" ] || pkgtype="$PKGFORMAT"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
681 682 683 684 685

   [ "$pkgtype" = "deb" ] && echo "_" && return
   echo "-"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709

__epm_remove_from_tmp_files()
{
   keep="$1"
   [ -r "$keep" ] || return 0
   [ -n "$to_remove_pkg_files" ] || return 0
   to_remove_pkg_files="$(echo "$to_remove_pkg_files" | sed -e "s|$keep||")"
}

__epm_remove_tmp_files()
{
    # TODO: move it to exit handler
    if [ -z "$DEBUG" ] ; then
        # TODO: reinvent
        [ -n "$to_remove_pkg_files" ] && rm -f $to_remove_pkg_files
        # hack??
        [ -n "$to_remove_pkg_files" ] && rmdir $(dirname $to_remove_pkg_files | head -n1) 2>/dev/null
        [ -n "$to_remove_pkg_dirs" ] && rmdir $to_remove_pkg_dirs 2>/dev/null
        [ -n "$to_clean_tmp_dirs" ] && rm -rf $to_clean_tmp_dirs 2>/dev/null
    fi
    return 0
}


Vitaly Lipatov's avatar
Vitaly Lipatov committed
710 711
has_space()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
712
    estrlist -- has_space "$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
713 714
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
715 716 717 718 719
is_url()
{
    echo "$1" | grep -q "^[filehtps]*:/"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
720
if a= which which 2>/dev/null >/dev/null ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
721 722 723 724
    # the best case if we have which command (other ways needs checking)
    # TODO: don't use which at all, it is binary, not builtin shell command
print_command_path()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
725
    a= which -- "$1" 2>/dev/null
Vitaly Lipatov's avatar
Vitaly Lipatov committed
726
}
Vitaly Lipatov's avatar
Vitaly Lipatov committed
727
elif a= type -a type 2>/dev/null >/dev/null ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
728 729
print_command_path()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
730
    a= type -fpP -- "$1" 2>/dev/null
Vitaly Lipatov's avatar
Vitaly Lipatov committed
731 732 733 734
}
else
print_command_path()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
735
    a= type "$1" 2>/dev/null | sed -e 's|.* /|/|'
Vitaly Lipatov's avatar
Vitaly Lipatov committed
736 737 738 739 740 741 742 743 744
}
fi

is_command()
{
    print_command_path "$1" >/dev/null
}


Vitaly Lipatov's avatar
Vitaly Lipatov committed
745
if ! is_command realpath ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
746 747 748 749 750 751 752
realpath()
{
    [ -n "$*" ] || return
    readlink -f "$@"
}
fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
753

Vitaly Lipatov's avatar
Vitaly Lipatov committed
754 755

if ! is_command subst ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
756 757 758 759 760 761
subst()
{
    sed -i -e "$@"
}
fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
762 763 764

check_core_commands()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
765 766 767
    #which which >/dev/null || fatal "Can't find which command (which or debianutils package is missed?)"
    is_command grep || fatal "Can't find grep command (coreutils package is missed?)"
    is_command sed || fatal "Can't find sed command (sed package is missed?)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
768 769 770
}


Vitaly Lipatov's avatar
Vitaly Lipatov committed
771 772 773 774
# File bin/serv-cat:

serv_cat()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
    local SERVICE="$1"
    shift

    case $SERVICETYPE in
        systemd)
            docmd systemctl cat "$SERVICE" "$@"
            ;;
        *)
            case $BASEDISTRNAME in
            "alt")
                local INITFILE=/etc/init.d/$SERVICE
                [ -r "$INITFILE" ] || fatal "Can't find init file $INITFILE"
                docmd cat $INITFILE
                return ;;
            *)
                fatal "Have no suitable for $DISTRNAME command for $SERVICETYPE"
                ;;
            esac
    esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
794 795
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
796 797 798 799
# File bin/serv-common:

serv_common()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
    local SERVICE="$1"
    shift
    case $SERVICETYPE in
        service-chkconfig|service-upstart)
            if is_anyservice $SERVICE ; then
                fatal "Have no idea how to call anyservice service with args"
            fi
            sudocmd service $SERVICE "$@"
            ;;
        service-initd|service-update)
            sudocmd $INITDIR/$SERVICE "$@"
            ;;
        systemd)
            # run init script directly (for nonstandart commands)
            if [ -x $INITDIR/$SERVICE ] ; then
                sudocmd $INITDIR/$SERVICE "$@"
            else
                sudocmd systemctl "$@" $SERVICE
            fi
            ;;
        runit)
            sudocmd sv $SERVICE "$@"
            ;;
        *)
            fatal "Have no suitable command for $SERVICETYPE"
            ;;
    esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
827 828 829 830 831 832 833
}

# File bin/serv-disable:


serv_disable()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
    local SERVICE="$1"

    is_service_autostart $1 || { info "Service $1 already disabled for startup" && return ; }

    case $SERVICETYPE in
        service-chkconfig|service-upstart)
            if is_anyservice $SERVICE ; then
                sudocmd anyservice $SERVICE off
                return
            fi
            sudocmd chkconfig $1 off
            ;;
        service-initd|service-update)
            sudocmd update-rc.d $1 remove
            ;;
        systemd)
            sudocmd systemctl disable $1
            ;;
        openrc)
            sudocmd rc-update del $1 default
            ;;
        runit)
            sudocmd rm -fv /var/service/$SERVICE
            ;;
        *)
            fatal "Have no suitable command for $SERVICETYPE"
            ;;
    esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
862 863
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
864 865 866 867
# File bin/serv-edit:

serv_edit()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
868 869
    local SERVICE="$1"
    shift
Vitaly Lipatov's avatar
Vitaly Lipatov committed
870

Vitaly Lipatov's avatar
Vitaly Lipatov committed
871 872 873 874 875 876 877 878
    case $SERVICETYPE in
        systemd)
            sudocmd systemctl edit "$@" "$SERVICE"
            ;;
        *)
            fatal "Have no suitable for $DISTRNAME command for $SERVICETYPE"
            ;;
    esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
879 880
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
881 882 883
# File bin/serv-enable:


Vitaly Lipatov's avatar
Vitaly Lipatov committed
884
serv_enable()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
885
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
    local SERVICE="$1"

    is_service_autostart $1 && info "Service $1 is already enabled for startup" && return

    case $SERVICETYPE in
        service-chkconfig)
            if is_anyservice $SERVICE ; then
                sudocmd anyservice $SERVICE on
                return
            fi
            sudocmd chkconfig --add $1 || return
            sudocmd chkconfig $1 on
            ;;
        service-upstart)
            sudocmd chkconfig --add $1 || return
            sudocmd chkconfig $1 on
            ;;
        service-initd|service-update)
            sudocmd update-rc.d $1 defaults
            ;;
        systemd)
            sudocmd systemctl enable $1
            ;;
        openrc)
            sudocmd rc-update add $1 default
            ;;
        runit)
            assure_exists $SERVICE
            [ -r "/etc/sv/$SERVICE" ] || fatal "Can't find /etc/sv/$SERVICE"
            sudocmd ln -s /etc/sv/$SERVICE /var/service/
            ;;
        *)
            fatal "Have no suitable command for $SERVICETYPE"
            ;;
    esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
921 922
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
923 924 925
# File bin/serv-exists:

serv_exists()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
926
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
    local SERVICE="$1"
    shift

    case $SERVICETYPE in
        systemd)
            # too direct way: test -s /lib/systemd/system/dm.service
            docmd systemctl cat "$SERVICE" "$@" >/dev/null 2>/dev/null
            ;;
        *)
            case $BASEDISTRNAME in
            "alt")
                local INITFILE=/etc/init.d/$SERVICE
                [ -r "$INITFILE" ] || return
                return ;;
            *)
                fatal "Have no suitable for $DISTRNAME command for $SERVICETYPE"
                ;;
            esac
    esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
946 947
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
948 949 950 951
# File bin/serv-list:

serv_list()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
    [ -n "$short" ] || info "Running services:"
    case $SERVICETYPE in
        service-upstart)
            sudocmd initctl list
            ;;
        service-update)
            sudocmd service --status-all
            ;;
        systemd)
            if [ -n "$short" ] ; then
                docmd systemctl list-units --type=service "$@" | grep '\.service' | sed -e 's|\.service.*||' -e 's|^ *||'
            else
                docmd systemctl list-units --type=service "$@"
            fi
            ;;
        openrc)
            sudocmd rc-status
            ;;
        *)
            # hack to improve list speed
            [ "$UID" = 0 ] || { sudocmd $PROGDIR/serv --quiet list ; return ; }
            for i in $(quiet=1 serv_list_all) ; do
                is_service_running $i >/dev/null && echo $i
            done
            ;;
    esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
978 979 980 981 982 983
}

# File bin/serv-list_all:

serv_list_all()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
    case $SERVICETYPE in
        service-chkconfig|service-upstart)
            if [ -n "$short" ] ; then
                # service --status-all for Ubuntu/Fedora
                sudocmd chkconfig --list | cut -f1 | grep -v "^$" | grep -v "xinetd:$" | cut -f 1 -d" "
            else
                # service --status-all for Ubuntu/Fedora
                sudocmd chkconfig --list | cut -f1 | grep -v "^$" | grep -v "xinetd:$"
            fi
            if [ -n "$ANYSERVICE" ] ; then
                if [ -n "$short" ] ; then
                    sudocmd anyservice --quiet list | cut -f 1 -d" "
                else
                    sudocmd anyservice --quiet list
                fi
                return
            fi
            ;;
        service-initd|service-update)
            if [ -n "$short" ] ; then
                sudocmd ls $INITDIR/ | grep -v README | cut -f 1 -d" "
            else
                sudocmd ls $INITDIR/ | grep -v README
            fi
            ;;
        systemd)
            if [ -n "$short" ] ; then
                docmd systemctl list-unit-files --type=service "$@" | sed -e 's|\.service.*||' | grep -v 'unit files listed' | grep -v '^$'
            else
                docmd systemctl list-unit-files --type=service "$@"
            fi
            ;;
        openrc)
            if [ -n "$short" ] ; then
                sudocmd rc-service -l | cut -f 1 -d" "
            else
                sudocmd rc-service -l
            fi
            ;;
        *)
            fatal "Have no suitable command for $SERVICETYPE"
            ;;
    esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1027 1028
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1029 1030 1031 1032
# File bin/serv-list_failed:

serv_list_failed()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
    case $SERVICETYPE in
        systemd)
            sudocmd systemctl --failed
            ;;
        *)
            for i in $(short=1 serv_list_startup) ; do
                is_service_running >/dev/null $i && continue
                echo ; echo $i
                serv_status $i
            done
            ;;
    esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1045 1046
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1047 1048 1049 1050
# File bin/serv-list_startup:

serv_list_startup()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
    case $SERVICETYPE in
        systemd)
            #sudocmd systemctl list-unit-files
            # TODO: native command? implement --short for list (only names)
            for i in $(short=1 serv_list_all) ; do
                is_service_autostart >/dev/null 2>/dev/null $i && echo $i
            done
            ;;
        *)
            for i in $(short=1 serv_list_all) ; do
                is_service_autostart >/dev/null 2>/dev/null $i && echo $i
            done
            ;;
    esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1065 1066
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1067 1068 1069 1070
# File bin/serv-log:

__serv_log_altlinux()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
    local SERVICE="$1"
    local PRG="less"
    [ "$2" = "-f" ] && PRG="tail -f"

    case "$SERVICE" in
        postfix)
            sudocmd $PRG /var/log/mail/all /var/log/mail/errors
            ;;
        sshd)
            sudocmd $PRG /var/log/auth/all
            ;;
        cups)
            sudocmd $PRG /var/log/cups/access_log /var/log/cups/error_log
            ;;
        fail2ban)
            sudocmd $PRG /var/log/$SERVICE.log
            ;;
        *)
            fatal "Have no suitable for $SERVICE service"
            ;;
    esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1092 1093 1094 1095
}

serv_log()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
    local SERVICE="$1"
    shift

    case $SERVICETYPE in
        systemd)
            sudocmd journalctl -b -u "$SERVICE" "$@"
            ;;
        *)
            case $BASEDISTRNAME in
            "alt")
                FF="" ; [ "$1" = "-f" ] && FF="-f"
                __serv_log_altlinux "$SERVICE" $FF
                return ;;
            *)
                fatal "Have no suitable for $DISTRNAME command for $SERVICETYPE"
                ;;
            esac
    esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1114 1115
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1116 1117 1118 1119 1120
# File bin/serv-off:


serv_off()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1121
    local SERVICE="$1"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1122

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1123 1124 1125
    is_service_running $1 && { serv_stop $1 || return ; }
    is_service_autostart $1 || { info "Service $1 already disabled for startup" && return ; }
    serv_disable $SERVICE
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1126 1127 1128 1129 1130 1131 1132
}

# File bin/serv-on:


serv_on()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1133 1134 1135 1136
    serv_enable "$1" || return
    # start if need
    is_service_running $1 && info "Service $1 is already running" && return
    serv_start $1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1137 1138
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1139 1140 1141 1142
# File bin/serv-print:

serv_print()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1143 1144
    echo "Detected init system: $SERVICETYPE"
    [ -n "$ANYSERVICE" ] && echo "anyservice is detected too"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1145 1146 1147 1148 1149 1150 1151
}

# File bin/serv-reload:


serv_reload()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
    local SERVICE="$1"
    shift

    case $SERVICETYPE in
        service-chkconfig|service-upstart)
            if is_anyservice $SERVICE ; then
                sudocmd anyservice $SERVICE reload
                return
            fi
            sudocmd service $SERVICE reload "$@"
            ;;
        service-initd|service-update)
            sudocmd $INITDIR/$SERVICE reload "$@"
            ;;
        systemd)
            sudocmd systemctl reload $SERVICE "$@"
            ;;
        *)
            info "Fallback to restart..."
            serv_restart "$SERVICE" "$@"
            ;;
    esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1174 1175
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1176 1177 1178 1179 1180
# File bin/serv-restart:


serv_restart()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
    local SERVICE="$1"
    shift

    case $SERVICETYPE in
        service-chkconfig|service-upstart)
            if is_anyservice $SERVICE ; then
                sudocmd anyservice $SERVICE restart
                return
            fi
            sudocmd service $SERVICE restart "$@"
            ;;
        service-initd|service-update)
            sudocmd $INITDIR/$SERVICE restart "$@"
            ;;
        systemd)
            sudocmd systemctl restart $SERVICE "$@"
            ;;
        runit)
            sudocmd sv restart "$SERVICE"
            ;;
        openrc)
            sudocmd rc-service restart "$SERVICE"
            ;;
        *)
            fatal "Have no suitable command for $SERVICETYPE"
            ;;
    esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1208 1209
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1210 1211 1212 1213
# File bin/serv-start:

serv_start()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
    local SERVICE="$1"
    shift

    case $SERVICETYPE in
        service-chkconfig|service-upstart)
            if is_anyservice $SERVICE ; then
                sudocmd anyservice $SERVICE start
                return
            fi
            sudocmd service $SERVICE start "$@"
            ;;
        service-initd|service-update)
            sudocmd $INITDIR/$SERVICE start "$@"
            ;;
        systemd)
            sudocmd systemctl start "$SERVICE" "$@"
            ;;
        runit)
            sudocmd sv up "$SERVICE"
            ;;
        openrc)
            sudocmd rc-service start "$SERVICE"
            ;;
        *)
            fatal "Have no suitable command for $SERVICETYPE"
            ;;
    esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1241 1242 1243 1244 1245 1246
}

# File bin/serv-status:

is_service_running()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
    local SERVICE="$1"
    local OUTPUT
    # TODO: real status can be checked only with grep output
    case $SERVICETYPE in
        service-chkconfig|service-upstart)
            if is_anyservice $1 ; then
                OUTPUT="$(sudorun anyservice $1 status 2>/dev/null)" || return 1
                echo "$OUTPUT" | grep -q "is stopped" && return 1
                return 0
            fi
            OUTPUT="$(sudorun service $1 status 2>/dev/null)" || return 1
            echo "$OUTPUT" | grep -q "is stopped" && return 1
            return 0
            ;;
        service-initd|service-update)
            sudorun $INITDIR/$1 status >/dev/null 2>/dev/null
            ;;
        systemd)
            a='' systemctl status $1 >/dev/null 2>/dev/null
            ;;
        runit)
            sudorun sv status "$SERVICE" >/dev/null 2>/dev/null
            ;;
        *)
            fatal "Have no suitable command for $SERVICETYPE"
            ;;
    esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1274 1275 1276 1277
}

is_service_autostart()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
    local SERVICE="$1"

    case $SERVICETYPE in
        service-chkconfig|service-upstart)
            if is_anyservice $SERVICE; then
                $ANYSERVICE $SERVICE isautostarted
                return
            fi

            # FIXME: check for current runlevel
            LANG=C sudorun chkconfig $1 --list | grep -q "[35]:on"
            ;;
        service-initd|service-update)
            test -L "$(echo /etc/rc5.d/S??$1)"
            ;;
        systemd)
            a='' systemctl is-enabled $1
            ;;
        runit)
            test -L "/var/service/$SERVICE"
            ;;
        *)
            fatal "Have no suitable command for $SERVICETYPE"
            ;;
    esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1303 1304 1305 1306
}

serv_status()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
    is_service_autostart $1 && echo "Service $1 is scheduled to run on startup" || echo "Service $1 will NOT run on startup"

    local SERVICE="$1"
    shift

    case $SERVICETYPE in
        service-chkconfig|service-upstart)
            if is_anyservice $SERVICE ; then
                sudocmd anyservice $SERVICE status
                return
            fi
            sudocmd service $SERVICE status "$@"
            ;;
        service-update)
            sudocmd $INITDIR/$SERVICE status "$@"
            ;;
        systemd)
            docmd systemctl -l status $SERVICE "$@"
            ;;
        runit)
            sudocmd sv status "$SERVICE"
            ;;
        *)
            fatal "Have no suitable command for $SERVICETYPE"
            ;;
    esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1333 1334 1335 1336 1337 1338
}

# File bin/serv-stop:

serv_stop()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
    local SERVICE="$1"
    shift

    case $SERVICETYPE in
        service-chkconfig|service-upstart)
            if is_anyservice $SERVICE ; then
                sudocmd anyservice $SERVICE stop
                return
            fi
            sudocmd service $SERVICE stop "$@"
            ;;
        service-initd|service-update)
            sudocmd $INITDIR/$SERVICE stop "$@"
            ;;
        systemd)
            sudocmd systemctl stop $SERVICE "$@"
            ;;
        runit)
            sudocmd sv down "$SERVICE"
            ;;
        openrc)
            sudocmd rc-service stop "$SERVICE"
            ;;
        *)
            fatal "Have no suitable command for $SERVICETYPE"
            ;;
    esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1366 1367
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1368 1369 1370 1371
# File bin/serv-test:

serv_test()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
    local SERVICE="$1"
    shift

    case $SERVICE in
        cups|cupsd)
            docmd cupsd -t
            ;;
        nginx)
            docmd nginx -t
            ;;
        sshd)
            docmd sshd -t
            ;;
        httpd2|httpd|apache|apache2)
            if is_command httpd2 ; then
                docmd httpd2 -t
            elif is_command apache2 ; then
                docmd apache2 -t
            fi
            ;;
        postfix)
            docmd /etc/init.d/postfix check
            ;;
        *)
            fatal "$SERVICE is not supported yet. Please report if you know how to test"
            ;;
    esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1399 1400
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1401 1402 1403 1404 1405
# File bin/serv-try_restart:


serv_try_restart()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1406 1407
    local SERVICE="$1"
    shift
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1408

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
    case $SERVICETYPE in
        systemd)
            sudocmd systemctl try-restart $SERVICE "$@"
            ;;
        *)
            info "Fallback to restart..."
            is_service_running $SERVICE || { info "Service $SERVICE is not running, restart skipping…" ; return 0 ; }
            serv_restart "$SERVICE" "$@"
            ;;
    esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1419 1420 1421 1422 1423 1424
}

# File bin/serv-usage:

_print_additional_usage()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1425
    echo "serv addition usage: {on|off|try-restart|usage}"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1426 1427 1428 1429
}

serv_usage()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
    local SERVICE="$1"
    shift
    case $SERVICETYPE in
        service-chkconfig|service-upstart)
            # CHECKME: many services print out usage in stderr, it conflicts with printout command
            #sudocmd service $SERVICE 2>&1
            sudorun service $SERVICE 2>&1
            ;;
        service-initd|service-update)
            #sudocmd /etc/init.d/$SERVICE 2>&1
            sudorun service $SERVICE 2>&1
            ;;
        systemd)
            sudocmd systemctl $SERVICE 2>&1
            ;;
        *)
            fatal "Have no suitable command for $SERVICETYPE"
            ;;
    esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1449 1450 1451 1452

_print_additional_usage

}
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1453

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1454
################# incorporate bin/distr_info #################
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1455 1456
internal_distr_info()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1457 1458 1459
# 2007-2023 (c) Vitaly Lipatov <lav@etersoft.ru>
# 2007-2023 (c) Etersoft
# 2007-2023 Public domain
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1460 1461 1462 1463

# You can set ROOTDIR to root system dir
#ROOTDIR=

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1464
PROGVERSION="20230406"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1465

1466 1467
# TODO: check /etc/system-release

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1468 1469 1470
# Check for DISTRO specific file in /etc
distro()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1471 1472 1473 1474
    #[ -n "$ROOTDIR" ] || return
    # fill global DISTROFILE
    DISTROFILE="$ROOTDIR/etc/$1"
    [ -f "$DISTROFILE" ]
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1475 1476 1477 1478 1479
}

# Has a distro file the specified word?
has()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1480 1481
    [ -n "$DISTROFILE" ] || exit 1
    grep "$*" "$DISTROFILE" >/dev/null 2>&1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1482 1483
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1484 1485 1486 1487 1488 1489
# copied from epm-sh-functions
# print a path to the command if exists in $PATH
if a= which which 2>/dev/null >/dev/null ; then
    # the best case if we have which command (other ways needs checking)
    # TODO: don't use which at all, it is binary, not builtin shell command
print_command_path()
1490
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1491
    a= which -- "$1" 2>/dev/null
1492
}
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
elif a= type -a type 2>/dev/null >/dev/null ; then
print_command_path()
{
    a= type -fpP -- "$1" 2>/dev/null
}
else
print_command_path()
{
    a= type "$1" 2>/dev/null | sed -e 's|.* /|/|'
}
fi

# check if <arg> is a real command
is_command()
{
    print_command_path "$1" >/dev/null
}
##########################3

1512

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1513 1514
firstupper()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1515 1516
    # FIXME: works with GNU sed only
    echo "$*" | sed 's/.*/\u&/'
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1517 1518
}

1519 1520
tolower()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1521 1522 1523
    # tr is broken in busybox (checked with OpenWrt)
    #echo "$*" | tr "[:upper:]" "[:lower:]"
    echo "$*" | awk '{print tolower($0)}'
1524 1525
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1526 1527
print_bug_report_url()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1528
    echo "$BUG_REPORT_URL"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1529 1530
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1531 1532
override_distrib()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543
    [ -n "$1" ] || return
    VENDOR_ID=''
    PRETTY_NAME=''
    local name="$(echo "$1" | sed -e 's|x86_64/||')"
    [ "$name" = "$1" ] && DIST_ARCH="x86" || DIST_ARCH="x86_64"
    DISTRIB_ID="$(echo "$name" | sed -e 's|/.*||')"
    DISTRO_NAME="$DISTRIB_ID"
    DISTRIB_RELEASE="$(echo "$name" | sed -e 's|.*/||')"
    [ "$DISTRIB_ID" = "$DISTRIB_RELEASE" ] && DISTRIB_RELEASE=''
    DISTRIB_CODENAME="$DISTRIB_RELEASE"
    DISTRIB_FULL_RELEASE="$DISTRIB_RELEASE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1544

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1545 1546
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1547 1548
# Translate DISTRIB_ID to vendor name (like %_vendor does or package release name uses), uses VENDOR_ID by default
pkgvendor()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1549
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
    [ "$DISTRIB_ID" = "ALTLinux" ] && echo "alt" && return
    [ "$DISTRIB_ID" = "ALTServer" ] && echo "alt" && return
    [ "$DISTRIB_ID" = "MOC" ] && echo "alt" && return
    [ "$DISTRIB_ID" = "MESh" ] && echo "alt" && return
    [ "$DISTRIB_ID" = "AstraLinuxSE" ] && echo "astra" && return
    [ "$DISTRIB_ID" = "AstraLinuxCE" ] && echo "astra" && return
    [ "$DISTRIB_ID" = "LinuxXP" ] && echo "lxp" && return
    [ "$DISTRIB_ID" = "TinyCoreLinux" ] && echo "tcl" && return
    [ "$DISTRIB_ID" = "VoidLinux" ] && echo "void" && return
    [ "$DISTRIB_ID" = "OpenSUSE" ] && echo "suse" && return
    [ "$DISTRIB_ID" = "openSUSETumbleweed" ] && echo "suse" && return
    [ "$DISTRIB_ID" = "openSUSELeap" ] && echo "suse" && return
    if [ -n "$VENDOR_ID" ] ; then
        [ "$VENDOR_ID" = "altlinux" ] && echo "alt" && return
        echo "$VENDOR_ID"
        return
    fi
    tolower "$DISTRIB_ID"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1568 1569
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1570 1571 1572 1573 1574 1575 1576 1577 1578
# TODO: in more appropriate way
#which pkcon 2>/dev/null >/dev/null && info "You can run $ PMTYPE=packagekit epm to use packagekit backend"

# Print package manager (need DISTRIB_ID var)
pkgmanager()
{
local CMD
# FIXME: some problems with multibased distros (Server Edition on CentOS and Desktop Edition on Ubuntu)
case $DISTRIB_ID in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
    ALTLinux|ALTServer)
        #which ds-install 2>/dev/null >/dev/null && CMD=deepsolver-rpm
        #which pkcon 2>/dev/null >/dev/null && CMD=packagekit-rpm
        CMD="apt-rpm"
        ;;
    ALTServer)
        CMD="apt-rpm"
        ;;
    PCLinux)
        CMD="apt-rpm"
        ;;
    Ubuntu|Debian|Mint|OSNovaLinux|AstraLinux*|Elbrus)
        CMD="apt-dpkg"
        #which aptitude 2>/dev/null >/dev/null && CMD=aptitude-dpkg
        #is_command snappy && CMD=snappy
        ;;
    Solus)
        CMD="eopkg"
        ;;
    Mandriva)
        CMD="urpm-rpm"
        ;;
    ROSA)
        CMD="urpm-rpm"
        is_command yum && CMD="yum-rpm"
        is_command dnf && CMD="dnf-rpm"
        # use dnf since 2020
        #[ "$DISTRIB_ID/$DISTRIB_RELEASE" = "ROSA/2020" ] && CMD="urpm-rpm"
        ;;
    FreeBSD|NetBSD|OpenBSD|Solaris)
        CMD="pkgsrc"
        is_command pkg && CMD=pkgng
        ;;
    Gentoo)
        CMD="emerge"
        ;;
    ArchLinux)
        CMD="pacman"
        ;;
    Fedora|CentOS|OracleLinux|RockyLinux|AlmaLinux|RHEL|RELS|Scientific|GosLinux|Amzn|RedOS)
        CMD="dnf-rpm"
        is_command dnf || CMD="yum-rpm"
        [ "$DISTRIB_ID/$DISTRIB_RELEASE" = "CentOS/7" ] && CMD="yum-rpm"
        ;;
    Slackware)
        CMD="slackpkg"
        ;;
    SUSE|SLED|SLES|openSUSETumbleweed|openSUSELeap)
        CMD="zypper-rpm"
        ;;
    ForesightLinux|rPathLinux)
        CMD="conary"
        ;;
    Windows)
        CMD="appget"
        is_command $CMD || CMD="chocolatey"
        is_command $CMD || CMD="winget"
        ;;
    MacOS)
        CMD="homebrew"
        ;;
    OpenWrt)
        CMD="opkg"
        ;;
    GNU/Linux/Guix)
        CMD="guix"
        ;;
    Android)
        CMD="android"
        ;;
    Cygwin)
        CMD="aptcyg"
        ;;
    AlpineLinux)
        CMD="apk"
        ;;
    TinyCoreLinux)
        CMD="tce"
        ;;
    VoidLinux)
        CMD="xbps"
        ;;
    *)
        # try detect firstly
        if grep -q "ID_LIKE=debian" /etc/os-release 2>/dev/null ; then
            echo "apt-dpkg" && return
        fi

        if is_command "rpm" && [ -s /var/lib/rpm/Name ] || [ -s /var/lib/rpm/rpmdb.sqlite ] ; then
            is_command "zypper" && echo "zypper-rpm" && return
            is_command "dnf" && echo "dnf-rpm" && return
            is_command "apt-get" && echo "apt-rpm" && return
            is_command "yum" && echo "yum-rpm" && return
            is_command "urpmi" && echo "urpm-rpm" && return
        fi

        if is_command "dpkg" && [ -s /var/lib/dpkg/status ] ; then
            is_command "apt" && echo "apt-dpkg" && return
            is_command "apt-get" && echo "apt-dpkg" && return
        fi

        echo "We don't support yet DISTRIB_ID $DISTRIB_ID" >&2
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1682 1683 1684 1685
esac
echo "$CMD"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1686 1687 1688 1689 1690
# Print pkgtype (need DISTRIB_ID var)
pkgtype()
{
# TODO: try use generic names
    case $(pkgvendor) in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713
        freebsd) echo "tbz" ;;
        sunos) echo "pkg.gz" ;;
        slackware|mopslinux) echo "tgz" ;;
        archlinux|manjaro) echo "pkg.tar.xz" ;;
        gentoo) echo "tbz2" ;;
        windows) echo "exe" ;;
        android) echo "apk" ;;
        alpine) echo "apk" ;;
        tinycorelinux) echo "tcz" ;;
        voidlinux) echo "xbps" ;;
        openwrt) echo "ipk" ;;
        cygwin) echo "tar.xz" ;;
        solus) echo "eopkg" ;;
        *)
            case $(pkgmanager) in
                *-dpkg)
                    echo "deb" ;;
                *-rpm)
                    echo "rpm" ;;
                *)
                    echo "rpm" ;;
            esac
    esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1714 1715
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1716 1717
print_codename()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1718
    echo "$DISTRIB_CODENAME"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1719
}
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1720

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1721 1722
print_repo_name()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1723
    echo "$DISTRIB_CODENAME"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1724 1725
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1726 1727
get_var()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1728 1729
    # get first variable and print it out, drop quotes if exists
    grep -i "^$1 *=" | head -n 1 | sed -e "s/^[^=]*[ \t]*=[ \t]*//" | sed -e "s/^[\'\"]\(.*\)[\'\"]/\1/"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1730 1731 1732 1733 1734
}

# 2010.1 -> 2010
get_major_version()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1735
    echo "$1" | sed -e "s/\..*//g"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1736 1737
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1738 1739
normalize_name()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
    case "$1" in
        "RED OS")
            echo "RedOS"
            ;;
        "Debian GNU/Linux")
            echo "Debian"
            ;;
        "CentOS Linux")
            echo "CentOS"
            ;;
        "Fedora Linux")
            echo "Fedora"
            ;;
        "Red Hat Enterprise Linux Server")
            echo "RHEL"
            ;;
        "ROSA Fresh"*|"ROSA Desktop Fresh"*)
            echo "ROSA"
            ;;
        "ROSA Chrome Desktop")
            echo "ROSA"
            ;;
        "ROSA Enterprise Linux Desktop")
            echo "RELS"
            ;;
        "ROSA Enterprise Linux Server")
            echo "RELS"
            ;;
        *)
            #echo "${1// /}"
            #firstupper "$1" | sed -e "s/ //g" -e 's|(.*||'
            echo "$1" | sed -e "s/ //g" -e 's|(.*||'
            ;;
    esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1774 1775
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1776 1777
# 1.2.3.4.5 -> 1
normalize_version1()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1778
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1779
    echo "$1" | sed -e "s|\..*||"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1780 1781
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794
# 1.2.3.4.5 -> 1.2
normalize_version2()
{
    echo "$1" | sed -e "s|^\([^.][^.]*\.[^.][^.]*\)\..*|\1|"
}

# 1.2.3.4.5 -> 1.2.3
normalize_version3()
{
    echo "$1" | sed -e "s|^\([^.][^.]*\.[^.][^.]*\.[^.][^.]*\)\..*|\1|"
}


Vitaly Lipatov's avatar
Vitaly Lipatov committed
1795 1796
fill_distr_info()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1797
# Default values
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1798
PRETTY_NAME=""
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1799
DISTRIB_ID=""
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1800
DISTRIB_RELEASE=""
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1801 1802
DISTRIB_FULL_RELEASE=""
DISTRIB_RELEASE_ORIG=""
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1803
DISTRIB_CODENAME=""
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1804 1805
BUG_REPORT_URL=""
BUILD_ID=""
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1806

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1807
# Default detection by /etc/os-release
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1808 1809
# https://www.freedesktop.org/software/systemd/man/os-release.html
if distro os-release ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821
    # shellcheck disable=SC1090
    . $DISTROFILE
    DISTRO_NAME="$NAME"
    DISTRIB_ID="$(normalize_name "$NAME")"
    DISTRIB_RELEASE_ORIG="$VERSION_ID"
    DISTRIB_RELEASE="$VERSION_ID"
    [ -n "$DISTRIB_RELEASE" ] || DISTRIB_RELEASE="CUR"
    # set by os-release:
    #PRETTY_NAME
    VENDOR_ID="$ID"
    DISTRIB_FULL_RELEASE="$DISTRIB_RELEASE"
    DISTRIB_CODENAME="$VERSION_CODENAME"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1822

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1823
elif distro lsb-release ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1824 1825 1826 1827 1828 1829 1830
    DISTRIB_ID=$(cat $DISTROFILE | get_var DISTRIB_ID)
    DISTRO_NAME=$(cat $DISTROFILE | get_var DISTRIB_ID)
    DISTRIB_RELEASE="$(cat $DISTROFILE | get_var DISTRIB_RELEASE)"
    DISTRIB_RELEASE_ORIG="$DISTRIB_RELEASE"
    DISTRIB_FULL_RELEASE="$DISTRIB_RELEASE"
    DISTRIB_CODENAME=$(cat $DISTROFILE | get_var DISTRIB_CODENAME)
    PRETTY_NAME=$(cat $DISTROFILE | get_var DISTRIB_DESCRIPTION)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1831 1832
fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1833
DISTRIB_RELEASE=$(normalize_version2 "$DISTRIB_RELEASE")
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1834
[ -n "$DISTRIB_CODENAME" ] || DISTRIB_CODENAME=$DISTRIB_RELEASE
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1835 1836

case "$VENDOR_ID" in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860
    "alt"|"altlinux")
        # 2.4.5.99 -> 2
        DISTRIB_RELEASE=$(normalize_version1 "$DISTRIB_RELEASE_ORIG")
        case "$DISTRIB_ID" in
            "ALTServer"|"ALTSPWorkstation"|"Sisyphus")
                ;;
            *)
                DISTRIB_ID="ALTLinux"
                ;;
        esac
        ;;
    "astra")
        DISTRIB_RELEASE=$(normalize_version2 "$DISTRIB_RELEASE_ORIG" | sed -e 's|_.*||')
        DISTRIB_FULL_RELEASE=$(normalize_version3 "$DISTRIB_RELEASE_ORIG" | sed -e 's|_.*||')
        if [ "$VARIANT" = "orel" ] || [ "$VARIANT" = "Orel" ] ; then
            DISTRIB_ID="AstraLinuxCE"
        else
            DISTRIB_ID="AstraLinuxSE"
        fi
        if [ "$DISTRIB_ID" = "AstraLinuxSE" ] ; then
            local fr="$(cat /etc/astra_version 2>/dev/null)"
            [ -n "$fr" ] && echo "$fr" | grep -q "$DISTRIB_RELEASE" && DISTRIB_FULL_RELEASE="$fr"
        fi
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1861 1862 1863
esac

case "$DISTRIB_ID" in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904
    "ALTLinux")
        echo "$VERSION" | grep -q "c9.* branch" && DISTRIB_RELEASE="c9"
        echo "$VERSION" | grep -q "c9f1 branch" && DISTRIB_RELEASE="c9f1"
        echo "$VERSION" | grep -q "c9f2 branch" && DISTRIB_RELEASE="c9f2"
        DISTRIB_CODENAME="$DISTRIB_RELEASE"
        # FIXME: fast hack for fallback: 10.1 -> p10 for /etc/os-release
        if echo "$DISTRIB_RELEASE" | grep -q "^[0-9]" && echo "$DISTRIB_RELEASE" | grep -q -v "[0-9][0-9][0-9]"  ; then
            DISTRIB_RELEASE="$(echo p$DISTRIB_RELEASE | sed -e 's|\..*||')"
            DISTRIB_CODENAME="$DISTRIB_RELEASE"
        fi
        ;;
    "ALTServer")
        DISTRIB_CODENAME="$(echo p$DISTRIB_RELEASE | sed -e 's|\..*||')"
        ;;
    "ALTSPWorkstation")
        DISTRIB_ID="ALTLinux"
        case "$DISTRIB_RELEASE_ORIG" in
            8.0|8.1)
                ;;
            8.2|8.3)
                DISTRIB_RELEASE="c9f1"
            ;;
            8.4)
                DISTRIB_RELEASE="c9f2"
            ;;
            8.*)
                DISTRIB_RELEASE="c9f3"
            ;;
        esac
        DISTRIB_CODENAME="$DISTRIB_RELEASE"
#        DISTRIB_RELEASE=$(echo $DISTRIB_RELEASE | sed -e "s/\..*//g")
        ;;
    "Sisyphus")
        DISTRIB_ID="ALTLinux"
        DISTRIB_RELEASE="Sisyphus"
        DISTRIB_CODENAME="$DISTRIB_RELEASE"
        ;;
    "ROSA")
        DISTRIB_FULL_RELEASE="$DISTRIB_CODENAME"
        DISTRIB_CODENAME="$DISTRIB_RELEASE"
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1905 1906 1907 1908 1909 1910 1911 1912
esac


[ -n "$DISTRIB_ID" ] && return


# check via obsoleted ways

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1913 1914
# ALT Linux based
if distro altlinux-release ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952
    DISTRIB_ID="ALTLinux"
    # FIXME: fast hack for fallback: 10 -> p10 for /etc/os-release
    DISTRIB_RELEASE="$(echo p$DISTRIB_RELEASE | sed -e 's|\..*||' -e 's|^pp|p|')"
    if has Sisyphus ; then DISTRIB_RELEASE="Sisyphus"
    elif has "ALT p10.* p10 " ; then DISTRIB_RELEASE="p10"
    elif has "ALTServer 10." ; then DISTRIB_RELEASE="p10"
    elif has "ALTServer 9." ; then DISTRIB_RELEASE="p9"
    elif has "ALT c10.* c10 " ; then DISTRIB_RELEASE="c10"
    elif has "ALT p9.* p9 " ; then DISTRIB_RELEASE="p9"
    elif has "ALT 9 SP " ; then DISTRIB_RELEASE="c9"
    elif has "ALT c9f1" ; then DISTRIB_RELEASE="c9f1"
    elif has "ALT MED72 " ; then DISTRIB_RELEASE="p8"
    elif has "ALT 8 SP " ; then DISTRIB_RELEASE="c8"
    elif has "ALT c8.2 " ; then DISTRIB_RELEASE="c8.2"
    elif has "ALT c8.1 " ; then DISTRIB_RELEASE="c8.1"
    elif has "ALT c8 " ; then DISTRIB_RELEASE="c8"
    elif has "ALT .*8.[0-9]" ; then DISTRIB_RELEASE="p8"
    elif has "Simply Linux 10." ; then DISTRIB_RELEASE="p10"
    elif has "Simply Linux 9." ; then DISTRIB_RELEASE="p9"
    elif has "Simply Linux 8." ; then DISTRIB_RELEASE="p8"
    elif has "Simply Linux 7." ; then DISTRIB_RELEASE="p7"
    elif has "Simply Linux 6." ; then DISTRIB_RELEASE="p6"
    elif has "ALT Linux p8"  ; then DISTRIB_RELEASE="p8"
    elif has "ALT Linux 8." ; then DISTRIB_RELEASE="p8"
    elif has "ALT Linux p7"  ; then DISTRIB_RELEASE="p7"
    elif has "ALT Linux 7." ; then DISTRIB_RELEASE="p7"
    elif has "ALT Linux t7." ; then DISTRIB_RELEASE="t7"
    elif has "ALT Linux 6." ; then DISTRIB_RELEASE="p6"
    elif has "ALT Linux p6"  ; then DISTRIB_RELEASE="p6"
    elif has "ALT Linux p5"  ; then DISTRIB_RELEASE="p5"
    elif has "ALT Linux 5.1" ; then DISTRIB_RELEASE="5.1"
    elif has "ALT Linux 5.0" ; then DISTRIB_RELEASE="5.0"
    elif has "ALT Linux 4.1" ; then DISTRIB_RELEASE="4.1"
    elif has "ALT Linux 4.0" ; then DISTRIB_RELEASE="4.0"
    elif has "starter kit"   ; then DISTRIB_RELEASE="Sisyphus"
    elif has Citron   ; then DISTRIB_RELEASE="2.4"
    fi
    PRETTY_NAME="$(cat /etc/altlinux-release)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1953 1954

elif distro gentoo-release ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1955 1956 1957 1958
    DISTRIB_ID="Gentoo"
    MAKEPROFILE=$(readlink $ROOTDIR/etc/portage/make.profile 2>/dev/null) || MAKEPROFILE=$(readlink $ROOTDIR/etc/make.profile)
    DISTRIB_RELEASE=$(basename $MAKEPROFILE)
    echo $DISTRIB_RELEASE | grep -q "[0-9]" || DISTRIB_RELEASE=$(basename "$(dirname $MAKEPROFILE)") #"
1959

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1960
elif distro slackware-version ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1961 1962
    DISTRIB_ID="Slackware"
    DISTRIB_RELEASE="$(grep -Eo '[0-9]+\.[0-9]+' $DISTROFILE)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1963

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1964
elif distro os-release && is_command tce-ab ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1965 1966 1967 1968
    # shellcheck disable=SC1090
    . $ROOTDIR/etc/os-release
    DISTRIB_ID="TinyCoreLinux"
    DISTRIB_RELEASE="$VERSION_ID"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1969

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1970
elif distro os-release && is_command xbps-query ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1971 1972 1973 1974
    # shellcheck disable=SC1090
    . $ROOTDIR/etc/os-release
    DISTRIB_ID="VoidLinux"
    DISTRIB_RELEASE="Live"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1975

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1976
# TODO: use standart /etc/os-release or lsb
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1977
elif distro arch-release ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1978 1979
    DISTRIB_ID="ArchLinux"
    DISTRIB_RELEASE="rolling"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1980

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1981
# Elbrus
1982
elif distro mcst_version ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1983 1984
    DISTRIB_ID="MCST"
    DISTRIB_RELEASE=$(cat "$DISTROFILE" | grep "release" | sed -e "s|.*release \([0-9]*\).*|\1|g") #"
1985

1986 1987
# OpenWrt
elif distro openwrt_release ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1988 1989
    . $DISTROFILE
    DISTRIB_RELEASE=$(cat $ROOTDIR/etc/openwrt_version)
1990

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1991 1992
# Debian based
elif distro debian_version ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1993 1994
    DISTRIB_ID="Debian"
    DISTRIB_RELEASE=$(cat $DISTROFILE | sed -e "s/\..*//g")
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1995 1996 1997 1998


# SUSE based
elif distro SuSe-release || distro SuSE-release ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1999 2000 2001 2002 2003 2004 2005
    DISTRIB_ID="SUSE"
    DISTRIB_RELEASE=$(cat "$DISTROFILE" | grep "VERSION" | sed -e "s|^VERSION = ||g")
    if   has "SUSE Linux Enterprise Desktop" ; then
        DISTRIB_ID="SLED"
    elif has "SUSE Linux Enterprise Server" ; then
        DISTRIB_ID="SLES"
    fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2006 2007

# fixme: can we detect by some file?
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2008
elif [ "$(uname)" = "FreeBSD" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2009 2010 2011
    DISTRIB_ID="FreeBSD"
    UNAME=$(uname -r)
    DISTRIB_RELEASE=$(echo "$UNAME" | grep RELEASE | sed -e "s|\([0-9]\.[0-9]\)-RELEASE|\1|g") #"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2012 2013

# fixme: can we detect by some file?
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2014
elif [ "$(uname)" = "SunOS" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2015 2016
    DISTRIB_ID="SunOS"
    DISTRIB_RELEASE=$(uname -r)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2017

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2018
# fixme: can we detect by some file?
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2019
elif [ "$(uname -s 2>/dev/null)" = "Darwin" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2020 2021
    DISTRIB_ID="MacOS"
    DISTRIB_RELEASE=$(uname -r)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2022 2023

# fixme: move to up
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2024
elif [ "$(uname)" = "Linux" ] && is_command guix ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2025 2026
    DISTRIB_ID="GNU/Linux/Guix"
    DISTRIB_RELEASE=$(uname -r)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2027

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2028
# fixme: move to up
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2029
elif [ "$(uname)" = "Linux" ] && [ -x $ROOTDIR/system/bin/getprop ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2030 2031
    DISTRIB_ID="Android"
    DISTRIB_RELEASE=$(getprop | awk -F": " '/build.version.release/ { print $2 }' | tr -d '[]')
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2032

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2033
elif [ "$(uname -o 2>/dev/null)" = "Cygwin" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2034 2035
        DISTRIB_ID="Cygwin"
        DISTRIB_RELEASE="all"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2036
fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2037

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2038 2039 2040
}

fill_distr_info
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2041 2042
[ -n "$DISTRIB_ID" ] || DISTRIB_ID="Generic"

2043 2044 2045 2046 2047
get_uname()
{
    tolower $(uname $1) | tr -d " \t\r\n"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2048 2049 2050 2051 2052 2053 2054
get_glibc_version()
{
    for i in /lib/x86_64-linux-gnu /lib64 /lib/i386-linux-gnu /lib ; do
        [ -x "$ROOTDIR$i/libc.so.6" ] && $ROOTDIR$i/libc.so.6 | head -n1 | grep "version" | sed -e 's|.*version ||' -e 's|\.$||' && return
    done
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2055 2056 2057 2058
get_base_os_name()
{
local DIST_OS
# Resolve the os
2059
DIST_OS="$(get_uname -s)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079
case "$DIST_OS" in
    'sunos')
        DIST_OS="solaris"
        ;;
    'hp-ux' | 'hp-ux64')
        DIST_OS="hpux"
        ;;
    'darwin' | 'oarwin')
        DIST_OS="macosx"
        ;;
    'unix_sv')
        DIST_OS="unixware"
        ;;
    'freebsd' | 'openbsd' | 'netbsd')
        DIST_OS="freebsd"
        ;;
esac
echo "$DIST_OS"
}

2080

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2081 2082 2083 2084
get_arch()
{
local DIST_ARCH
# Resolve the architecture
2085
DIST_ARCH="$(get_uname -m)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2086
case "$DIST_ARCH" in
2087
    'ia32' | 'i386' | 'i486' | 'i586' | 'i686')
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2088 2089
        DIST_ARCH="x86"
        ;;
2090 2091 2092
    'amd64' | 'x86_64')
        DIST_ARCH="x86_64"
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110
    'ia64' | 'ia-64')
        DIST_ARCH="ia64"
        ;;
    'ip27' | 'mips')
        DIST_ARCH="mips"
        ;;
    'powermacintosh' | 'power' | 'powerpc' | 'power_pc' | 'ppc64')
        DIST_ARCH="ppc"
        ;;
    'pa_risc' | 'pa-risc')
        DIST_ARCH="parisc"
        ;;
    'sun4u' | 'sparcv9')
        DIST_ARCH="sparc"
        ;;
    '9000/800')
        DIST_ARCH="parisc"
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2111 2112 2113 2114 2115 2116
    'arm64' | 'aarch64')
        DIST_ARCH='aarch64'
        ;;
    armv7*)
        # TODO: use uname only
        # uses binutils package
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2117
        if is_command readelf && [ -z "$(readelf -A /proc/self/exe | grep Tag_ABI_VFP_args)" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2118 2119 2120 2121 2122 2123 2124 2125 2126
            DIST_ARCH="armel"
        else
            DIST_ARCH="armhf"
        fi
        ;;
esac
echo "$DIST_ARCH"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2127 2128 2129 2130
get_debian_arch()
{
    local arch="$(get_arch)"
    case $arch in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2131
    'x86')
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2132 2133 2134
        arch='i386' ;;
    'x86_64')
        arch='amd64' ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2135 2136
    'aarch64')
        arch='arm64' ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2137 2138 2139 2140
    esac
    echo "$arch"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2141 2142 2143 2144 2145
get_distro_arch()
{
    local arch="$(get_arch)"
    case "$(pkgtype)" in
        rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2146 2147 2148 2149
            case $arch in
            'x86')
                arch='i586' ;;
            esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2150 2151
            ;;
        deb)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2152 2153
            get_debian_arch
            return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2154 2155 2156 2157 2158
            ;;
    esac
    echo "$arch"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2159 2160 2161
get_bit_size()
{
local DIST_BIT
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2162 2163 2164 2165 2166 2167 2168 2169 2170

DIST_BIT="$(getconf LONG_BIT 2>/dev/null)"
if [ -n "$DIST_BIT" ] ; then
    echo "$DIST_BIT"
    return
fi

# Try detect arch size by arch name
case "$(get_uname -m)" in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2171 2172 2173
    'amd64' | 'ia64' | 'x86_64' | 'ppc64')
        DIST_BIT="64"
        ;;
2174 2175 2176
    'aarch64')
        DIST_BIT="64"
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2177 2178 2179
    'e2k')
        DIST_BIT="64"
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195
#    'pa_risc' | 'pa-risc') # Are some of these 64bit? Least not all...
#       BIT="64"
#        ;;
    'sun4u' | 'sparcv9') # Are all sparcs 64?
        DIST_BIT="64"
        ;;
#    '9000/800')
#       DIST_BIT="64"
#        ;;
    *) # In any other case default to 32
        DIST_BIT="32"
        ;;
esac
echo "$DIST_BIT"
}

2196
# TODO: check before calc
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2197 2198 2199
get_memory_size()
{
    local detected=""
2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210
    local DIST_OS="$(get_base_os_name)"
    case "$DIST_OS" in
        macosx)
            detected=$((`sysctl hw.memsize | sed s/"hw.memsize: "//`/1024/1024))
            ;;
        freebsd)
            detected=$((`sysctl hw.physmem | sed s/"hw.physmem: "//`/1024/1024))
            ;;
        linux)
            [ -r /proc/meminfo ] && detected=$((`cat /proc/meminfo | grep MemTotal | awk '{print $2}'`/1024))
            ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2211
        solaris)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2212
            detected=$(prtconf | grep Memory | sed -e "s|Memory size: \([0-9][0-9]*\) Megabyte.*|\1|") #"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2213 2214 2215
            ;;
#        *)
#            fatal "Unsupported OS $DIST_OS"
2216 2217
    esac

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2218
    [ -n "$detected" ] || detected=0
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2219 2220 2221
    echo $detected
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2222 2223 2224 2225 2226
print_name_version()
{
    [ -n "$DISTRIB_RELEASE" ] && echo $DISTRIB_ID/$DISTRIB_RELEASE || echo $DISTRIB_ID
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2227 2228 2229 2230 2231 2232
get_core_count()
{
    local detected=""
    local DIST_OS="$(get_base_os_name)"
    case "$DIST_OS" in
        macos|freebsd)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2233
            detected=$(a= sysctl hw.ncpu | awk '{print $2}')
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2234 2235 2236 2237 2238
            ;;
        linux)
            detected=$(grep -c "^processor" /proc/cpuinfo)
            ;;
        solaris)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2239
            detected=$(a= prtconf | grep -c 'cpu[^s]')
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2240 2241
            ;;
        aix)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2242
            detected=$(a= lsdev -Cc processor -S A | wc -l)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2243 2244 2245 2246 2247 2248 2249 2250 2251
            ;;
#        *)
#            fatal "Unsupported OS $DIST_OS"
    esac

    [ -n "$detected" ] || detected=0
    echo $detected
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2252 2253 2254 2255 2256
get_core_mhz()
{
    cat /proc/cpuinfo | grep "cpu MHz" | head -n1 | cut -d':' -f2 | cut -d' ' -f2 | cut -d'.' -f1
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2257

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2258 2259 2260
get_virt()
{
    local VIRT
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2261 2262
    if is_command systemd-detect-virt ; then
        VIRT="$(systemd-detect-virt)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2263 2264 2265 2266
        [ "$VIRT" = "none" ] && echo "(host system)" && return
        [ -z "$VIRT" ] && echo "(unknown)" && return
        echo "$VIRT" && return
    fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2267 2268 2269 2270 2271

    # TODO: use virt-what under root

    # inspired by virt_what
    if [ -d "/proc/vz" -a ! -d "/proc/bc" ]; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2272 2273
        echo "openvz" && return
    fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2274 2275 2276 2277 2278

    if [ -r "/sys/bus/xen" ] ; then
        echo "xen" && return
    fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2279 2280
    # use util-linux
    if LANG=C a= lscpu | grep "Hypervisor vendor:" | grep -q "KVM" ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2281 2282 2283
        echo "kvm" && return
    fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2284 2285 2286 2287 2288 2289 2290 2291
    echo "(unknown)"
    # TODO: check for openvz
}

# https://unix.stackexchange.com/questions/196166/how-to-find-out-if-a-system-uses-sysv-upstart-or-systemd-initsystem
get_service_manager()
{
    [ -d /run/systemd/system ] && echo "systemd" && return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2292 2293
    # TODO
    #[ -d /usr/share/upstart ] && echo "upstart" && return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2294
    is_command systemctl && echo "systemd" && return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2295 2296 2297 2298
    [ -d /etc/init.d ] && echo "sysvinit" && return
    echo "(unknown)"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2299 2300 2301 2302 2303
filter_duplicated_words()
{
    echo "$*" | xargs -n1 echo | uniq | xargs -n100 echo
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2304 2305
print_pretty_name()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2306 2307 2308 2309
    if [ -z "$PRETTY_NAME" ] ; then
        PRETTY_NAME="$DISTRIB_ID $DISTRIB_RELEASE"
    fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2310 2311 2312 2313
    if ! echo "$PRETTY_NAME" | grep -q "$DISTRIB_FULL_RELEASE" ; then
        PRETTY_NAME="$PRETTY_NAME ($DISTRIB_FULL_RELEASE)"
    fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2314 2315 2316 2317
    if ! echo "$PRETTY_NAME" | grep -q "$DISTRIB_RELEASE" ; then
        PRETTY_NAME="$PRETTY_NAME ($DISTRIB_RELEASE)"
    fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2318
    echo "$(filter_duplicated_words "$PRETTY_NAME")"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2319
}
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2320 2321 2322

print_total_info()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2323 2324
local orig=''
[ -n "$BUILD_ID" ] && orig=" (orig. $BUILD_ID)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2325
cat <<EOF
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2326 2327
distro_info v$PROGVERSION : Copyright © 2007-2023 Etersoft

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2328 2329 2330 2331 2332 2333 2334 2335
                Pretty distro name (--pretty): $(print_pretty_name)
Distro name / version (--distro-name/version): $DISTRO_NAME / $DISTRIB_FULL_RELEASE$orig
         Base distro name (-d) / version (-v): $(print_name_version)
     Vendor distro name (-s) / Repo name (-r): $(pkgvendor) / $(print_repo_name)
                 Package manager/type (-g/-p): $(pkgmanager) / $(pkgtype)
            Base OS name (-o) / CPU arch (-a): $(get_base_os_name) $(get_arch)
            Bug report URL (--bug-report-url): $(print_bug_report_url)
                 CPU norm register size  (-b): $(get_bit_size)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2336 2337 2338 2339
                          Virtualization (-i): $(get_virt)
                        CPU Cores/MHz (-c/-z): $(get_core_count) / $(get_core_mhz) MHz
                 System memory size (MB) (-m): $(get_memory_size)
                 Running service manager (-y): $(get_service_manager)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2340 2341

(run with -h to get help)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2342 2343 2344
EOF
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2345
case "$2" in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2346 2347 2348 2349
    -*)
        echo "Unsupported option $2" >&2
        exit 1
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2350
esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2351

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2352
case "$1" in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493
    -h|--help)
        echo "distro_info v$PROGVERSION - distro information retriever"
        echo "Usage: distro_info [options] [SystemName/Version]"
        echo "Options:"
        echo " -h | --help            - this help"
        echo " -a                     - print hardware architecture (--distro-arch for distro depended name)"
        echo " -b                     - print size of arch bit (32/64)"
        echo " -c                     - print number of CPU cores"
        echo " -i                     - print virtualization type"
        echo " -m                     - print system memory size (in MB)"
        echo " -y|--service-manager   - print running service manager"
        echo " -z                     - print current CPU MHz"
        echo " --glibc-version        - print system glibc version"
        echo
        echo " -d|--base-distro-name  - print distro id (short distro name)"
        echo " -e                     - print full name of distro with version"
        echo " -o | --os-name         - print base OS name"
        echo " -p | package-type      - print type of the packaging system"
        echo " -g                     - print name of the packaging system"
        echo " -s|-n|--vendor-name    - print name of the distro family (vendor name) (ubuntu for all Ubuntu family, alt for all ALT family) (see _vendor macros in rpm)"
        echo " --pretty|--pretty-name - print pretty distro name"
        echo " -v | --base-version    - print version of the distro"
        echo " --distro-name          - print distro name"
        echo " --distro-version       - print full version of the distro"
        echo " --full-version         - print full version of the distro"
        echo " --codename (obsoleted) - print distro codename (focal for Ubuntu 20.04)"
        echo " -r|--repo-name         - print repository name (focal for Ubuntu 20.04)"
        echo " --build-id             - print a string uniquely identifying the system image originally used as the installation base"
        echo " -V                     - print the utility version"
        echo "Run without args to print all information."
        exit 0
        ;;
    -p|--package-type)
        override_distrib "$2"
        pkgtype
        exit 0
        ;;
    -g)
        override_distrib "$2"
        pkgmanager
        exit 0
        ;;
    --pretty|--pretty-name)
        override_distrib "$2"
        print_pretty_name
        ;;
    --distro-arch)
        override_distrib "$2"
        get_distro_arch
        exit 0
        ;;
    --debian-arch)
        override_distrib "$2"
        get_debian_arch
        exit 0
        ;;
    --glibc-version)
        override_distrib "$2"
        get_glibc_version
        exit 0
        ;;
    -d|--base-distro-name)
        override_distrib "$2"
        echo $DISTRIB_ID
        ;;
    --distro-name)
        override_distrib "$2"
        echo $DISTRO_NAME
        ;;
    --codename)
        override_distrib "$2"
        print_codename
        ;;
    -a)
        override_distrib "$2"
        [ -n "$DIST_ARCH" ] && echo "$DIST_ARCH" && exit 0
        get_arch
        ;;
    -b)
        get_bit_size
        ;;
    -c)
        get_core_count
        ;;
    -z)
        get_core_mhz
        ;;
    -i)
        get_virt
        ;;
    -m)
        get_memory_size
        ;;
    -o|--os-name)
        override_distrib "$2"
        get_base_os_name
        ;;
    -r|--repo-name)
        override_distrib "$2"
        print_repo_name
        ;;
    --build-id)
        echo "$BUILD_ID"
        ;;
    -v|--base-version)
        override_distrib "$2"
        echo "$DISTRIB_RELEASE"
        ;;
    --full-version|--distro-version)
        override_distrib "$2"
        echo "$DISTRIB_FULL_RELEASE"
        ;;
    --bug-report-url)
        print_bug_report_url
        return
        ;;
    -s|-n|--vendor-name)
        override_distrib "$2"
        pkgvendor
        exit 0
        ;;
    -y|--service-manager)
        override_distrib "$2"
        get_service_manager
        ;;
    -V)
        echo "$PROGVERSION"
        exit 0
        ;;
    -e)
        override_distrib "$2"
        print_name_version
        ;;
    -*)
        echo "Unsupported option $1" >&2
        exit 1
        ;;
    *)
        override_distrib "$1"
        print_total_info
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2494 2495 2496
esac

}
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2497
################# end of incorporated bin/distr_info #################
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2498

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2499

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2500
serv_main()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2501
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2502

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2503 2504
INITDIR=/etc/init.d

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516
PATH=$PATH:/sbin:/usr/sbin

set_sudo

check_tty

#############################

# FIXME: detect by real init system
# FIXME: add upstart support (Ubuntu?)
set_service_type()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2517
    local CMD
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2518

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2519 2520
    set_distro_info
    set_target_pkg_env
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2521

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2522
case "$DISTRCONTROL" in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2523 2524 2525 2526 2527 2528
    sysvinit)
        CMD="service-chkconfig"
        ;;
    systemd)
        CMD="systemd"
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2529 2530 2531
esac

# override system control detection result
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2532
[ -n "$FORCESERVICE" ] && CMD="$FORCESERVICE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2533

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2534
SERVICETYPE="$CMD"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2535

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2536
ANYSERVICE=$(print_command_path anyservice)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2537

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2538 2539
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2540 2541 2542
# TODO: done it on anyservice part
is_anyservice()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2543 2544 2545 2546
    [ -n "$ANYSERVICE" ] || return
    [ -n "$1" ] || return
    # check if anyservice is exists and checkd returns true
    $ANYSERVICE "$1" checkd 2>/dev/null
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2547
}
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2548 2549 2550 2551


phelp()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2552
    echo "$Descr
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563
$Usage
 Commands:
$(get_help HELPCMD)

 Options:
$(get_help HELPOPT)
"
}

print_version()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2564
        local on_text="(host system)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2565 2566
        local virt="$($DISTRVENDOR -i)"
        [ "$virt" = "(unknown)" ] || [ "$virt" = "(host system)" ] || on_text="(under $virt)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2567
        echo "Service manager version 3.50.1  https://wiki.etersoft.ru/Epm"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2568
        echo "Running on $($DISTRVENDOR -e) $on_text with $SERVICETYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2569
        echo "Copyright (c) Etersoft 2012-2021"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2570 2571 2572
        echo "This program may be freely redistributed under the terms of the GNU AGPLv3."
}

2573 2574 2575
progname="${0##*/}"

Usage="Usage: $progname [options] [<service>] [<command>] [params]..."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2576 2577 2578 2579 2580 2581
Descr="serv - Service manager"

set_service_type

verbose=
quiet=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2582
short=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2583 2584 2585 2586 2587
non_interactive=
show_command_only=
serv_cmd=
service_name=
params=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2588
withoutservicename=
2589

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2590 2591 2592
# load system wide config
[ -f /etc/eepm/serv.conf ] && . /etc/eepm/serv.conf

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2593 2594 2595 2596 2597 2598 2599 2600 2601
check_command()
{
    # do not override command
    [ -z "$serv_cmd" ] || return

    case $1 in
    status)                   # HELPCMD: show service status
        serv_cmd=status
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2602 2603 2604
    restart)                 # HELPCMD: restart service
        serv_cmd=restart
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2605 2606 2607
    reload)                  # HELPCMD: reload service
        serv_cmd=reload
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2608 2609 2610 2611 2612 2613
    start)                    # HELPCMD: start service
        serv_cmd=start
        ;;
    stop)                     # HELPCMD: stop service
        serv_cmd=stop
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2614 2615 2616 2617 2618 2619 2620
    on)                       # HELPCMD: add service to run on startup and start it now
        serv_cmd=on
        ;;
    off)                      # HELPCMD: remove service to run on startup and stop it now
        serv_cmd=off
        ;;
    enable)                   # HELPCMD: add service to run on startup (see 'on' also)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2621 2622
        serv_cmd=enable
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2623
    disable)                 # HELPCMD: remove service to run on startup (see 'off' also)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2624 2625 2626 2627 2628 2629 2630 2631
        serv_cmd=disable
        ;;
    log|journal)              # HELPCMD: print log for the service (-f - follow,  -r - reverse order)
        serv_cmd=log
        ;;
    cat)                      # HELPCMD: print out service file for the service
        serv_cmd=cat
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2632 2633 2634
    exists)                   # HELPCMD: check if the service is installed on the system
        serv_cmd=exists
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2635 2636 2637 2638 2639 2640
    edit)                     # HELPCMD: edit service file overload (use --full to edit full file)
        serv_cmd=edit
        ;;
    test|-t)                  # HELPCMD: test a config file of the service
        serv_cmd=test
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2641 2642
    list)                     # HELPCMD: list running services
        serv_cmd=list
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2643
        withoutservicename=1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2644 2645 2646
        ;;
    list-all)                 # HELPCMD: list all available services
        serv_cmd=list_all
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2647
        withoutservicename=1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2648 2649 2650
        ;;
    list-startup)             # HELPCMD: list all services to run on startup
        serv_cmd=list_startup
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2651
        withoutservicename=1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2652
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2653 2654 2655 2656
    list-failed|--failed)       # HELPCMD: list services failed on startup
        serv_cmd=list_failed
        withoutservicename=1
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2657 2658 2659 2660
    print)                    # HELPCMD: print some info
        serv_cmd=print
        withoutservicename=1
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2661 2662
    try-restart|condrestart)  # HELPCMD: Restart service if running
        serv_cmd=try_restart
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2663
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2664 2665 2666
    usage)                    # HELPCMD: print out usage of the service
        serv_cmd=usage
        withoutservicename=1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2667
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687
    *)
        return 1
        ;;
    esac
    return 0
}

check_option()
{
    case $1 in
    -h|--help|help)       # HELPOPT: this help
        phelp
        exit 0
        ;;
    -v|--version)         # HELPOPT: print version
        print_version
        exit 0
        ;;
    --verbose)            # HELPOPT: verbose mode
        verbose=1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2688 2689 2690
        ;;
    --short)              # HELPOPT: short mode
        short=1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718
        ;;
    --show-command-only)  # HELPOPT: show command only, do not any action
        show_command_only=1
        ;;
    --quiet)              # HELPOPT: quiet mode (do not print commands before exec)
        quiet=1
        ;;
    --auto)               # HELPOPT: non interactive mode
        non_interactive=1
        ;;
    *)
        return 1
        ;;
    esac
    return 0
}

for opt in "$@" ; do
    check_command $opt && continue
    check_option $opt && continue
    [ -z "$service_name" ] && service_name=$opt && continue
    params="$params $opt"
done

echover "service: $service_name"
echover "command: $serv_cmd"

# Just printout help if run without args
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2719
if [ -z "$withoutservicename" ] && [ -z "$service_name" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2720 2721
    print_version
    echo
2722
    fatal "Run $ $progname --help for get help"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2723 2724
fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2725
# use common way if the command is unknown
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2726 2727 2728 2729 2730 2731 2732
if [ -z "$serv_cmd" ] ; then
    serv_cmd=common
fi

# Run helper for command
serv_$serv_cmd $service_name $params
# return last error code (from subroutine)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2733 2734
}
serv_main "$@"