serv.sh 74.1 KB
Newer Older
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1
#!/usr/bin/env bash
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2
#
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3 4
# Copyright (C) 2012-2013, 2016, 2020, 2021, 2023  Etersoft
# Copyright (C) 2012-2013, 2016, 2020, 2021, 2023  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
#
# 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/>.
#

Vitaly Lipatov's avatar
Vitaly Lipatov committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
PROGDIR=$(dirname "$0")
PROGNAME=$(basename "$0")
[ -n "$EPMCURDIR" ] || export EPMCURDIR="$(pwd)"
CMDSHELL="/bin/bash"
# TODO: pwd for ./epm and which for epm
[ "$PROGDIR" = "." ] && PROGDIR="$EPMCURDIR"
if [ "$0" = "/dev/stdin" ] || [ "$0" = "sh" ] ; then
    PROGDIR=""
    PROGNAME=""
fi

# will replaced with /usr/share/eepm during install
SHAREDIR=$PROGDIR
# will replaced with /etc/eepm during install
CONFIGDIR=$PROGDIR/../etc

Vitaly Lipatov's avatar
Vitaly Lipatov committed
36
EPMVERSION="3.60.9"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
37 38 39 40 41 42 43 44 45 46

# package, single (file), pipe, git
EPMMODE="package"
[ "$SHAREDIR" = "$PROGDIR" ] && EPMMODE="single"
[ "$EPMVERSION" = "@""VERSION""@" ] && EPMMODE="git"
[ "$PROGNAME" = "" ] && EPMMODE="pipe"

if [ "$EPMMODE" = "git" ] ; then
    EPMVERSION=$(head $PROGDIR/../eepm.spec | grep "^Version: " | sed -e 's|Version: ||' )
fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
47

Vitaly Lipatov's avatar
Vitaly Lipatov committed
48 49 50

load_helper()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
51 52 53 54
    local shieldname="loaded$(echo "$1" | sed -e 's|-||g')"
    # already loaded
    eval "test -n \"\$$shieldname\"" && debug "Already loaded $1" && return

Vitaly Lipatov's avatar
Vitaly Lipatov committed
55
    local CMD="$SHAREDIR/$1"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
56 57 58 59
    # do not use fatal() here, it can be initial state
    [ -r "$CMD" ] || { echo "FATAL: Have no $CMD helper file" ; exit 1; }
    eval "$shieldname=1"
    # shellcheck disable=SC1090
Vitaly Lipatov's avatar
Vitaly Lipatov committed
60 61 62 63
    . $CMD
}


Vitaly Lipatov's avatar
Vitaly Lipatov committed
64

Vitaly Lipatov's avatar
Vitaly Lipatov committed
65 66
# File bin/epm-sh-functions:

Vitaly Lipatov's avatar
Vitaly Lipatov committed
67

Vitaly Lipatov's avatar
Vitaly Lipatov committed
68

Vitaly Lipatov's avatar
Vitaly Lipatov committed
69 70
inputisatty()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
71 72 73
    # check stdin
    #tty -s 2>/dev/null
    test -t 0
Vitaly Lipatov's avatar
Vitaly Lipatov committed
74 75
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
76 77
isatty()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
78 79
    # check stdout
    test -t 1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
80 81
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
82 83
isatty2()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
84 85
    # check stderr
    test -t 2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
86 87
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
88 89
check_tty()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
90
    isatty2 || return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
91

Vitaly Lipatov's avatar
Vitaly Lipatov committed
92 93 94
    # Set a sane TERM required for tput
    [ -n "$TERM" ] || TERM=dumb
    export TERM
Vitaly Lipatov's avatar
Vitaly Lipatov committed
95

Vitaly Lipatov's avatar
Vitaly Lipatov committed
96
    check_core_commands
Vitaly Lipatov's avatar
Vitaly Lipatov committed
97

Vitaly Lipatov's avatar
Vitaly Lipatov committed
98 99 100 101 102
    # 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
103

Vitaly Lipatov's avatar
Vitaly Lipatov committed
104 105 106 107
    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
108 109 110 111 112 113
}

: ${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
114 115 116 117 118
    [ -n "$USETTY" ] || return
    {
        echo bold
        echo setaf $1
    } | $USETTY
Vitaly Lipatov's avatar
Vitaly Lipatov committed
119 120
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
121 122
set_color()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
123 124 125 126
    [ -n "$USETTY" ] || return
    {
        echo setaf $1
    } | $USETTY
Vitaly Lipatov's avatar
Vitaly Lipatov committed
127 128
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
129 130
restore_color()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
131 132 133 134 135
    [ -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
136 137 138 139
}

echover()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
140
    [ -z "$verbose" ] && return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
141 142 143
    echo "$*" >&2
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
144 145
echon()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
146 147
    # 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
148 149 150
}


Vitaly Lipatov's avatar
Vitaly Lipatov committed
151 152
showcmd()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
153 154 155 156 157 158 159
    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
160 161
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
162 163
echocmd()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
164 165 166 167 168
    set_boldcolor $GREEN
    local PROMTSIG="\$"
    is_root && PROMTSIG="#"
    echo -n "$PROMTSIG $*"
    restore_color
Vitaly Lipatov's avatar
Vitaly Lipatov committed
169 170
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
171 172
docmd()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
173 174
    showcmd "$*$EXTRA_SHOWDOCMD"
    "$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
175 176 177 178
}

docmd_foreach()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
179 180 181 182 183 184 185
    local cmd pkg
    cmd="$1"
    #showcmd "$@"
    shift
    for pkg in "$@" ; do
        docmd $cmd $pkg
    done
Vitaly Lipatov's avatar
Vitaly Lipatov committed
186 187
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
188 189
sudorun()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
190 191 192 193 194 195
    set_sudo
    if [ -z "$SUDO" ] ; then
        "$@"
        return
    fi
    $SUDO "$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
196 197
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
198 199
sudocmd()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
200 201 202
    set_sudo
    [ -n "$SUDO" ] && showcmd "$SUDO $*" || showcmd "$*"
    sudorun "$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
203 204
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
205 206
sudocmd_foreach()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
207 208 209 210 211 212 213 214
    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
215 216
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
217 218
make_filepath()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
219 220 221 222 223 224
    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
225 226
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
227 228
get_firstarg()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
229
    echon "$1"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
230 231 232 233
}

get_lastarg()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
234 235 236
    local lastarg
    eval "lastarg=\${$#}"
    echon "$lastarg"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
237 238
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
239 240
isnumber()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
241
    echo "$*" | filter_strip_spaces | grep -q "^[0-9]\+$"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
242 243
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
244 245
rhas()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
246
    echo "$1" | grep -E -q -- "$2"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
247 248
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
249 250 251 252 253 254 255 256 257 258 259 260
startwith()
{
    # rhas "$1" "^$2"
    [[ "$1" = ${2}* ]]
}

is_abs_path()
{
    #echo "$1" | grep -q "^/"
    startwith "$1" "/"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
261 262 263
is_dirpath()
{
    [ "$1" = "." ] && return $?
Vitaly Lipatov's avatar
Vitaly Lipatov committed
264 265
    # rhas "$1" "/"
    startwith "$1" "/"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
266 267
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
268

Vitaly Lipatov's avatar
Vitaly Lipatov committed
269 270 271 272 273 274 275 276 277 278 279 280 281
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
282 283 284

sed_escape()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
285
    echo "$*" | sed -e 's/[]()$*.^|[]/\\&/g'
Vitaly Lipatov's avatar
Vitaly Lipatov committed
286 287 288
}


Vitaly Lipatov's avatar
Vitaly Lipatov committed
289 290
subst_option()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
291
    eval "[ -n \"\$$1\" ]" && echo "$2" || echo "$3"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
292 293 294 295 296
}

store_output()
{
    # use make_temp_file from etersoft-build-utils
Vitaly Lipatov's avatar
Vitaly Lipatov committed
297
    RC_STDOUT="$(mktemp)" || fatal
Vitaly Lipatov's avatar
Vitaly Lipatov committed
298
    remove_on_exit $RC_STDOUT
Vitaly Lipatov's avatar
Vitaly Lipatov committed
299 300
    local CMDSTATUS=$RC_STDOUT.pipestatus
    echo 1 >$CMDSTATUS
Vitaly Lipatov's avatar
Vitaly Lipatov committed
301
    #RC_STDERR=$(mktemp)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
302
    ( LC_ALL=C $@ 2>&1 ; echo $? >$CMDSTATUS ) | tee $RC_STDOUT
Vitaly Lipatov's avatar
Vitaly Lipatov committed
303
    return "$(cat $CMDSTATUS)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
304
    # bashism
Vitaly Lipatov's avatar
Vitaly Lipatov committed
305
    # http://tldp.org/LDP/abs/html/bashver3.html#PIPEFAILREF
Vitaly Lipatov's avatar
Vitaly Lipatov committed
306
    #return $PIPESTATUS
Vitaly Lipatov's avatar
Vitaly Lipatov committed
307 308
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
309 310 311 312 313 314
showcmd_store_output()
{
    showcmd "$@"
    store_output "$@"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
315 316
clean_store_output()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
317
    rm -f $RC_STDOUT $RC_STDOUT.pipestatus
Vitaly Lipatov's avatar
Vitaly Lipatov committed
318 319
}

320 321
epm()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
322
    if [ "$EPMMODE" = "pipe" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
323
        epm_main --inscript "$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
324
        return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
325
    fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
326 327 328

    # run epm again to full initialization
    local bashopt=''
Vitaly Lipatov's avatar
Vitaly Lipatov committed
329
    [ -n "$debug" ] && bashopt='-x'
Vitaly Lipatov's avatar
Vitaly Lipatov committed
330 331

    $CMDSHELL $bashopt $PROGDIR/$PROGNAME --inscript "$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
332 333 334 335
}

sudoepm()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
336
    [ "$EPMMODE" = "pipe" ] && fatal "Can't use sudo epm call from the piped script"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
337

Vitaly Lipatov's avatar
Vitaly Lipatov committed
338
    local bashopt=''
Vitaly Lipatov's avatar
Vitaly Lipatov committed
339
    [ -n "$debug" ] && bashopt='-x'
Vitaly Lipatov's avatar
Vitaly Lipatov committed
340

Vitaly Lipatov's avatar
Vitaly Lipatov committed
341
    sudorun $CMDSHELL $bashopt $PROGDIR/$PROGNAME --inscript "$@"
342
}
Vitaly Lipatov's avatar
Vitaly Lipatov committed
343 344 345

fatal()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
346 347
    local PROMOMESSAGE="$EPMPROMOMESSAGE"
    [ -n "$PROMOMESSAGE" ] || PROMOMESSAGE=" (you can discuss the epm $EPMVERSION problem in Telegram: https://t.me/useepm)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
348
    if [ -z "$TEXTDOMAIN" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
349 350 351
        set_color $RED >&2
        echo -n "ERROR: " >&2
        restore_color >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
352
        echo "$* $PROMOMESSAGE" >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
353 354
    fi
    exit 1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
355 356
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
357 358 359 360 361 362 363 364 365 366 367 368
debug()
{
    [ -n "$debug" ] || return
    if [ -z "$TEXTDOMAIN" ] ; then
        set_color $YELLOW >&2
        echo -n "WARNING: " >&2
        restore_color >&2
        echo "$*" >&2
    fi
}


Vitaly Lipatov's avatar
Vitaly Lipatov committed
369 370
warning()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
371
    if [ -z "$TEXTDOMAIN" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
372 373 374 375
        set_color $YELLOW >&2
        echo -n "WARNING: " >&2
        restore_color >&2
        echo "$*" >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
376
    fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
377 378
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
379 380
info()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
381
    [ -n "$quiet" ] && return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
382

Vitaly Lipatov's avatar
Vitaly Lipatov committed
383 384 385 386 387 388 389
    # 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
390 391
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
392 393 394 395 396 397 398 399 400 401 402 403 404

check_su_root()
{
    [ "$BASEDISTRNAME" = "alt" ] || return 0

    is_root || return 0

    echo "$PATH" | grep -q "/usr/sbin" && return 0

    fatal "There is missed /usr/sbin path in PATH. Probably you have used 'su' without '-' to get root access. Use 'esu' or 'su -' command to get root permissions."
}


Vitaly Lipatov's avatar
Vitaly Lipatov committed
405 406
SUDO_TESTED=''
SUDO_CMD='sudo'
Vitaly Lipatov's avatar
Vitaly Lipatov committed
407 408
set_sudo()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
409 410 411 412 413 414 415 416 417 418 419 420 421 422
    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

Vitaly Lipatov's avatar
Vitaly Lipatov committed
423 424
    check_su_root

Vitaly Lipatov's avatar
Vitaly Lipatov committed
425 426 427 428 429 430 431
    # if we are root, do not need sudo
    is_root && return

    # start error section
    SUDO_TESTED="1"

    if ! is_command $SUDO_CMD ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
432
        [ "$nofail" = "nofail" ] || SUDO="fatal 'For this operation run epm under root, or install and tune sudo (http://altlinux.org/sudo)'"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
433
        SUDO_TESTED="2"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
434 435 436 437 438
        return "$SUDO_TESTED"
    fi

    # if input is a console
    if inputisatty && isatty && isatty2 ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
439 440 441
        if ! $SUDO_CMD -n true ; then
            info "Please enter sudo user password to use sudo in the current session."
            if ! $SUDO_CMD -l >/dev/null ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
442
                [ "$nofail" = "nofail" ] || SUDO="fatal 'For this operation run epm under root, or install and tune sudo (http://altlinux.org/sudo)'"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
443
                SUDO_TESTED="3"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
444 445
                return "$SUDO_TESTED"
            fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
446 447 448
        fi
    else
        # use sudo if one is tuned and tuned without password
Vitaly Lipatov's avatar
Vitaly Lipatov committed
449 450
        # hack: check twice
        $SUDO_CMD -l -n >/dev/null 2>/dev/null
Vitaly Lipatov's avatar
Vitaly Lipatov committed
451
        if ! $SUDO_CMD -l -n >/dev/null 2>/dev/null ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
452
            [ "$nofail" = "nofail" ] || SUDO="fatal 'Can't use sudo (only passwordless sudo is supported here). Please run epm under root or check http://altlinux.org/sudo '"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
453
            SUDO_TESTED="4"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
454 455 456 457 458 459 460 461 462 463
            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
464

Vitaly Lipatov's avatar
Vitaly Lipatov committed
465 466
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
467 468
sudo_allowed()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
469
    set_sudo nofail
Vitaly Lipatov's avatar
Vitaly Lipatov committed
470 471
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
472 473
withtimeout()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
474 475 476 477 478 479 480 481 482
    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
483 484
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
485 486
set_eatmydata()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
487 488 489 490 491 492 493 494 495 496 497
    # 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
498 499
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
500
__get_package_for_command()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
501
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
502 503 504 505 506 507 508 509
    case "$1" in
        equery|revdep-rebuild)
            echo 'gentoolkit'
            ;;
        update-kernel|remove-old-kernels)
            echo 'update-kernel'
            ;;
    esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
510 511
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
512 513 514 515 516 517 518 519 520 521 522 523 524 525
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
526 527 528

confirm_info()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
529 530 531 532
    info "$*"
    if [ -z "$non_interactive" ] ; then
        confirm "Are you sure? [y/N]" || fatal "Exiting"
    fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
533 534 535 536

}


Vitaly Lipatov's avatar
Vitaly Lipatov committed
537 538
is_root()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
539 540
    local EFFUID="$(id -u)"
    [ "$EFFUID" = "0" ]
Vitaly Lipatov's avatar
Vitaly Lipatov committed
541 542
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
543 544
assure_root()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
545
    is_root || fatal "run me only under root"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
546 547
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
check_su_access()
{
    is_command su && return
    [ ! -f /bin/su ] && warning "/bin/su is missed. Try install su package (http://altlinux.org/su)." && return 1
    local group="$(stat -c '%G' /bin/su)" || fatal
    warning "Check if you are in $group group to have access to su command."
    return 1
}

check_sudo_access()
{
    is_command sudo && return
    local cmd=''
    local i
    for i in /bin/sudo /usr/bin/sudo ; do
        [ -f $i ] && cmd="$i"
    done
Vitaly Lipatov's avatar
Vitaly Lipatov committed
565 566
    [ ! -f "$cmd" ] && warning "sudo command is missed. Try install sudo package (http://altlinux.org/sudo)." && return 1
    local group="$(stat -c '%G' "$cmd")" || fatal
Vitaly Lipatov's avatar
Vitaly Lipatov committed
567 568 569 570 571 572 573 574 575 576 577 578
    warning "Check if you are in $group group to have access to sudo command."
    return 1
}

check_sudo_access_only()
{
    is_command sudo && return
    local cmd=''
    local i
    for i in /bin/sudo /usr/bin/sudo ; do
        [ -f $i ] && cmd="$i"
    done
Vitaly Lipatov's avatar
Vitaly Lipatov committed
579 580
    [ ! -f "$cmd" ] && return 1
    local group="$(stat -c '%G' "$cmd")" || fatal
Vitaly Lipatov's avatar
Vitaly Lipatov committed
581 582 583 584
    warning "sudo command is presence, but is not accessible for you. Check if you are in $group group to have access to sudo command."
    return 1
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
585 586 587 588 589 590 591 592 593
esu()
{
    if is_root ; then
        if [ -n "$*" ] ; then
            [ -n "$quiet" ] || showcmd "$*"
            exec "$@"
        else
            # just shell
            showcmd "su -"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
594
            a= exec su -
Vitaly Lipatov's avatar
Vitaly Lipatov committed
595 596 597 598 599 600
        fi
    fi

    set_pm_type


Vitaly Lipatov's avatar
Vitaly Lipatov committed
601

Vitaly Lipatov's avatar
Vitaly Lipatov committed
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
    escape_args()
    {
        local output=''
        while [ -n "$1" ] ; do
            if has_space "$1" ; then
                [ -n "$output" ] && output="$output '$1'" || output="'$1'"
            else
                [ -n "$output" ] && output="$output $1" || output="$1"
            fi
            shift
        done
        echo "$output"
    }

    escaped="$(escape_args "$@")"

Vitaly Lipatov's avatar
Vitaly Lipatov committed
618
    check_sudo_access_only
Vitaly Lipatov's avatar
Vitaly Lipatov committed
619 620
    # sudo is not accessible, will ask root password
    if ! set_sudo ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
621
        check_su_access
Vitaly Lipatov's avatar
Vitaly Lipatov committed
622 623 624
        #info "Enter root password:"
        if [ -n "$*" ] ; then
            [ -n "$quiet" ] || showcmd "su - -c $escaped"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
625
            a= exec su - -c "$escaped"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
626 627 628
        else
            # just shell
            showcmd "su -"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
629
            a= exec su -
Vitaly Lipatov's avatar
Vitaly Lipatov committed
630 631 632
        fi
    fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
633 634
    check_sudo_access

Vitaly Lipatov's avatar
Vitaly Lipatov committed
635 636 637 638 639 640 641 642 643 644
    #info "You can be asked about your password:"
    if [ -n "$*" ] ; then
        [ -n "$quiet" ] || showcmd "$SUDO su - -c $escaped"
        $SUDO su - -c "$escaped"
    else
        showcmd "$SUDO su -"
        $SUDO su -
    fi
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
645 646
regexp_subst()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
647 648 649
    local expression="$1"
    shift
    sed -i -r -e "$expression" "$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
650 651
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
652 653
assure_exists()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
654 655
    local package="$2"
    [ -n "$package" ] || package="$(__get_package_for_command "$1")"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
656 657 658 659 660

    # ask for install: https://bugzilla.altlinux.org/42240
    local ask=''
    [ -n "$non_interactive" ] || ask=1

Vitaly Lipatov's avatar
Vitaly Lipatov committed
661
    ( verbose='' direct='' interactive=$ask epm_assure "$1" $package $3 ) || fatal
Vitaly Lipatov's avatar
Vitaly Lipatov committed
662 663
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
664 665
assure_exists_erc()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
666 667
    local package="erc"
    ( direct='' epm_assure "$package" ) || epm ei erc || fatal "erc is not available to install."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
668 669
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
670 671
disabled_eget()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
672 673
    # use internal eget only if exists
    if [ -s $SHAREDIR/tools_eget ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
674
        ( EGET_BACKEND=$eget_backend $CMDSHELL $SHAREDIR/tools_eget "$@" )
Vitaly Lipatov's avatar
Vitaly Lipatov committed
675 676 677
        return
    fi
    fatal "Internal error: missed tools_eget"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
678

Vitaly Lipatov's avatar
Vitaly Lipatov committed
679
    local EGET
Vitaly Lipatov's avatar
Vitaly Lipatov committed
680 681 682 683 684
    # 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
685 686
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
687

Vitaly Lipatov's avatar
Vitaly Lipatov committed
688 689
__epm_assure_7zip()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
690 691 692 693
    # install 7zip in any case (can be used)
    if is_command 7z || is_command 7za || is_command 7zr || is_command 7zz ; then
        :
    else
Vitaly Lipatov's avatar
Vitaly Lipatov committed
694
        epm install 7-zip || epm install p7zip
Vitaly Lipatov's avatar
Vitaly Lipatov committed
695
    fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
696 697 698 699 700 701
}

disabled_erc()
{

    __epm_assure_7zip
Vitaly Lipatov's avatar
Vitaly Lipatov committed
702

Vitaly Lipatov's avatar
Vitaly Lipatov committed
703 704
    # use internal eget only if exists
    if [ -s $SHAREDIR/tools_erc ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
705
        $CMDSHELL $SHAREDIR/tools_erc "$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
706 707 708
        return
    fi
    fatal "Internal error: missed tools_erc"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
709

Vitaly Lipatov's avatar
Vitaly Lipatov committed
710 711 712
    # FIXME: we need disable output here, ercat can be used for get output
    assure_exists_erc >/dev/null
    # run external command, not the function
Vitaly Lipatov's avatar
Vitaly Lipatov committed
713
    local ERC
Vitaly Lipatov's avatar
Vitaly Lipatov committed
714 715
    ERC=$(print_command_path erc) || fatal "Missed command erc from installed package erc"
    $ERC "$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
716 717 718 719
}

disabled_ercat()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
720 721 722
    local ERCAT
    # use internal eget only if exists
    if [ -s $SHAREDIR/tools_ercat ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
723
        $CMDSHELL $SHAREDIR/tools_ercat "$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
724 725 726
        return
    fi
    fatal "Internal error: missed tools_ercat"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
727

Vitaly Lipatov's avatar
Vitaly Lipatov committed
728 729 730 731 732
    # 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
733 734
}

735 736
disabled_estrlist()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
737
    if [ -s $SHAREDIR/tools_estrlist ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
738
        $CMDSHELL $SHAREDIR/tools_estrlist "$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
739 740 741
        return
    fi
    fatal "missed tools_estrlist"
742 743 744 745
}

estrlist()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
746
    internal_tools_estrlist "$@"
747 748
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
749 750
eget()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
751 752 753 754 755
    # 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
756 757
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
758 759
get_package_type()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
    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
            ;;
        *)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
791
            if [ -r "$1" ] && file "$1" | grep -q " ELF " ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
792 793 794
                echo "ELF"
                return
            fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
795
            # print extension by default
Vitaly Lipatov's avatar
Vitaly Lipatov committed
796
            basename "$1" | sed -e 's|.*\.||'
Vitaly Lipatov's avatar
Vitaly Lipatov committed
797 798 799
            return 1
            ;;
    esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
800 801 802
}


Vitaly Lipatov's avatar
Vitaly Lipatov committed
803 804
get_help()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
805 806 807
    if [ "$0" = "/dev/stdin" ] || [ "$0" = "sh" ] ; then
        return
    fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
808
    local F="$0"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
809 810 811
    if [ -n "$2" ] ; then
        is_dirpath "$2" && F="$2" || F="$(dirname $0)/$2"
    fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
812 813 814 815 816 817 818 819

    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
820 821 822
        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
823 824 825
    done
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
826
set_bigtmpdir()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
827
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
828 829 830 831
    # 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
Vitaly Lipatov's avatar
Vitaly Lipatov committed
832 833 834 835
    if [ -z "$BIGTMPDIR" ] ; then
        BIGTMPDIR="/var/tmp"
        [ -d "$BIGTMPDIR" ] || BIGTMPDIR="$TMPDIR"
    fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
836 837 838 839 840 841 842
    export BIGTMPDIR
}

assure_tmpdir()
{
    if [ -z "$TMPDIR" ] ; then
        export TMPDIR="/tmp"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
843
        debug "Your have no TMPDIR defined. Using $TMPDIR as fallback."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
844 845 846 847
    fi

    if [ ! -d "$TMPDIR" ] ; then
        fatal "TMPDIR $TMPDIR does not exist."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
848 849 850 851 852 853 854
    fi

    if [ ! -w "$TMPDIR" ] ; then
        fatal "TMPDIR $TMPDIR is not writable."
    fi
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
855 856 857 858 859 860 861 862
test_shell()
{
    local R
    R="$($CMDSHELL /dev/null 2>&1)"
    [ -n "$R" ] && fatal "$CMDSHELL is broken (bash wrongly printing out '$R'). Check ~/.bashrc and /etc/bashrc, run $CMDSHELL manually for test."
}


Vitaly Lipatov's avatar
Vitaly Lipatov committed
863 864
set_distro_info()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
865 866 867

    test_shell

Vitaly Lipatov's avatar
Vitaly Lipatov committed
868 869 870 871 872 873 874 875 876 877 878 879
    assure_tmpdir

    set_bigtmpdir

    # don't run again in subprocesses
    [ -n "$DISTRVENDOR" ] && return 0

    DISTRVENDOR=internal_distr_info

    # export pack of variables, see epm print info --print-eepm-env
    [ -n "$verbose" ] && $DISTRVENDOR --print-eepm-env
    eval $($DISTRVENDOR --print-eepm-env | grep -v '^ *#')
Vitaly Lipatov's avatar
Vitaly Lipatov committed
880 881 882 883
}

set_pm_type()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
884 885
    local CMD
    set_distro_info
Vitaly Lipatov's avatar
Vitaly Lipatov committed
886

Vitaly Lipatov's avatar
Vitaly Lipatov committed
887 888 889 890
if [ -n "$EPM_BACKEND" ] ; then
    PMTYPE=$EPM_BACKEND
    return
fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
891
if [ -n "$FORCEPM" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
892 893
    PMTYPE=$FORCEPM
    return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
894 895 896 897
fi

}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
898 899
is_active_systemd()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
900
    [ "$DISTRCONTROL" = "systemd" ]
Vitaly Lipatov's avatar
Vitaly Lipatov committed
901 902
}

903 904
assure_distr()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
905 906 907
    local TEXT="this option"
    [ -n "$2" ] && TEXT="$2"
    [ "$DISTRNAME" = "$1" ] || fatal "$TEXT supported only for $1 distro"
908 909
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
910 911 912
get_pkg_name_delimiter()
{
   local pkgtype="$1"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
913
   [ -n "$pkgtype" ] || pkgtype="$PKGFORMAT"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
914 915 916 917 918

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

Vitaly Lipatov's avatar
Vitaly Lipatov committed
919 920
__epm_remove_tmp_files()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
921 922 923
    trap "-" EXIT
    [ -n "$DEBUG" ] && return 0

Vitaly Lipatov's avatar
Vitaly Lipatov committed
924 925
    [ -n "$verbose" ] && info "Removing tmp files on exit ..."

Vitaly Lipatov's avatar
Vitaly Lipatov committed
926 927
    if [ -n "$to_clean_tmp_dirs" ] ; then
        echo "$to_clean_tmp_dirs" | while read p ; do
Vitaly Lipatov's avatar
Vitaly Lipatov committed
928 929
            [ -n "$verbose" ] && echo "rm -rf '$p'"
            rm -rf "$p" 2>/dev/null
Vitaly Lipatov's avatar
Vitaly Lipatov committed
930 931 932 933 934
        done
    fi

    if [ -n "$to_clean_tmp_files" ] ; then
        echo "$to_clean_tmp_files" | while read p ; do
Vitaly Lipatov's avatar
Vitaly Lipatov committed
935
            rm $verbose -f "$p" 2>/dev/null
Vitaly Lipatov's avatar
Vitaly Lipatov committed
936
        done
Vitaly Lipatov's avatar
Vitaly Lipatov committed
937
    fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
938

Vitaly Lipatov's avatar
Vitaly Lipatov committed
939 940 941 942
    return 0
}


Vitaly Lipatov's avatar
Vitaly Lipatov committed
943 944
remove_on_exit()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
945 946 947 948
    if [ -z "$set_remove_on_exit" ] ; then
        trap "__epm_remove_tmp_files" EXIT
        set_remove_on_exit=1
    fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
949 950
    while [ -n "$1" ] ; do
        if [ -d "$1" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
951 952
            to_clean_tmp_dirs="$to_clean_tmp_dirs
$1"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
953
        elif [ -f "$1" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
954 955
            to_clean_tmp_files="$to_clean_tmp_files
$1"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
956 957 958 959 960
        fi
        shift
    done
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
961 962
has_space()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
963 964 965
        # not for dash:
        [ "$1" != "${1/ //}" ]
        # [ "$(echo "$*" | sed -e "s| ||")" != "$*" ]
Vitaly Lipatov's avatar
Vitaly Lipatov committed
966 967
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
968

Vitaly Lipatov's avatar
Vitaly Lipatov committed
969 970 971 972 973
is_url()
{
    echo "$1" | grep -q "^[filehtps]*:/"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
974
if a= type -a type 2>/dev/null >/dev/null ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
975 976
print_command_path()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
977
    a= type -fpP -- "$1" 2>/dev/null
Vitaly Lipatov's avatar
Vitaly Lipatov committed
978
}
Vitaly Lipatov's avatar
Vitaly Lipatov committed
979 980 981
elif 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 a binary, not builtin shell command
Vitaly Lipatov's avatar
Vitaly Lipatov committed
982 983
print_command_path()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
984
    a= which -- "$1" 2>/dev/null
Vitaly Lipatov's avatar
Vitaly Lipatov committed
985 986 987 988
}
else
print_command_path()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
989
    a= type "$1" 2>/dev/null | sed -e 's|.* /|/|'
Vitaly Lipatov's avatar
Vitaly Lipatov committed
990 991 992 993 994 995 996 997 998
}
fi

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


Vitaly Lipatov's avatar
Vitaly Lipatov committed
999
if ! is_command realpath ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1000 1001 1002
realpath()
{
    [ -n "$*" ] || return
1003 1004 1005 1006 1007
    if [ "$1" = "-s" ] ; then
        shift
        echo "$(cd "$(dirname "$1")" && pwd -P)/$(basename "$1")" #"
        return
    fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1008 1009 1010 1011
    readlink -f "$@"
}
fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1012

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1013 1014

if ! is_command subst ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1015 1016 1017 1018 1019 1020
subst()
{
    sed -i -e "$@"
}
fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1021 1022
check_core_commands()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1023 1024 1025
    #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
1026 1027 1028
}


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

serv_cat()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
    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
1052 1053
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1054 1055 1056 1057
# File bin/serv-common:

serv_common()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
    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
1085 1086 1087 1088 1089 1090 1091
}

# File bin/serv-disable:


serv_disable()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
    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
1120 1121
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1122 1123 1124 1125
# File bin/serv-edit:

serv_edit()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1126 1127
    local SERVICE="$1"
    shift
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1128

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1129 1130 1131 1132 1133 1134 1135 1136
    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
1137 1138
}

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


Vitaly Lipatov's avatar
Vitaly Lipatov committed
1142
serv_enable()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1143
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
    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
1179 1180
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1181 1182 1183
# File bin/serv-exists:

serv_exists()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1184
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
    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
1204 1205
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1206 1207 1208 1209
# File bin/serv-list:

serv_list()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
    [ -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
1236 1237 1238 1239 1240 1241
}

# File bin/serv-list_all:

serv_list_all()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1242 1243 1244 1245 1246 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 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
    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
1285 1286
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1287 1288 1289 1290
# File bin/serv-list_failed:

serv_list_failed()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
    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
1303 1304
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1305 1306 1307 1308
# File bin/serv-list_startup:

serv_list_startup()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
    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
1323 1324
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1325 1326 1327 1328
# File bin/serv-log:

__serv_log_altlinux()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
    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
1350 1351 1352 1353
}

serv_log()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
    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
1372 1373
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1374 1375 1376 1377 1378
# File bin/serv-off:


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

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1381 1382 1383
    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
1384 1385 1386 1387 1388 1389 1390
}

# File bin/serv-on:


serv_on()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1391 1392 1393 1394
    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
1395 1396
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1397 1398 1399 1400
# File bin/serv-print:

serv_print()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1401 1402
    echo "Detected init system: $SERVICETYPE"
    [ -n "$ANYSERVICE" ] && echo "anyservice is detected too"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1403 1404 1405 1406 1407 1408 1409
}

# File bin/serv-reload:


serv_reload()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
    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
1432 1433
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1434 1435 1436 1437 1438
# File bin/serv-restart:


serv_restart()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
    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
1466 1467
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1468 1469 1470 1471
# File bin/serv-start:

serv_start()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
    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
1499 1500 1501 1502 1503 1504
}

# File bin/serv-status:

is_service_running()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531
    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
1532 1533 1534 1535
}

is_service_autostart()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
    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
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1546
            LC_ALL=C sudorun chkconfig $1 --list | grep -q "[35]:on"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
            ;;
        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
1561 1562 1563 1564
}

serv_status()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590
    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
1591 1592 1593 1594 1595 1596
}

# File bin/serv-stop:

serv_stop()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
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
    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
1624 1625
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1626 1627 1628 1629
# File bin/serv-test:

serv_test()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
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
    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
1657 1658
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1659 1660 1661 1662 1663
# File bin/serv-try_restart:


serv_try_restart()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1664 1665
    local SERVICE="$1"
    shift
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1666

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676
    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
1677 1678 1679 1680 1681 1682
}

# File bin/serv-usage:

_print_additional_usage()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1683
    echo "serv addition usage: {on|off|try-restart|usage}"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1684 1685 1686 1687
}

serv_usage()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
    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
1707 1708 1709 1710

_print_additional_usage

}
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1711

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1712
################# incorporate bin/distr_info #################
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1713 1714
internal_distr_info()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1715 1716 1717
# 2007-2023 (c) Vitaly Lipatov <lav@etersoft.ru>
# 2007-2023 (c) Etersoft
# 2007-2023 Public domain
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1718 1719 1720 1721

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

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1722
PROGVERSION="20230406"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1723

1724 1725
# TODO: check /etc/system-release

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1726 1727 1728
# Check for DISTRO specific file in /etc
distro()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1729 1730 1731 1732
    #[ -n "$ROOTDIR" ] || return
    # fill global DISTROFILE
    DISTROFILE="$ROOTDIR/etc/$1"
    [ -f "$DISTROFILE" ]
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1733 1734 1735 1736 1737
}

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

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1742 1743
# copied from epm-sh-functions
# print a path to the command if exists in $PATH
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1744
if a='' which which 2>/dev/null >/dev/null ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1745 1746 1747
    # 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()
1748
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1749
    a='' which -- "$1" 2>/dev/null
1750
}
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1751
elif a='' type -a type 2>/dev/null >/dev/null ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1752 1753
print_command_path()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1754
    a='' type -fpP -- "$1" 2>/dev/null
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1755 1756 1757 1758
}
else
print_command_path()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1759
    a='' type "$1" 2>/dev/null | sed -e 's|.* /|/|'
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1760 1761 1762 1763 1764 1765 1766 1767 1768 1769
}
fi

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

1770

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1771 1772
firstupper()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1773 1774
    # FIXME: works with GNU sed only
    echo "$*" | sed 's/.*/\u&/'
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1775 1776
}

1777 1778
tolower()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1779 1780 1781
    # tr is broken in busybox (checked with OpenWrt)
    #echo "$*" | tr "[:upper:]" "[:lower:]"
    echo "$*" | awk '{print tolower($0)}'
1782 1783
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1784 1785
print_bug_report_url()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1786
    echo "$BUG_REPORT_URL"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1787 1788
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1789
# allows x86_64/Distro/Version
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1790 1791
override_distrib()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1792 1793
    [ -n "$DISTRNAMEOVERRIDE" ] || DISTRNAMEOVERRIDE="$1"
    [ -n "$DISTRNAMEOVERRIDE" ] || return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1794

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1795 1796 1797 1798 1799
    local name="$(echo "$DISTRNAMEOVERRIDE" | sed -e 's|x86_64/||')"
    [ "$name" = "$DISTRNAMEOVERRIDE" ] && DIST_ARCH="x86" || DIST_ARCH="x86_64"
    DISTRIB_ID="$(echo "$name" | sed -e 's|/.*||')"
    DISTRIB_RELEASE="$(echo "$name" | sed -e 's|.*/||')"
    [ "$DISTRIB_ID" = "$DISTRIB_RELEASE" ] && DISTRIB_RELEASE=''
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1800

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1801
    VENDOR_ID=''
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1802
    PRETTY_NAME="$DISTRIB_ID"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1803 1804 1805
    DISTRO_NAME="$DISTRIB_ID"
    DISTRIB_CODENAME="$DISTRIB_RELEASE"
    DISTRIB_FULL_RELEASE="$DISTRIB_RELEASE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1806

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1807 1808
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1809 1810
# 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
1811
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1812 1813 1814 1815 1816 1817 1818 1819 1820
    [ "$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
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1821
    [ "$DISTRIB_ID" = "ManjaroLinux" ] && echo "manjaro" && return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1822 1823 1824 1825 1826 1827 1828 1829 1830
    [ "$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
1831 1832
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1833 1834 1835
# TODO: in more appropriate way
#which pkcon 2>/dev/null >/dev/null && info "You can run $ PMTYPE=packagekit epm to use packagekit backend"

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1836
# Print package manager (need DISTRIB_ID, DISTRIB_RELEASE vars)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1837 1838 1839
pkgmanager()
{
local CMD
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1840 1841 1842 1843

case $VENDOR_ID in
    alt)
        echo "apt-rpm" && return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1844
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1845 1846
    arch|manjaro)
        echo "pacman" && return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1847
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1848 1849 1850 1851 1852 1853 1854
    debian)
        echo "apt-dpkg" && return
        ;;
esac

# 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
1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882
    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"
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1883
    ArchLinux|ManjaroLinux)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900
        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)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1901 1902
        is_command winget && echo "winget" && return
        is_command appget && CMD="appget"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1903
        is_command choco && CMD="choco"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1904
        is_command npackdcl && CMD="npackd"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1905 1906 1907 1908 1909 1910 1911 1912 1913 1914
        ;;
    MacOS)
        CMD="homebrew"
        ;;
    OpenWrt)
        CMD="opkg"
        ;;
    GNU/Linux/Guix)
        CMD="guix"
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1915 1916 1917
    NixOS)
        CMD="nix"
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1918 1919
    Android)
        CMD="android"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1920
        # TODO: CMD="termux-pkg"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935
        ;;
    Cygwin)
        CMD="aptcyg"
        ;;
    AlpineLinux)
        CMD="apk"
        ;;
    TinyCoreLinux)
        CMD="tce"
        ;;
    VoidLinux)
        CMD="xbps"
        ;;
    *)
        if is_command "rpm" && [ -s /var/lib/rpm/Name ] || [ -s /var/lib/rpm/rpmdb.sqlite ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1936
            is_command "apt-get" && [ -d /var/lib/apt ] && echo "apt-rpm" && return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947
            is_command "zypper" && echo "zypper-rpm" && return
            is_command "dnf" && echo "dnf-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

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1948
        echo "pkgmanager(): We don't support yet DISTRIB_ID $DISTRIB_ID (VENDOR_ID $VENDOR_ID)" >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1949
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1950 1951 1952 1953
esac
echo "$CMD"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1954 1955 1956
# Print pkgtype (need DISTRIB_ID var)
pkgtype()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1957 1958 1959 1960 1961 1962 1963

    case $VENDOR_ID in
        arch|manjaro)
            echo "pkg.tar.xz" && return
            ;;
    esac

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1964 1965
# TODO: try use generic names
    case $(pkgvendor) in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985
        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" ;;
                *)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1986
                    echo "" ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1987 1988
            esac
    esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1989 1990
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1991 1992
print_codename()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1993
    echo "$DISTRIB_CODENAME"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1994
}
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1995

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1996 1997
print_repo_name()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1998
    echo "$DISTRIB_CODENAME"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1999 2000
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2001 2002
get_var()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2003 2004
    # 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
2005 2006 2007 2008 2009
}

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

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2013 2014
normalize_name()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2015 2016 2017 2018 2019 2020 2021
    case "$1" in
        "RED OS")
            echo "RedOS"
            ;;
        "Debian GNU/Linux")
            echo "Debian"
            ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2022 2023 2024
        "Liya GNU/Linux")
            echo "LiyaLinux"
            ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039
        "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"
            ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2040
        "MOS Desktop"|"MOS Panel")
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2041 2042
            echo "ROSA"
            ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054
        "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
2055 2056
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2057 2058
# 1.2.3.4.5 -> 1
normalize_version1()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2059
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2060
    echo "$1" | sed -e "s|\..*||"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2061 2062
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075
# 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
2076 2077
fill_distr_info()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2078
# Default values
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2079
PRETTY_NAME=""
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2080
DISTRIB_ID=""
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2081
DISTRIB_RELEASE=""
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2082 2083
DISTRIB_FULL_RELEASE=""
DISTRIB_RELEASE_ORIG=""
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2084
DISTRIB_CODENAME=""
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2085 2086
BUG_REPORT_URL=""
BUILD_ID=""
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2087

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2088
# Default detection by /etc/os-release
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2089 2090
# https://www.freedesktop.org/software/systemd/man/os-release.html
if distro os-release ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2091 2092 2093 2094 2095 2096 2097
    # 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"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2098 2099
    [ "$BUILD_ID" = "rolling" ] && DISTRIB_RELEASE="rolling"
    [ -n "$BUG_REPORT_URL" ] || BUG_REPORT_URL="$HOME_URL"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2100 2101 2102
    # set by os-release:
    #PRETTY_NAME
    VENDOR_ID="$ID"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2103
    case "$VENDOR_ID" in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2104
        ubuntu|reld|rhel|astra|manjaro)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2105 2106
            ;;
        *)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2107 2108
            # ID_LIKE can be 'rhel centos fedora', use latest word
            [ -n "$ID_LIKE" ] && VENDOR_ID="$(echo "$ID_LIKE" | xargs -n1 | tail -n1)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2109 2110
            ;;
    esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2111 2112
    DISTRIB_FULL_RELEASE="$DISTRIB_RELEASE"
    DISTRIB_CODENAME="$VERSION_CODENAME"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2113

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2114
elif distro lsb-release ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2115 2116 2117 2118 2119 2120 2121
    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
2122 2123
fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2124
DISTRIB_RELEASE=$(normalize_version2 "$DISTRIB_RELEASE")
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2125
[ -n "$DISTRIB_CODENAME" ] || DISTRIB_CODENAME=$DISTRIB_RELEASE
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2126 2127

case "$VENDOR_ID" in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151
    "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
2152 2153 2154
esac

case "$DISTRIB_ID" in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2155 2156 2157 2158
    "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"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2159
        echo "$VERSION" | grep -q "c9f3 branch" && DISTRIB_RELEASE="c9f3"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2160 2161
        DISTRIB_CODENAME="$DISTRIB_RELEASE"
        # FIXME: fast hack for fallback: 10.1 -> p10 for /etc/os-release
2162 2163 2164 2165
        if echo "$DISTRIB_RELEASE" | grep -q "^0" ; then
            DISTRIB_RELEASE="Sisyphus"
            DISTRIB_CODENAME="$DISTRIB_RELEASE"
        elif echo "$DISTRIB_RELEASE" | grep -q "^[0-9]" && echo "$DISTRIB_RELEASE" | grep -q -v "[0-9][0-9][0-9]"  ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2166 2167 2168
            DISTRIB_CODENAME="$(echo p$DISTRIB_RELEASE | sed -e 's|\..*||')"
            # TODO: change p10 to 10
            DISTRIB_RELEASE="$DISTRIB_CODENAME"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2169 2170 2171
        fi
        ;;
    "ALTServer")
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2172
        DISTRIB_ID="ALTLinux"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2173
        DISTRIB_CODENAME="$(echo p$DISTRIB_RELEASE | sed -e 's|\..*||')"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2174 2175
        # TODO: change p10 to 10
        DISTRIB_RELEASE="$DISTRIB_CODENAME"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191
        ;;
    "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
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2192
        [ -n "$ALT_BRANCH_ID" ] && DISTRIB_RELEASE="$ALT_BRANCH_ID"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2193 2194 2195 2196 2197 2198 2199 2200
        DISTRIB_CODENAME="$DISTRIB_RELEASE"
#        DISTRIB_RELEASE=$(echo $DISTRIB_RELEASE | sed -e "s/\..*//g")
        ;;
    "Sisyphus")
        DISTRIB_ID="ALTLinux"
        DISTRIB_RELEASE="Sisyphus"
        DISTRIB_CODENAME="$DISTRIB_RELEASE"
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2201
    "ROSA"|"MOSDesktop"|"MOSPanel")
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2202 2203 2204
        DISTRIB_FULL_RELEASE="$DISTRIB_CODENAME"
        DISTRIB_CODENAME="$DISTRIB_RELEASE"
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2205 2206 2207
esac


Vitaly Lipatov's avatar
Vitaly Lipatov committed
2208
[ -n "$DISTRIB_ID" ] && [ -n "$DISTRIB_RELEASE" ] && return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2209 2210 2211 2212


# check via obsoleted ways

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2213 2214
# ALT Linux based
if distro altlinux-release ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252
    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
2253 2254 2255
    DISTRIB_CODENAME="$DISTRIB_RELEASE"
    DISTRO_NAME="$DISTRIB_ID"
    DISTRIB_FULL_RELEASE="$DISTRIB_RELEASE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2256 2257

elif distro gentoo-release ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2258 2259 2260 2261
    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)") #"
2262

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2263
elif distro slackware-version ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2264 2265
    DISTRIB_ID="Slackware"
    DISTRIB_RELEASE="$(grep -Eo '[0-9]+\.[0-9]+' $DISTROFILE)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2266

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2267
elif distro os-release && is_command tce-ab ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2268 2269 2270 2271
    # shellcheck disable=SC1090
    . $ROOTDIR/etc/os-release
    DISTRIB_ID="TinyCoreLinux"
    DISTRIB_RELEASE="$VERSION_ID"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2272

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2273
elif distro os-release && is_command xbps-query ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2274 2275 2276 2277
    # shellcheck disable=SC1090
    . $ROOTDIR/etc/os-release
    DISTRIB_ID="VoidLinux"
    DISTRIB_RELEASE="Live"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2278

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2279
# TODO: use standart /etc/os-release or lsb
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2280
elif distro arch-release ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2281 2282
    DISTRIB_ID="ArchLinux"
    DISTRIB_RELEASE="rolling"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2283

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2284
# Elbrus
2285
elif distro mcst_version ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2286 2287
    DISTRIB_ID="MCST"
    DISTRIB_RELEASE=$(cat "$DISTROFILE" | grep "release" | sed -e "s|.*release \([0-9]*\).*|\1|g") #"
2288

2289 2290
# OpenWrt
elif distro openwrt_release ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2291 2292
    . $DISTROFILE
    DISTRIB_RELEASE=$(cat $ROOTDIR/etc/openwrt_version)
2293

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2294 2295
# Debian based
elif distro debian_version ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2296 2297
    DISTRIB_ID="Debian"
    DISTRIB_RELEASE=$(cat $DISTROFILE | sed -e "s/\..*//g")
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2298 2299 2300 2301


# SUSE based
elif distro SuSe-release || distro SuSE-release ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2302 2303 2304 2305 2306 2307 2308
    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
2309 2310

# fixme: can we detect by some file?
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2311
elif [ "$(uname)" = "FreeBSD" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2312 2313 2314
    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
2315 2316

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

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2321
# fixme: can we detect by some file?
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2322
elif [ "$(uname -s 2>/dev/null)" = "Darwin" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2323 2324
    DISTRIB_ID="MacOS"
    DISTRIB_RELEASE=$(uname -r)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2325 2326

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

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2331
# fixme: move to up
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2332
elif [ "$(uname)" = "Linux" ] && [ -x $ROOTDIR/system/bin/getprop ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2333
    DISTRIB_ID="Android"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2334 2335
    DISTRIB_RELEASE=$(getprop | awk -F": " '/system.build.version.release\]/ { print $2 }' | tr -d '[]' | head -n1)
    [ -n "$DISTRIB_RELEASE" ] || DISTRIB_RELEASE=$(getprop | awk -F": " '/build.version.release/ { print $2 }' | tr -d '[]' | head -n1)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2336

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2337
elif [ "$(uname -o 2>/dev/null)" = "Cygwin" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2338 2339
        DISTRIB_ID="Cygwin"
        DISTRIB_RELEASE="all"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2340
fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2341

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2342 2343
}

2344 2345
get_uname()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2346
    tolower "$(uname $1)" | tr -d " \t\r\n"
2347 2348
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2349 2350 2351 2352 2353 2354 2355
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
2356 2357 2358 2359
get_base_os_name()
{
local DIST_OS
# Resolve the os
2360
DIST_OS="$(get_uname -s)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380
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"
}

2381

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2382 2383
get_arch()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2384
[ -n "$DIST_ARCH" ] && return 0
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2385
# Resolve the architecture
2386
DIST_ARCH="$(get_uname -m)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2387
case "$DIST_ARCH" in
2388
    'ia32' | 'i386' | 'i486' | 'i586' | 'i686')
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2389 2390
        DIST_ARCH="x86"
        ;;
2391 2392 2393
    'amd64' | 'x86_64')
        DIST_ARCH="x86_64"
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411
    '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
2412 2413 2414 2415 2416 2417
    'arm64' | 'aarch64')
        DIST_ARCH='aarch64'
        ;;
    armv7*)
        # TODO: use uname only
        # uses binutils package
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2418
        if is_command readelf && [ -z "$(readelf -A /proc/self/exe | grep Tag_ABI_VFP_args)" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2419 2420 2421 2422 2423 2424 2425 2426 2427
            DIST_ARCH="armel"
        else
            DIST_ARCH="armhf"
        fi
        ;;
esac
echo "$DIST_ARCH"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2428 2429 2430 2431
get_debian_arch()
{
    local arch="$(get_arch)"
    case $arch in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2432
    'x86')
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2433 2434 2435
        arch='i386' ;;
    'x86_64')
        arch='amd64' ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2436 2437
    'aarch64')
        arch='arm64' ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2438 2439 2440 2441
    esac
    echo "$arch"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2442 2443 2444 2445 2446
get_distro_arch()
{
    local arch="$(get_arch)"
    case "$(pkgtype)" in
        rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2447 2448 2449 2450
            case $arch in
            'x86')
                arch='i586' ;;
            esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2451 2452
            ;;
        deb)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2453 2454
            get_debian_arch
            return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2455 2456 2457 2458 2459
            ;;
    esac
    echo "$arch"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2460 2461 2462
get_bit_size()
{
local DIST_BIT
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2463 2464 2465 2466 2467 2468 2469 2470 2471

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
2472 2473 2474
    'amd64' | 'ia64' | 'x86_64' | 'ppc64')
        DIST_BIT="64"
        ;;
2475 2476 2477
    'aarch64')
        DIST_BIT="64"
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2478 2479 2480
    'e2k')
        DIST_BIT="64"
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496
#    '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"
}

2497
# TODO: check before calc
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2498 2499 2500
get_memory_size()
{
    local detected=""
2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511
    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
2512
        solaris)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2513
            detected=$(prtconf | grep Memory | sed -e "s|Memory size: \([0-9][0-9]*\) Megabyte.*|\1|") #"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2514 2515 2516
            ;;
#        *)
#            fatal "Unsupported OS $DIST_OS"
2517 2518
    esac

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2519
    [ -n "$detected" ] || detected=0
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2520 2521 2522
    echo $detected
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2523 2524 2525 2526 2527
print_name_version()
{
    [ -n "$DISTRIB_RELEASE" ] && echo $DISTRIB_ID/$DISTRIB_RELEASE || echo $DISTRIB_ID
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2528 2529 2530 2531 2532 2533
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
2534
            detected=$(a= sysctl hw.ncpu | awk '{print $2}')
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2535 2536 2537 2538 2539
            ;;
        linux)
            detected=$(grep -c "^processor" /proc/cpuinfo)
            ;;
        solaris)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2540
            detected=$(a= prtconf | grep -c 'cpu[^s]')
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2541 2542
            ;;
        aix)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2543
            detected=$(a= lsdev -Cc processor -S A | wc -l)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2544 2545 2546 2547 2548 2549 2550 2551 2552
            ;;
#        *)
#            fatal "Unsupported OS $DIST_OS"
    esac

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

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2553 2554 2555 2556 2557
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
2558

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2559 2560 2561
get_virt()
{
    local VIRT
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2562 2563
    if is_command systemd-detect-virt ; then
        VIRT="$(systemd-detect-virt)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2564 2565 2566 2567
        [ "$VIRT" = "none" ] && echo "(host system)" && return
        [ -z "$VIRT" ] && echo "(unknown)" && return
        echo "$VIRT" && return
    fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2568 2569 2570 2571 2572

    # 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
2573 2574
        echo "openvz" && return
    fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2575 2576 2577 2578 2579

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

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2580
    # use util-linux
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2581
    if LC_ALL=C a= lscpu 2>/dev/null | grep "Hypervisor vendor:" | grep -q "KVM" ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2582 2583 2584
        echo "kvm" && return
    fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2585 2586 2587 2588
    echo "(unknown)"
    # TODO: check for openvz
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2589 2590 2591 2592 2593 2594 2595
get_init_process_name()
{
    [ ! -f /proc/1/comm ] && echo "(unknown)" && return 1
    cat /proc/1/comm | head -n1
    #ps --no-headers -o comm 1
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2596 2597 2598 2599
# 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
2600 2601
    # TODO
    #[ -d /usr/share/upstart ] && echo "upstart" && return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2602
    is_command systemctl && [ "$(get_init_process_name)" = 'systemd' ] && echo "systemd" && return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2603
    [ -d /etc/init.d ] && echo "sysvinit" && return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2604
    get_init_process_name
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2605 2606
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2607 2608 2609 2610 2611
filter_duplicated_words()
{
    echo "$*" | xargs -n1 echo | uniq | xargs -n100 echo
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2612 2613
print_pretty_name()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2614 2615 2616 2617
    if [ -z "$PRETTY_NAME" ] ; then
        PRETTY_NAME="$DISTRIB_ID $DISTRIB_RELEASE"
    fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2618 2619 2620 2621
    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
2622 2623 2624 2625
    if ! echo "$PRETTY_NAME" | grep -q "$DISTRIB_RELEASE" ; then
        PRETTY_NAME="$PRETTY_NAME ($DISTRIB_RELEASE)"
    fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2626
    echo "$(filter_duplicated_words "$PRETTY_NAME")"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2627
}
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2628 2629 2630

print_total_info()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2631
local orig=''
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2632
[ -n "$BUILD_ID" ] && [ "$DISTRIB_FULL_RELEASE" != "$BUILD_ID" ] && orig=" (orig. $BUILD_ID)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2633 2634
local EV=''
[ -n "$EPMVERSION" ] && EV="(EPM version $EPMVERSION) "
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2635
cat <<EOF
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2636
distro_info v$PROGVERSION $EV: Copyright © 2007-2024 Etersoft
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2637

2638 2639
                       Pretty name (--pretty): $(print_pretty_name)
           (--distro-name / --distro-version): $DISTRO_NAME / $DISTRIB_FULL_RELEASE$orig
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2640 2641 2642 2643
         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)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2644
                 CPU norm register size  (-b): $(get_bit_size) bit
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2645 2646
                          Virtualization (-i): $(get_virt)
                        CPU Cores/MHz (-c/-z): $(get_core_count) / $(get_core_mhz) MHz
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2647
                      System memory size (-m): $(get_memory_size) MiB
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2648
                 Running service manager (-y): $(get_service_manager)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2649
            Bug report URL (--bug-report-url): $(print_bug_report_url)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2650 2651

(run with -h to get help)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2652 2653 2654
EOF
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2655 2656 2657 2658 2659 2660
print_help()
{
    echo "distro_info v$PROGVERSION - distro information retriever"
    echo "Usage: distro_info [options] [SystemName/Version]"
    echo "Options:"
    echo " -h | --help            - this help"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2661
    echo " -a                     - print hardware architecture (use --distro-arch for distro depended arch name)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672
    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"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2673 2674
    echo " -p | --package-type    - print type of the packaging system (f.i., apt-dpkg)"
    echo " -g                     - print name of the packaging system (f.i., deb)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691
    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."
}

# print code for eval with names for eepm
print_eepm_env()
{
cat <<EOF
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2692
# -d | --base-distro-name
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2693
DISTRNAME="$(echo $DISTRIB_ID)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2694
# -v | --base-version
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2695 2696 2697
DISTRVERSION="$(echo "$DISTRIB_RELEASE")"
# distro dependent arch
DISTRARCH="$(get_distro_arch)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2698
# -s | --vendor-name
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2699
BASEDISTRNAME=$(pkgvendor)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2700
# --repo-name
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2701
DISTRREPONAME=$(print_repo_name)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2702

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2703 2704 2705 2706
# -a
SYSTEMARCH="$(get_arch)"
# -y | --service-manager
DISTRCONTROL="$(get_service_manager)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2707
# -g
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2708
PMTYPE="$(pkgmanager)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2709
# -p | --package-type
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2710
PKGFORMAT=$(pkgtype)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2711
# -m
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2712
DISTRMEMORY="$(get_memory_size)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2713 2714

# TODO: remove?
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2715 2716
PKGVENDOR=$(pkgvendor)
RPMVENDOR=$(pkgvendor)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2717 2718 2719 2720

EOF

}
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2721

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2722
override_distrib "$DISTRNAMEOVERRIDE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737

if [ -n "$*" ] ; then
    eval lastarg=\${$#}
    case "$lastarg" in
        -*)
            ;;
        *)
            override_distrib "$lastarg"
            # drop last arg
            set -- "${@:1:$(($#-1))}"
            ;;
    esac
fi

# if without override
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2738
if [ -z "$DISTRIB_ID" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2739 2740 2741 2742 2743 2744 2745 2746 2747 2748
    fill_distr_info
    [ -n "$DISTRIB_ID" ] || DISTRIB_ID="Generic"
fi

if [ -z "$1" ] ; then
    print_total_info
    return
fi

while [ -n "$1" ] ; do
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2749
case "$1" in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2750
    -h|--help)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2751
        print_help
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781
        exit 0
        ;;
    -p|--package-type)
        pkgtype
        ;;
    -g)
        pkgmanager
        ;;
    --pretty|--pretty-name)
        print_pretty_name
        ;;
    --distro-arch)
        get_distro_arch
        ;;
    --debian-arch)
        get_debian_arch
        ;;
    --glibc-version)
        get_glibc_version
        ;;
    -d|--base-distro-name)
        echo $DISTRIB_ID
        ;;
    --distro-name)
        echo $DISTRO_NAME
        ;;
    --codename)
        print_codename
        ;;
    -a)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2782 2783 2784 2785 2786
        if [ -n "$DIST_ARCH" ] ; then
            echo "$DIST_ARCH"
        else
            get_arch
        fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832
        ;;
    -b)
        get_bit_size
        ;;
    -c)
        get_core_count
        ;;
    -z)
        get_core_mhz
        ;;
    -i)
        get_virt
        ;;
    -m)
        get_memory_size
        ;;
    -o|--os-name)
        get_base_os_name
        ;;
    -r|--repo-name)
        print_repo_name
        ;;
    --build-id)
        echo "$BUILD_ID"
        ;;
    -v|--base-version)
        echo "$DISTRIB_RELEASE"
        ;;
    --full-version|--distro-version)
        echo "$DISTRIB_FULL_RELEASE"
        ;;
    --bug-report-url)
        print_bug_report_url
        ;;
    -s|-n|--vendor-name)
        pkgvendor
        ;;
    -y|--service-manager)
        get_service_manager
        ;;
    -V)
        echo "$PROGVERSION"
        ;;
    -e)
        print_name_version
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2833 2834 2835 2836
    --print-eepm-env)
        print_eepm_env
        exit 0
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2837 2838
    -*)
        echo "Unsupported option $1" >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2839 2840
        # print empty line in any case
        echo
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2841 2842
        exit 1
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2843
esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2844 2845
shift
done
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2846
}
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2847
################# end of incorporated bin/distr_info #################
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2848

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2849

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2850
serv_main()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2851
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2852

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2853 2854
INITDIR=/etc/init.d

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866
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
2867
    local CMD
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2868

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2869
    set_distro_info
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2870

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2871
case "$DISTRCONTROL" in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2872 2873 2874 2875 2876 2877
    sysvinit)
        CMD="service-chkconfig"
        ;;
    systemd)
        CMD="systemd"
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2878 2879 2880
esac

# override system control detection result
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2881 2882
[ -n "$SERV_BACKEND" ] && CMD="$SERV_BACKEND"
# obsoleted
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2883
[ -n "$FORCESERVICE" ] && CMD="$FORCESERVICE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2884

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2885
SERVICETYPE="$CMD"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2886

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2887
ANYSERVICE=$(print_command_path anyservice)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2888

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2889 2890
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2891 2892 2893
# TODO: done it on anyservice part
is_anyservice()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2894 2895 2896 2897
    [ -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
2898
}
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2899 2900 2901 2902


phelp()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2903
    echo "$Descr
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914
$Usage
 Commands:
$(get_help HELPCMD)

 Options:
$(get_help HELPOPT)
"
}

print_version()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2915
        local on_text="(host system)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2916 2917
        local virt="$($DISTRVENDOR -i)"
        [ "$virt" = "(unknown)" ] || [ "$virt" = "(host system)" ] || on_text="(under $virt)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2918
        echo "Service manager version $EPMVERSION  https://wiki.etersoft.ru/Epm"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2919
        echo "Running on $DISTRNAME/$DISTRVERSION $on_text with $SERVICETYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2920
        echo "Copyright (c) Etersoft 2012-2023"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2921 2922 2923
        echo "This program may be freely redistributed under the terms of the GNU AGPLv3."
}

2924 2925 2926
progname="${0##*/}"

Usage="Usage: $progname [options] [<service>] [<command>] [params]..."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2927 2928 2929 2930 2931 2932
Descr="serv - Service manager"

set_service_type

verbose=
quiet=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2933
short=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2934 2935 2936 2937 2938
non_interactive=
show_command_only=
serv_cmd=
service_name=
params=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2939
withoutservicename=
2940

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2941 2942 2943
# load system wide config
[ -f /etc/eepm/serv.conf ] && . /etc/eepm/serv.conf

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2944 2945 2946 2947 2948 2949 2950 2951 2952
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
2953 2954 2955
    restart)                 # HELPCMD: restart service
        serv_cmd=restart
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2956 2957 2958
    reload)                  # HELPCMD: reload service
        serv_cmd=reload
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2959 2960 2961 2962 2963 2964
    start)                    # HELPCMD: start service
        serv_cmd=start
        ;;
    stop)                     # HELPCMD: stop service
        serv_cmd=stop
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2965 2966 2967 2968 2969 2970 2971
    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
2972 2973
        serv_cmd=enable
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2974
    disable)                 # HELPCMD: remove service to run on startup (see 'off' also)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2975 2976 2977 2978 2979 2980 2981 2982
        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
2983 2984 2985
    exists)                   # HELPCMD: check if the service is installed on the system
        serv_cmd=exists
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2986 2987 2988 2989 2990 2991
    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
2992 2993
    list)                     # HELPCMD: list running services
        serv_cmd=list
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2994
        withoutservicename=1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2995 2996 2997
        ;;
    list-all)                 # HELPCMD: list all available services
        serv_cmd=list_all
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2998
        withoutservicename=1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2999 3000 3001
        ;;
    list-startup)             # HELPCMD: list all services to run on startup
        serv_cmd=list_startup
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3002
        withoutservicename=1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3003
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3004 3005 3006 3007
    list-failed|--failed)       # HELPCMD: list services failed on startup
        serv_cmd=list_failed
        withoutservicename=1
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3008 3009 3010 3011
    print)                    # HELPCMD: print some info
        serv_cmd=print
        withoutservicename=1
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3012 3013
    try-restart|condrestart)  # HELPCMD: Restart service if running
        serv_cmd=try_restart
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3014
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3015 3016 3017
    usage)                    # HELPCMD: print out usage of the service
        serv_cmd=usage
        withoutservicename=1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3018
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038
    *)
        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
3039 3040 3041
        ;;
    --short)              # HELPOPT: short mode
        short=1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069
        ;;
    --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
3070
if [ -z "$withoutservicename" ] && [ -z "$service_name" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3071 3072
    print_version
    echo
3073
    fatal "Run $ $progname --help for get help"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3074 3075
fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3076
# use common way if the command is unknown
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3077 3078 3079 3080 3081 3082 3083
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
3084 3085
}
serv_main "$@"