epm-sh-functions 9.49 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
# FIXME on Android: FIX ME! implement ttyname_r() bionic/libc/bionic/stubs.c:366
23 24 25
inputisatty()
{
	# check stdin
Vitaly Lipatov's avatar
Vitaly Lipatov committed
26
	tty -s 2>/dev/null
27 28
}

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

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

41 42
check_tty()
{
43 44 45 46 47 48
	isatty2 || return

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

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

55 56 57 58
	which tput >/dev/null 2>/dev/null || return
	# FreeBSD does not support tput -S
	echo | tput -S >/dev/null 2>/dev/null || return
	[ -z "$USETTY" ] || return
59 60 61
	export USETTY=1
}

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

set_boldcolor()
{
66
	[ "$USETTY" = "1" ] || return
67 68 69 70 71 72 73 74
	{
		echo bold
		echo setaf $1
	} |tput -S
}

restore_color()
{
75
	[ "$USETTY" = "1" ] || return
76 77 78 79 80 81
	{
		echo op; # set Original color Pair.
		echo sgr0; # turn off all special graphics mode (bold in our case).
	} |tput -S
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
82 83
echover()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
84
    [ -z "$verbose" ] && return
85
    echo "$*" >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
86 87
}

88 89 90
# echo string without EOL
echon()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
91
	# default /bin/sh on MacOS does not recognize -n
Vitaly Lipatov's avatar
Vitaly Lipatov committed
92
	/bin/echo -n "$*"
93 94 95
}


Vitaly Lipatov's avatar
Vitaly Lipatov committed
96 97 98
# Used DISTRNAME
set_target_pkg_env()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
99
	[ -n "$DISTRNAME" ] || fatal "Missing DISTRNAME in set_target_pkg_env."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
100 101 102 103 104 105
	PKGFORMAT=$($DISTRVENDOR -p "$DISTRNAME")
	PKGVENDOR=$($DISTRVENDOR -s "$DISTRNAME")
	RPMVENDOR=$($DISTRVENDOR -n "$DISTRNAME")
}

# Print command line and run command line
106
showcmd()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
107
{
108 109 110
	if [ -z "$quiet" ] ; then
		set_boldcolor $GREEN
		local PROMTSIG="\$"
111
		[ "$EFFUID" = 0 ] && PROMTSIG="#"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
112
		echo " $PROMTSIG $*"
113
		restore_color
114
	fi >&2
115 116 117 118 119
}

# Print command line and run command line
docmd()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
120
	showcmd "$*$EXTRA_SHOWDOCMD"
121 122
#FIXME
	$@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
123 124
}

125
# Run every arg with docmd
126 127
docmd_foreach()
{
128
	local cmd pkg
129 130 131 132
	cmd="$1"
	#showcmd "$@"
	shift
	for pkg in "$@" ; do
133
		docmd "$cmd" $pkg
134 135 136
	done
}

137
# Print command line and run command line with SUDO
138
sudocmd()
139
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
140
	showcmd "$SUDO $*"
141
	$SUDO $@
142 143
}

144
# Run every arg with sudocmd
145
# Returns on any error
146 147
sudocmd_foreach()
{
148
	local cmd pkg
149 150 151 152
	cmd="$1"
	#showcmd "$@"
	shift
	for pkg in "$@" ; do
153
		sudocmd "$cmd" $pkg || return
154 155 156
	done
}

157 158 159 160 161 162 163 164
# add realpath if missed
if ! which realpath 2>/dev/null >/dev/null ; then
realpath()
{
	readlink -f "$@"
}
fi

165 166
get_firstarg()
{
167
	echon "$1"
168 169 170 171 172
}

get_lastarg()
{
	local lastarg
Vitaly Lipatov's avatar
Vitaly Lipatov committed
173
	eval "lastarg=\${$#}"
174
	echon "$lastarg"
175 176
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
177 178 179 180 181 182 183 184 185 186 187 188 189
filter_strip_spaces()
{
        # possible use just
        #xargs echo
        sed -e "s| \+| |g" | \
                sed -e "s|^ ||" | sed -e "s| \$||"
}

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

190 191 192 193 194 195
# param true false
subst_option()
{
	eval "[ -n \"\$$1\" ]" && echo "$2" || echo "$3"
}

196 197 198 199
store_output()
{
    # use make_temp_file from etersoft-build-utils
    RC_STDOUT=$(mktemp)
200 201
    local CMDSTATUS=$RC_STDOUT.pipestatus
    echo 1 >$CMDSTATUS
202
    #RC_STDERR=$(mktemp)
203
    ( $@ 2>&1 ; echo $? >$CMDSTATUS ) | tee $RC_STDOUT
Vitaly Lipatov's avatar
Vitaly Lipatov committed
204
    return "$(cat $CMDSTATUS)"
205
    # bashism
206
    # http://tldp.org/LDP/abs/html/bashver3.html#PIPEFAILREF
207
    #return $PIPESTATUS
208 209
}

210 211 212 213 214 215
showcmd_store_output()
{
    showcmd "$@"
    store_output "$@"
}

216 217
clean_store_output()
{
218
    rm -f $RC_STDOUT $RC_STDOUT.pipestatus
219 220
}

221
# run epm, possible from side repo
222 223
epm()
{
224 225
	[ -n "$PROGNAME" ] || fatal "Can't use epm call from the piped script"
	$PROGDIR/$PROGNAME $@
226
}
Vitaly Lipatov's avatar
Vitaly Lipatov committed
227 228 229 230 231

# Print error message and stop the program
fatal()
{
	if [ -z "$TEXTDOMAIN" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
232
		echo "Error: $*" >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
233 234
#	else
#		echog "Error in $0: $@" >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
235 236 237
	fi
	exit 1
}
238

Vitaly Lipatov's avatar
Vitaly Lipatov committed
239 240 241 242
# Print warning message
warning()
{
	if [ -z "$TEXTDOMAIN" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
243
		echo "Warning: $*" >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
244 245 246 247 248
#	else
#		echog "Error in $0: $@" >&2
	fi
}

249 250 251 252
info()
{
	[ -n "$quiet" ] && return

253 254 255
	# print message to stderr if stderr forwarded to (a file)
	if isatty2 ; then
		isatty || return 0
Vitaly Lipatov's avatar
Vitaly Lipatov committed
256
		echo "$*"
257
	else
Vitaly Lipatov's avatar
Vitaly Lipatov committed
258
		echo "$*" >&2
259
	fi
260 261
}

262 263
set_sudo()
{
264 265 266
	SUDO=""
	# skip SUDO if disabled
	[ -n "$EPMNOSUDO" ] && return
267 268 269 270
	if [ "$DISTRNAME" = "Cygwin" ] || [ "$DISTRNAME" = "Windows" ] ; then
		# skip sudo using on Windows
		return
	fi
271

Vitaly Lipatov's avatar
Vitaly Lipatov committed
272
	EFFUID=$(id -u)
273 274

	# do not need sudo
275
	[ $EFFUID = "0" ] && return
276 277

	# use sudo if possible
278 279
	if which sudo >/dev/null 2>/dev/null ; then
		SUDO="sudo --"
280
		# check for < 1.7 version which do not support -- (and --help possible too)
281
		sudo -h 2>/dev/null | grep -q "  --" || SUDO="sudo"
282 283
		return
	fi
284 285

	SUDO="fatal 'Can't find sudo. Please install sudo or run epm under root.'"
286 287
}

288 289
# wait for n seconds (if possible) during executing command
# args: seconds command
290 291 292
withtimeout()
{
	local TO=$(which timeout 2>/dev/null || which gtimeout 2>/dev/null)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
293 294 295 296 297
	if [ -x "$TO" ] ; then
		$TO $@
		return
	fi
	# fallback: drop time arg and run without timeout
298 299
	shift
	$@
300 301
}

302 303 304 305 306 307 308
set_eatmydata()
{
	# skip if disabled
	[ -n "$EPMNOEATMYDATA" ] && return
	# use if possible
	which eatmydata >/dev/null 2>/dev/null || return
	SUDO="$SUDO eatmydata"
309
	[ -n "$verbose" ] && info "Uwaga! eatmydata is installed, we will use it for disable all sync operations."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
310
	return 0
311 312
}

313 314
# 
__get_package_for_command()
315
{
316
	case "$1" in
317
		equery|revdep-rebuild)
318
			echo 'gentoolkit'
319
			;;
320
		update-kernel|remove-old-kernels)
321
			echo 'update-kernel'
322 323 324 325
			;;
	esac
}

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
# 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
}

assure_root()
{
	[ "$EFFUID" = 0 ] || fatal "run me only under root"
}

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

Vitaly Lipatov's avatar
Vitaly Lipatov committed
353
# TODO: we we can't use epm directly?
354 355 356
assure_exists()
{
	load_helper epm-assure
Vitaly Lipatov's avatar
Vitaly Lipatov committed
357
	local package="$2"
358
	local textpackage=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
359
	[ -n "$package" ] || package="$(__get_package_for_command "$1")"
360 361
	[ -n "$3" ] && textpackage=" >= $3"
	__epm_assure "$1" $package $3 || fatal "Can't assure in '$1' command from $package$textpackage package"
362 363
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
364 365
eget()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
366 367
	assure_exists wget
	$SHAREDIR/tools_eget "$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
368 369
}

370
# TODO: improve and drop!
371 372 373
get_package_type()
{
	local i
374 375 376 377 378 379 380 381 382
	case $1 in
		*.deb)
			echo "deb"
			return
			;;
		*.rpm)
			echo "rpm"
			return
			;;
383 384 385 386 387 388 389 390
		*.txz)
			echo "txz"
			return
			;;
		*.tbz)
			echo "tbz"
			return
			;;
391 392 393 394
		*.exe)
			echo "exe"
			return
			;;
395 396 397 398
		*.msi)
			echo "msi"
			return
			;;
399 400 401 402 403 404 405
		*)
			#fatal "Don't know type of $1"
			# return package name for info
			echo "$1"
			return 1
			;;
	esac
406 407 408
}


409 410 411
# print options description from HELPCMD/HELPOPT lines in the code
get_help()
{
412 413 414 415
    if [ "$0" = "/dev/stdin" ] || [ "$0" = "sh" ] ; then
        return
    fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
416
    grep -v -- "^#" $0 | grep -- "# $1" | while read -r n ; do
417 418 419 420 421 422
        opt=$(echo $n | sed -e "s|) # $1:.*||g")
        desc=$(echo $n | sed -e "s|.*) # $1:||g")
        printf "    %-20s %s\n" $opt "$desc"
    done
}

423

424 425 426 427
# FIXME: detect if not recognized
set_pm_type()
{
	local CMD
428 429 430

	# Fill for use: PMTYPE, DISTRNAME, DISTRVERSION, PKGFORMAT, PKGVENDOR, RPMVENDOR
	DISTRVENDOR=$PROGDIR/distr_info
431
	[ -n "$DISTRNAME" ] || DISTRNAME=$($DISTRVENDOR -d) || fatal "Can't get distro name."
432 433 434
	[ -n "$DISTRVERSION" ] || DISTRVERSION=$($DISTRVENDOR -v)
	set_target_pkg_env

435 436 437 438 439 440
# override package manager detection result
if [ -n "$FORCEPM" ] ; then
	PMTYPE=$FORCEPM
	return
fi

441 442
# TODO: move it in distr_vendor?
# FIXME: some problems with multibased distros (Server Edition on CentOS and Desktop Edition on Ubuntu)
443
case $DISTRNAME in
444
	ALTLinux)
445
		CMD="apt-rpm"
446
		#which ds-install 2>/dev/null >/dev/null && CMD=deepsolver-rpm
447 448
		;;
	PCLinux)
449 450
		CMD="apt-rpm"
		;;
451
	Ubuntu|Debian|Mint|AstraLinux|Elbrus)
452
		CMD="apt-dpkg"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
453
		#which aptitude 2>/dev/null >/dev/null && CMD=aptitude-dpkg
454
		which snappy 2>/dev/null >/dev/null && CMD=snappy
455
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
456
	Mandriva|ROSA)
457 458
		CMD="urpm-rpm"
		;;
459 460
	FreeBSD|NetBSD|OpenBSD|Solaris)
		CMD="pkgsrc"
461
		which pkg 2>/dev/null >/dev/null && CMD=pkgng
462
		;;
463 464 465
	Gentoo)
		CMD="emerge"
		;;
466 467 468
	ArchLinux)
		CMD="pacman"
		;;
469
	Fedora|LinuxXP|ASPLinux|CentOS|RHEL|Scientific|GosLinux)
470
		CMD="yum-rpm"
471
		which dnf 2>/dev/null >/dev/null && test -d /var/lib/dnf/yumdb && CMD=dnf-rpm
472
		;;
473
	Slackware)
474
		CMD="slackpkg"
475
		;;
476
	SUSE|SLED|SLES|Tumbleweed)
477 478
		CMD="zypper-rpm"
		;;
479 480 481
	ForesightLinux|rPathLinux)
		CMD="conary"
		;;
482
	Windows)
483
		CMD="chocolatey"
484
		;;
485 486 487 488 489 490
	MacOS)
		CMD="homebrew"
		;;
	OpenWRT)
		CMD="ipkg"
		;;
491 492 493
	GNU/Linux/Guix)
		CMD="guix"
		;;
494 495 496
	Android)
		CMD="android"
		;;
497 498 499
	Cygwin)
		CMD="aptcyg"
		;;
500 501 502
	alpine)
		CMD="apk"
		;;
503 504 505
	TinyCoreLinux)
		CMD="tce"
		;;
506 507 508
	VoidLinux)
		CMD="xbps"
		;;
509
	*)
510
		fatal "Have no suitable DISTRNAME $DISTRNAME"
511 512 513 514
		;;
esac
PMTYPE=$CMD
}
515

516 517 518 519 520 521 522 523

is_active_systemd()
{
	local a
	SYSTEMCTL=/bin/systemctl
	SYSTEMD_CGROUP_DIR=/sys/fs/cgroup/systemd
	[ -x "$SYSTEMCTL" ] || return
	[ -d "$SYSTEMD_CGROUP_DIR" ] || return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
524
	a='' mountpoint -q "$SYSTEMD_CGROUP_DIR" || return
525
	readlink /sbin/init | grep -q 'systemd' || return
526
	# some hack
Vitaly Lipatov's avatar
Vitaly Lipatov committed
527
	# shellcheck disable=SC2009
528
	ps ax | grep '[s]ystemd' | grep -q -v 'systemd-udev'
529
}