epm-sh-functions 16.6 KB
Newer Older
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1 2
#!/bin/sh
#
3 4
# Copyright (C) 2012, 2014  Etersoft
# Copyright (C) 2012, 2014  Vitaly Lipatov <lav@etersoft.ru>
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5
#
6 7 8
# 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
Vitaly Lipatov's avatar
Vitaly Lipatov committed
9 10 11 12 13
# (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
14
# GNU Affero General Public License for more details.
Vitaly Lipatov's avatar
Vitaly Lipatov committed
15
#
16 17
# 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
18 19
#

20
# copied from /etc/init.d/outformat (ALT Linux)
21

22

23
# FIXME on Android: FIX ME! implement ttyname_r() bionic/libc/bionic/stubs.c:366
24 25 26
inputisatty()
{
	# check stdin
27 28
	#tty -s 2>/dev/null
	test -t 0
29 30
}

31 32
isatty()
{
33 34 35 36 37 38
	# check stdout
	test -t 1
}

isatty2()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
39 40
	# check stderr
	test -t 2
41 42
}

43 44
check_tty()
{
45 46 47 48 49 50
	isatty2 || return

	# Set a sane TERM required for tput
	[ -n "$TERM" ] || TERM=dumb
	export TERM

51 52
	check_core_commands

Vitaly Lipatov's avatar
Vitaly Lipatov committed
53 54
	# grep -E from busybox may not --color
	# grep -E from MacOS print help to stderr
Vitaly Lipatov's avatar
Vitaly Lipatov committed
55
	if grep -E --help 2>&1 | grep -q -- "--color" ; then
56
		export EGREPCOLOR="--color"
57 58
	fi

59
	is_command tput || return
60
	# FreeBSD does not support tput -S
61
	echo | a= tput -S >/dev/null 2>/dev/null || return
62
	USETTY="tput -S"
63 64
}

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

set_boldcolor()
{
69
	[ -n "$USETTY" ] || return
70 71 72
	{
		echo bold
		echo setaf $1
73
	} | $USETTY
74 75
}

76 77
set_color()
{
78
	[ -n "$USETTY" ] || return
79 80
	{
		echo setaf $1
81
	} | $USETTY
82 83
}

84 85
restore_color()
{
86
	[ -n "$USETTY" ] || return
87 88 89
	{
		echo op; # set Original color Pair.
		echo sgr0; # turn off all special graphics mode (bold in our case).
90
	} | $USETTY
91 92
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
93 94
echover()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
95
    [ -z "$verbose" ] && return
96
    echo "$*" >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
97 98
}

99 100 101
# echo string without EOL
echon()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
102
	# default /bin/sh on MacOS does not recognize -n
103
	echo -n "$*" 2>/dev/null || a= /bin/echo -n "$*"
104 105 106
}


Vitaly Lipatov's avatar
Vitaly Lipatov committed
107 108 109
# Used DISTRNAME
set_target_pkg_env()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
110
	[ -n "$DISTRNAME" ] || fatal "Missing DISTRNAME in set_target_pkg_env."
111 112 113 114 115
	local ver="$DISTRVERSION"
	[ -n "$ver" ] && ver="/$ver"
	PKGFORMAT=$($DISTRVENDOR -p "$DISTRNAME$ver")
	PKGVENDOR=$($DISTRVENDOR -s "$DISTRNAME$ver")
	RPMVENDOR=$($DISTRVENDOR -n "$DISTRNAME$ver")
Vitaly Lipatov's avatar
Vitaly Lipatov committed
116 117 118
}

# Print command line and run command line
119
showcmd()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
120
{
121 122 123
	if [ -z "$quiet" ] ; then
		set_boldcolor $GREEN
		local PROMTSIG="\$"
124
		is_root && PROMTSIG="#"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
125
		echo " $PROMTSIG $*"
126
		restore_color
127
	fi >&2
128 129
}

130 131 132 133 134 135 136 137 138 139
# Print command
echocmd()
{
	set_boldcolor $GREEN
	local PROMTSIG="\$"
	is_root && PROMTSIG="#"
	echo -n "$PROMTSIG $*"
	restore_color
}

140 141 142
# Print command line and run command line
docmd()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
143
	showcmd "$*$EXTRA_SHOWDOCMD"
144
	"$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
145 146
}

147
# Run every arg with docmd
148 149
docmd_foreach()
{
150
	local cmd pkg
151 152 153 154
	cmd="$1"
	#showcmd "$@"
	shift
	for pkg in "$@" ; do
155
		docmd $cmd $pkg
156 157 158
	done
}

159 160 161 162
# run command line with SUDO
sudorun()
{
	set_sudo
Vitaly Lipatov's avatar
Vitaly Lipatov committed
163
	if [ -z "$SUDO" ] ; then
164
		"$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
165 166 167
		return
	fi
	$SUDO "$@"
168 169
}

170
# Print command line and run command line with SUDO
171
sudocmd()
172
{
173
	set_sudo
174
	[ -n "$SUDO" ] && showcmd "$SUDO $*" || showcmd "$*"
175
	sudorun "$@"
176 177
}

178
# Run every arg with sudocmd
179
# Returns on any error
180 181
sudocmd_foreach()
{
182
	local cmd pkg
183 184 185 186
	cmd="$1"
	#showcmd "$@"
	shift
	for pkg in "$@" ; do
187 188
		# don't quote $cmd here: it can be a command with an args
		sudocmd $cmd $pkg || return
189 190 191
	done
}

192 193 194 195 196 197 198 199 200 201 202
# print full path to files
make_filepath()
{
	local i
	for i in "$@" ; do
		[ -f "$i" ] || continue
		echo "$i" | grep -q "/" && echo "$i" && continue
		echo "./$i"
	done
}

203 204
get_firstarg()
{
205
	echon "$1"
206 207 208 209 210
}

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

215 216 217 218 219 220
# TODO: see etersoft-build-utils/tests/test_isnumber.sh
isnumber()
{
	echo "$*" | filter_strip_spaces | grep -q "^[0-9]\+$"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
221 222
# copied from strings
# CHECKME: the same like estrlist has ?
Vitaly Lipatov's avatar
Vitaly Lipatov committed
223
# Note: used grep -E! write '[0-9]+(first|two)', not '[0-9]\+...'
Vitaly Lipatov's avatar
Vitaly Lipatov committed
224 225 226 227 228 229 230 231 232 233 234 235
rhas()
{
	echo "$1" | grep -E -q -- "$2"
}

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

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

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

249 250 251 252 253 254 255 256 257 258

# https://superuser.com/questions/422459/substitution-in-text-file-without-regular-expressions
# http://stackoverflow.com/a/2705678/120999
# use for subst complex string with symbols treating as regexp
sed_escape()
{
	echo "$*" | sed -e 's/[]()$*.^|[]/\\&/g'
}


259 260 261 262 263 264
# param true false
subst_option()
{
	eval "[ -n \"\$$1\" ]" && echo "$2" || echo "$3"
}

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

279 280 281 282 283 284
showcmd_store_output()
{
    showcmd "$@"
    store_output "$@"
}

285 286
clean_store_output()
{
287
    rm -f $RC_STDOUT $RC_STDOUT.pipestatus
288 289
}

290
# run epm, possible from side repo
291 292
epm()
{
293
	if [ -n "$PROGNAME" ] ; then
294 295 296 297

		local bashopt=''
		[ -n "$verbose" ] && bashopt='-x'

298
		$CMDSHELL $bashopt $PROGDIR/$PROGNAME --inscript "$@"
299 300 301
	else
		epm_main --inscript "$@"
	fi
302 303 304 305 306
}

# run $SUDO epm, possible from side repo
sudoepm()
{
307
	[ -n "$PROGNAME" ] || fatal "Can't use sudo epm call from the piped script"
308 309 310 311

	local bashopt=''
	[ -n "$verbose" ] && bashopt='-x'

312
	sudorun $CMDSHELL $bashopt $PROGDIR/$PROGNAME --inscript "$@"
313
}
Vitaly Lipatov's avatar
Vitaly Lipatov committed
314 315 316 317 318

# Print error message and stop the program
fatal()
{
	if [ -z "$TEXTDOMAIN" ] ; then
319
		echo "Error: $*  (you can discuss the problem in Telegram: https://t.me/useepm)" >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
320 321
#	else
#		echog "Error in $0: $@" >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
322
	fi
323
#	[ "$TERM" = "screen" ] && echo "(screen detected: waiting ten seconds to exit ...)" >&2 && sleep 10
Vitaly Lipatov's avatar
Vitaly Lipatov committed
324 325
	exit 1
}
326

Vitaly Lipatov's avatar
Vitaly Lipatov committed
327 328 329 330
# Print warning message
warning()
{
	if [ -z "$TEXTDOMAIN" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
331
		echo "Warning: $*" >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
332 333 334 335 336
#	else
#		echog "Error in $0: $@" >&2
	fi
}

337 338 339 340
info()
{
	[ -n "$quiet" ] && return

341 342 343
	# print message to stderr if stderr forwarded to (a file)
	if isatty2 ; then
		isatty || return 0
Vitaly Lipatov's avatar
Vitaly Lipatov committed
344
		echo "$*"
345
	else
Vitaly Lipatov's avatar
Vitaly Lipatov committed
346
		echo "$*" >&2
347
	fi
348 349
}

350 351 352
# if we have not sudo, returns 1 and set SUDO variable to fatal
SUDO_TESTED=''
SUDO_CMD='sudo'
353 354
set_sudo()
{
355 356 357 358 359 360
	local nofail="$1"

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

361 362 363
	SUDO=""
	# skip SUDO if disabled
	[ -n "$EPMNOSUDO" ] && return
364 365 366 367
	if [ "$DISTRNAME" = "Cygwin" ] || [ "$DISTRNAME" = "Windows" ] ; then
		# skip sudo using on Windows
		return
	fi
368

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

372 373 374
	# start error section
	SUDO_TESTED="1"

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

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

394
	SUDO_TESTED="0"
395 396 397
	# FIXME: does not work: sudo -- VARIABLE=some command
	SUDO="$SUDO_CMD"
	#SUDO="$SUDO_CMD --"
398
	# check for < 1.7 version which do not support -- (and --help possible too)
399
	#$SUDO_CMD -h 2>/dev/null | grep -q "  --" || SUDO="$SUDO_CMD"
400

401 402
}

403 404 405 406 407 408
# return TRUE if we can run privileged command
sudo_allowed()
{
	set_sudo nofail
}

409 410
# wait for n seconds (if possible) during executing command
# args: seconds command
411 412
withtimeout()
{
413
	local TO=$(print_command_path timeout || print_command_path gtimeout)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
414
	if [ -x "$TO" ] ; then
415
		$TO "$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
416 417
		return
	fi
418
	fatal "Possible indefinite wait due timeout command is missed"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
419
	# fallback: drop time arg and run without timeout
420 421
	#shift
	#"$@"
422 423
}

424 425
set_eatmydata()
{
426 427
	# don't use eatmydata (useless)
	return 0
428 429 430
	# skip if disabled
	[ -n "$EPMNOEATMYDATA" ] && return
	# use if possible
431
	is_command eatmydata || return
432
	set_sudo
433
	# FIXME: check if SUDO already has eatmydata
434
	[ -n "$SUDO" ] && SUDO="$SUDO eatmydata" || SUDO="eatmydata"
435
	[ -n "$verbose" ] && info "Uwaga! eatmydata is installed, we will use it for disable all sync operations."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
436
	return 0
437 438
}

439 440
# 
__get_package_for_command()
441
{
442
	case "$1" in
443
		equery|revdep-rebuild)
444
			echo 'gentoolkit'
445
			;;
446
		update-kernel|remove-old-kernels)
447
			echo 'update-kernel'
448 449 450 451
			;;
	esac
}

452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
# TODO:
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
}

467 468 469 470 471 472 473 474 475 476 477

confirm_info()
{
	info "$*"
	if [ -z "$non_interactive" ] ; then
		confirm "Are you sure? [y/N]" || fatal "Exiting"
	fi

}


478 479 480 481 482 483
is_root()
{
	local EFFUID="$(id -u)"
	[ "$EFFUID" = "0" ]
}

484 485
assure_root()
{
486
	is_root || fatal "run me only under root"
487 488 489 490 491 492 493 494 495
}

regexp_subst()
{
	local expression="$1"
	shift
	sed -i -r -e "$expression" "$@"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
496
# TODO: we we can't use epm directly?
497 498 499
assure_exists()
{
	load_helper epm-assure
Vitaly Lipatov's avatar
Vitaly Lipatov committed
500
	local package="$2"
501
	local textpackage=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
502
	[ -n "$package" ] || package="$(__get_package_for_command "$1")"
503
	[ -n "$3" ] && textpackage=" >= $3"
504
	( direct='' epm_assure "$1" $package $3 ) || fatal "Can't assure in '$1' command from $package$textpackage package"
505 506
}

507 508 509 510 511 512 513
assure_exists_erc()
{
	load_helper epm-assure
	local package="erc"
	( direct='' epm_assure "$package" ) || epm ei erc || fatal "erc is not available to install."
}

514
# will replaced within disabled_eget in packaged version
Vitaly Lipatov's avatar
Vitaly Lipatov committed
515
eget()
516
{
517
	local EGET
518 519 520 521 522
	# use internal eget only if exists
	if [ -s $SHAREDIR/tools_eget ] ; then
		$SHAREDIR/tools_eget "$@"
		return
	fi
523
	fatal "Internal error: missed tools_eget"
524

525
	# FIXME: we need disable output here, eget can be used for get output
526
	assure_exists eget eget 3.3 >/dev/null
527
	# run external command, not the function
528
	EGET=$(print_command_path eget) || fatal "Missed command eget from installed package eget"
529 530 531
	$EGET "$@"
}

532 533 534 535 536 537 538 539 540 541 542 543 544 545
# will replaced within disabled_erc in packaged version
erc()
{
	local ERC
	# use internal eget only if exists
	if [ -s $SHAREDIR/tools_erc ] ; then
		$SHAREDIR/tools_erc "$@"
		return
	fi
	fatal "Internal error: missed tools_erc"

	# FIXME: we need disable output here, ercat can be used for get output
	assure_exists_erc >/dev/null
	# run external command, not the function
546
	ERC=$(print_command_path erc) || fatal "Missed command erc from installed package erc"
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
	$ERC "$@"
}

# will replaced within disabled_ercat in packaged version
ercat()
{
	local ERCAT
	# use internal eget only if exists
	if [ -s $SHAREDIR/tools_ercat ] ; then
		$SHAREDIR/tools_ercat "$@"
		return
	fi
	fatal "Internal error: missed tools_ercat"

	# FIXME: we need disable output here, ercat can be used for get output
	assure_exists_erc >/dev/null
	# run external command, not the function
564
	ERCAT=$(print_command_path ercat) || fatal "Missed command ercat from installed package erc"
565 566 567
	$ERCAT "$@"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
568 569 570 571 572 573 574 575 576 577 578 579 580 581
estrlist()
{
	if [ -s $SHAREDIR/tools_estrlist ] ; then
		$SHAREDIR/tools_estrlist "$@"
		return
	fi
	fatal "missed tools_estrlist"
}

onefile_estrlist()
{
	internal_tools_estrlist "$@"
}

582 583
# will replaced within eget() in packed version
onefile_eget()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
584
{
585
	# check for both
586 587 588
	# we really need that cross here,
	is_command curl || assure_exists wget
	is_command wget || assure_exists curl
589
	internal_tools_eget "$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
590 591
}

592
# TODO: improve and drop!
593 594 595
get_package_type()
{
	local i
596 597 598 599 600 601 602 603 604
	case $1 in
		*.deb)
			echo "deb"
			return
			;;
		*.rpm)
			echo "rpm"
			return
			;;
605 606 607 608 609 610 611 612
		*.txz)
			echo "txz"
			return
			;;
		*.tbz)
			echo "tbz"
			return
			;;
613 614 615 616
		*.exe)
			echo "exe"
			return
			;;
617 618 619 620
		*.msi)
			echo "msi"
			return
			;;
621 622 623 624
		*.AppImage)
			echo "AppImage"
			return
			;;
625 626 627 628 629 630 631
		*)
			#fatal "Don't know type of $1"
			# return package name for info
			echo "$1"
			return 1
			;;
	esac
632 633 634
}


635
# print options description from HELPCMD/HELPOPT lines in the code
636
# args: section_name, [file with code]
637 638
get_help()
{
639 640 641
    if [ "$0" = "/dev/stdin" ] || [ "$0" = "sh" ] ; then
        return
    fi
642
    local F="$0"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
643 644 645
    if [ -n "$2" ] ; then
        is_dirpath "$2" && F="$2" || F="$(dirname $0)/$2"
    fi
646

647
    cat "$F" | grep -- "# $1" | while read -r n ; do
Vitaly Lipatov's avatar
Vitaly Lipatov committed
648 649 650 651 652 653
        if echo "$n" | grep -q "# $1: PART: " ; then
            echo
            echo "$n" | sed -e "s|# $1: PART: ||"
            continue
        fi
        echo "$n" | grep -q "^ *#" && continue
654 655 656
        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"
657 658 659
    done
}

660 661
# TODO: get all info by one request (too slow)
set_distro_info()
662
{
663
	# use external distro_info if internal one is missed
664
	DISTRVENDOR=$PROGDIR/distr_info
665
	[ -x $DISTRVENDOR ] || DISTRVENDOR=distro_info
666
	export DISTRVENDOR
667

668
	[ -n "$DISTRNAME" ] || DISTRNAME=$($DISTRVENDOR -d) || fatal "Can't get distro name."
669
	[ -n "$DISTRVERSION" ] || DISTRVERSION=$($DISTRVENDOR -v)
670
	if [ -z "$DISTRARCH" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
671
		DISTRARCH=$($DISTRVENDOR --distro-arch)
672
	fi
673
	DISTRCONTROL="$($DISTRVENDOR -y)"
674
	[ -n "$BASEDISTRNAME" ] || BASEDISTRNAME=$($DISTRVENDOR -s)
675 676 677 678 679 680

	# TODO: improve BIGTMPDIR conception
	# https://bugzilla.mozilla.org/show_bug.cgi?id=69938
	# https://refspecs.linuxfoundation.org/FHS_3.0/fhs/ch05s15.html
	# https://geekpeach.net/ru/%D0%BA%D0%B0%D0%BA-systemd-tmpfiles-%D0%BE%D1%87%D0%B8%D1%89%D0%B0%D0%B5%D1%82-tmp-%D0%B8%D0%BB%D0%B8-var-tmp-%D0%B7%D0%B0%D0%BC%D0%B5%D0%BD%D0%B0-tmpwatch-%D0%B2-centos-rhel-7
	[ -n "$BIGTMPDIR" ] || [ -d "/var/tmp" ] && BIGTMPDIR="/var/tmp" || BIGTMPDIR="/tmp"
681 682 683 684 685 686 687
}

# FIXME: detect if not recognized
set_pm_type()
{
	local CMD
	set_distro_info
688 689
	set_target_pkg_env

690 691 692 693 694 695
# override package manager detection result
if [ -n "$FORCEPM" ] ; then
	PMTYPE=$FORCEPM
	return
fi

696
	PMTYPE="$($DISTRVENDOR -g $DISTRNAME/$DISTRVERSION)"
697
}
698

699 700
is_active_systemd()
{
701
	[ "$DISTRCONTROL" = "systemd" ]
702
}
703 704 705 706 707 708 709

assure_distr()
{
	local TEXT="this option"
	[ -n "$2" ] && TEXT="$2"
	[ "$DISTRNAME" = "$1" ] || fatal "$TEXT supported only for $1 distro"
}
710 711 712 713 714

# return delimiter sign in depend of package type
get_pkg_name_delimiter()
{
   local pkgtype="$1"
715
   [ -n "$pkgtype" ] || pkgtype="$PKGFORMAT"
716 717 718 719

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

721 722 723 724 725 726 727 728 729 730

# don't remove <arg> on exit
__epm_remove_from_tmp_files()
{
   keep="$1"
   [ -r "$keep" ] || return 0
   [ -n "$to_remove_pkg_files" ] || return 0
   to_remove_pkg_files="$(echo "$to_remove_pkg_files" | sed -e "s|$keep||")"
}

731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
# usage: trap "__epm_remove_tmp_files" EXIT
__epm_remove_tmp_files()
{
    # TODO: move it to exit handler
    if [ -z "$DEBUG" ] ; then
        # TODO: reinvent
        [ -n "$to_remove_pkg_files" ] && rm -f $to_remove_pkg_files
        # hack??
        [ -n "$to_remove_pkg_files" ] && rmdir $(dirname $to_remove_pkg_files | head -n1) 2>/dev/null
        [ -n "$to_remove_pkg_dirs" ] && rmdir $to_remove_pkg_dirs 2>/dev/null
        [ -n "$to_clean_tmp_dirs" ] && rm -rf $to_clean_tmp_dirs 2>/dev/null
    fi
    return 0
}


747 748
has_space()
{
749
    estrlist -- has_space "$@"
750
}
751

Vitaly Lipatov's avatar
Vitaly Lipatov committed
752 753
is_url()
{
754
    echo "$1" | grep -q "^[filehtps]*:/"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
755 756
}

757
# print a path to the command if exists in $PATH
758
if a= which which 2>/dev/null >/dev/null ; then
759 760 761 762
    # 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()
{
763
    a= which -- "$1" 2>/dev/null
764
}
765
elif a= type -a type 2>/dev/null >/dev/null ; then
766 767
print_command_path()
{
768
    a= type -fpP -- "$1" 2>/dev/null
769 770 771 772
}
else
print_command_path()
{
773
    a= type "$1" 2>/dev/null | sed -e 's|.* /|/|'
774 775 776 777 778 779 780 781 782
}
fi

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

783
# compatibility layer
784

785
# add realpath if missed
786
if ! is_command realpath ; then
787 788 789 790 791 792 793
realpath()
{
    [ -n "$*" ] || return
    readlink -f "$@"
}
fi

794

795 796 797 798 799
# TODO: use perl if sed -i is not accessible
# sed -i is only supported in GNU sed.
#  sed -i "s/$find/$replace/g" "$@"
#  perl -p -i -e "s/$find/$replace/g" "$@"

800
# add subst if missed
801
if ! is_command subst ; then
802 803 804 805 806
subst()
{
    sed -i -e "$@"
}
fi
807 808 809 810


check_core_commands()
{
811
	#which which >/dev/null || fatal "Can't find which command (which or debianutils package is missed?)"
812 813 814 815
	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?)"
}