epm-sh-functions 12.1 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
26 27
	#tty -s 2>/dev/null
	test -t 0
28 29
}

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

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

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

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

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

56 57 58 59
	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
60 61 62
	export USETTY=1
}

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

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

restore_color()
{
76
	[ "$USETTY" = "1" ] || return
77 78 79 80 81 82
	{
		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
83 84
echover()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
85
    [ -z "$verbose" ] && return
86
    echo "$*" >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
87 88
}

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


Vitaly Lipatov's avatar
Vitaly Lipatov committed
97 98 99
# Used DISTRNAME
set_target_pkg_env()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
100
	[ -n "$DISTRNAME" ] || fatal "Missing DISTRNAME in set_target_pkg_env."
101 102 103 104 105
	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
106 107 108
}

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

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

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

140 141 142 143
# run command line with SUDO
sudorun()
{
	set_sudo
Vitaly Lipatov's avatar
Vitaly Lipatov committed
144 145 146 147 148
	if [ -z "$SUDO" ] ; then
		"$@"
		return
	fi
	$SUDO "$@"
149 150
}

151
# Print command line and run command line with SUDO
152
sudocmd()
153
{
154
	set_sudo
155
	[ -n "$SUDO" ] && showcmd "$SUDO $*" || showcmd "$*"
156
	sudorun "$@"
157 158
}

159
# Run every arg with sudocmd
160
# Returns on any error
161 162
sudocmd_foreach()
{
163
	local cmd pkg
164 165 166 167
	cmd="$1"
	#showcmd "$@"
	shift
	for pkg in "$@" ; do
168 169
		# don't quote $cmd here: it can be a command with an args
		sudocmd $cmd $pkg || return
170 171 172
	done
}

173 174 175 176 177 178 179 180
# add realpath if missed
if ! which realpath 2>/dev/null >/dev/null ; then
realpath()
{
	readlink -f "$@"
}
fi

181 182
get_firstarg()
{
183
	echon "$1"
184 185 186 187 188
}

get_lastarg()
{
	local lastarg
Vitaly Lipatov's avatar
Vitaly Lipatov committed
189
	eval "lastarg=\${$#}"
190
	echon "$lastarg"
191 192
}

193 194 195 196 197 198
# 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
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
# copied from strings
# CHECKME: the same like estrlist has ?
# Note: used egrep! write '[0-9]+(first|two)', not '[0-9]\+...'
rhas()
{
	echo "$1" | grep -E -q -- "$2"
}

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

Vitaly Lipatov's avatar
Vitaly Lipatov committed
214 215 216 217 218 219 220 221 222 223 224 225 226
filter_strip_spaces()
{
        # possible use just
        #xargs echo
        sed -e "s| \+| |g" | \
                sed -e "s|^ ||" | sed -e "s| \$||"
}

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

227 228 229 230 231 232
# param true false
subst_option()
{
	eval "[ -n \"\$$1\" ]" && echo "$2" || echo "$3"
}

233 234 235 236
store_output()
{
    # use make_temp_file from etersoft-build-utils
    RC_STDOUT=$(mktemp)
237 238
    local CMDSTATUS=$RC_STDOUT.pipestatus
    echo 1 >$CMDSTATUS
239
    #RC_STDERR=$(mktemp)
240
    ( LANG=C $@ 2>&1 ; echo $? >$CMDSTATUS ) | tee $RC_STDOUT
Vitaly Lipatov's avatar
Vitaly Lipatov committed
241
    return "$(cat $CMDSTATUS)"
242
    # bashism
243
    # http://tldp.org/LDP/abs/html/bashver3.html#PIPEFAILREF
244
    #return $PIPESTATUS
245 246
}

247 248 249 250 251 252
showcmd_store_output()
{
    showcmd "$@"
    store_output "$@"
}

253 254
clean_store_output()
{
255
    rm -f $RC_STDOUT $RC_STDOUT.pipestatus
256 257
}

258
# run epm, possible from side repo
259 260
epm()
{
261
	[ -n "$PROGNAME" ] || fatal "Can't use epm call from the piped script"
262 263 264 265 266 267 268 269
	$PROGDIR/$PROGNAME --inscript "$@"
}

# run $SUDO epm, possible from side repo
sudoepm()
{
	[ -n "$PROGNAME" ] || fatal "Can't use epm call from the piped script"
	sudorun $PROGDIR/$PROGNAME --inscript "$@"
270
}
Vitaly Lipatov's avatar
Vitaly Lipatov committed
271 272 273 274 275

# Print error message and stop the program
fatal()
{
	if [ -z "$TEXTDOMAIN" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
276
		echo "Error: $*" >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
277 278
#	else
#		echog "Error in $0: $@" >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
279
	fi
280
#	[ "$TERM" = "screen" ] && echo "(screen detected: waiting ten seconds to exit ...)" >&2 && sleep 10
Vitaly Lipatov's avatar
Vitaly Lipatov committed
281 282
	exit 1
}
283

Vitaly Lipatov's avatar
Vitaly Lipatov committed
284 285 286 287
# Print warning message
warning()
{
	if [ -z "$TEXTDOMAIN" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
288
		echo "Warning: $*" >&2
Vitaly Lipatov's avatar
Vitaly Lipatov committed
289 290 291 292 293
#	else
#		echog "Error in $0: $@" >&2
	fi
}

294 295 296 297
info()
{
	[ -n "$quiet" ] && return

298 299 300
	# print message to stderr if stderr forwarded to (a file)
	if isatty2 ; then
		isatty || return 0
Vitaly Lipatov's avatar
Vitaly Lipatov committed
301
		echo "$*"
302
	else
Vitaly Lipatov's avatar
Vitaly Lipatov committed
303
		echo "$*" >&2
304
	fi
305 306
}

307 308 309
# if we have not sudo, returns 1 and set SUDO variable to fatal
SUDO_TESTED=''
SUDO_CMD='sudo'
310 311
set_sudo()
{
312 313 314 315 316 317
	local nofail="$1"

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

318 319 320
	SUDO=""
	# skip SUDO if disabled
	[ -n "$EPMNOSUDO" ] && return
321 322 323 324
	if [ "$DISTRNAME" = "Cygwin" ] || [ "$DISTRNAME" = "Windows" ] ; then
		# skip sudo using on Windows
		return
	fi
325

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

329 330 331
	# start error section
	SUDO_TESTED="1"

332
	if ! which $SUDO_CMD >/dev/null 2>/dev/null ; then
333 334
		[ "$nofail" = "nofail" ] || SUDO="fatal 'Can't find sudo. Please install and tune sudo ('# epm install sudo') or run epm under root.'"
		return "$SUDO_TESTED"
335
	fi
336

337 338
	# if input is a console
	if inputisatty && isatty && isatty2 ; then
339
		if ! $SUDO_CMD -l >/dev/null ; then
340 341
			[ "$nofail" = "nofail" ] || SUDO="fatal 'Can't use sudo (only without password sudo is supported in non interactive using). Please run epm under root.'"
			return "$SUDO_TESTED"
342 343 344
		fi
	else
		# use sudo if one is tuned and tuned without password
345
		if ! $SUDO_CMD -l -n >/dev/null 2>/dev/null ; then
346 347
			[ "$nofail" = "nofail" ] || SUDO="fatal 'Can't use sudo (only without password sudo is supported). Please run epm under root.'"
			return "$SUDO_TESTED"
348
		fi
349 350
	fi

351
	SUDO_TESTED="0"
352
	SUDO="$SUDO_CMD --"
353
	# check for < 1.7 version which do not support -- (and --help possible too)
354
	$SUDO_CMD -h 2>/dev/null | grep -q "  --" || SUDO="$SUDO_CMD"
355

356 357
}

358 359
# wait for n seconds (if possible) during executing command
# args: seconds command
360 361 362
withtimeout()
{
	local TO=$(which timeout 2>/dev/null || which gtimeout 2>/dev/null)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
363
	if [ -x "$TO" ] ; then
364
		$TO "$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
365 366 367
		return
	fi
	# fallback: drop time arg and run without timeout
368
	shift
369
	"$@"
370 371
}

372 373 374 375 376 377
set_eatmydata()
{
	# skip if disabled
	[ -n "$EPMNOEATMYDATA" ] && return
	# use if possible
	which eatmydata >/dev/null 2>/dev/null || return
378
	set_sudo
379
	[ -n "$SUDO" ] && SUDO="$SUDO eatmydata" || SUDO="eatmydata"
380
	[ -n "$verbose" ] && info "Uwaga! eatmydata is installed, we will use it for disable all sync operations."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
381
	return 0
382 383
}

384 385
# 
__get_package_for_command()
386
{
387
	case "$1" in
388
		equery|revdep-rebuild)
389
			echo 'gentoolkit'
390
			;;
391
		update-kernel|remove-old-kernels)
392
			echo 'update-kernel'
393 394 395 396
			;;
	esac
}

397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
# 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
}

412 413 414 415 416 417 418 419 420 421 422

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

}


423 424 425 426 427 428
is_root()
{
	local EFFUID="$(id -u)"
	[ "$EFFUID" = "0" ]
}

429 430
assure_root()
{
431
	is_root || fatal "run me only under root"
432 433 434 435 436 437 438 439 440
}

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

Vitaly Lipatov's avatar
Vitaly Lipatov committed
441
# TODO: we we can't use epm directly?
442 443 444
assure_exists()
{
	load_helper epm-assure
Vitaly Lipatov's avatar
Vitaly Lipatov committed
445
	local package="$2"
446
	local textpackage=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
447
	[ -n "$package" ] || package="$(__get_package_for_command "$1")"
448
	[ -n "$3" ] && textpackage=" >= $3"
449
	( direct='' epm_assure "$1" $package $3 ) || fatal "Can't assure in '$1' command from $package$textpackage package"
450 451
}

452 453 454 455 456 457 458
__set_EGET()
{
	# use internal eget only if exists
	if [ -s $SHAREDIR/tools_eget ] ; then
		export EGET="$SHAREDIR/tools_eget"
		return
	fi
459
	fatal "Internal error: missed tools_eget"
460 461

	# FIXME: we need disable output here, eget can be used for get output
462
	assure_exists eget eget 3.3 >/dev/null
463 464 465 466
	# use external command, not the function
	export EGET="$(which eget)" || fatal "Missed command eget from installed package eget"
}

467
# will replaced within disabled_eget in packaged version
Vitaly Lipatov's avatar
Vitaly Lipatov committed
468
eget()
469
{
470
	local EGET
471 472 473 474 475
	# use internal eget only if exists
	if [ -s $SHAREDIR/tools_eget ] ; then
		$SHAREDIR/tools_eget "$@"
		return
	fi
476
	fatal "Internal error: missed tools_eget"
477

478
	# FIXME: we need disable output here, eget can be used for get output
479
	assure_exists eget eget 3.3 >/dev/null
480 481 482 483 484
	# run external command, not the function
	EGET=$(which eget) || fatal "Missed command eget from installed package eget"
	$EGET "$@"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
485 486 487 488 489 490 491 492 493 494 495 496 497 498
estrlist()
{
	if [ -s $SHAREDIR/tools_estrlist ] ; then
		$SHAREDIR/tools_estrlist "$@"
		return
	fi
	fatal "missed tools_estrlist"
}

onefile_estrlist()
{
	internal_tools_estrlist "$@"
}

499 500
# will replaced within eget() in packed version
onefile_eget()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
501
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
502
	assure_exists wget
503
	internal_tools_eget "$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
504 505
}

506
# TODO: improve and drop!
507 508 509
get_package_type()
{
	local i
510 511 512 513 514 515 516 517 518
	case $1 in
		*.deb)
			echo "deb"
			return
			;;
		*.rpm)
			echo "rpm"
			return
			;;
519 520 521 522 523 524 525 526
		*.txz)
			echo "txz"
			return
			;;
		*.tbz)
			echo "tbz"
			return
			;;
527 528 529 530
		*.exe)
			echo "exe"
			return
			;;
531 532 533 534
		*.msi)
			echo "msi"
			return
			;;
535 536 537 538 539 540 541
		*)
			#fatal "Don't know type of $1"
			# return package name for info
			echo "$1"
			return 1
			;;
	esac
542 543 544
}


545
# print options description from HELPCMD/HELPOPT lines in the code
546
# args: section_name, [file with code]
547 548
get_help()
{
549 550 551
    if [ "$0" = "/dev/stdin" ] || [ "$0" = "sh" ] ; then
        return
    fi
552
    local F="$0"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
553 554 555
    if [ -n "$2" ] ; then
        is_dirpath "$2" && F="$2" || F="$(dirname $0)/$2"
    fi
556

557
    cat "$F" | grep -- "# $1" | while read -r n ; do
Vitaly Lipatov's avatar
Vitaly Lipatov committed
558 559 560 561 562 563
        if echo "$n" | grep -q "# $1: PART: " ; then
            echo
            echo "$n" | sed -e "s|# $1: PART: ||"
            continue
        fi
        echo "$n" | grep -q "^ *#" && continue
564 565 566
        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"
567 568 569
    done
}

570 571
# TODO: get all info by one request (too slow)
set_distro_info()
572
{
573
	# use external distro_info if internal one is missed
574
	DISTRVENDOR=$PROGDIR/distr_info
575
	[ -x $DISTRVENDOR ] || DISTRVENDOR=distro_info
576
	export DISTRVENDOR
577

578
	[ -n "$DISTRNAME" ] || DISTRNAME=$($DISTRVENDOR -d) || fatal "Can't get distro name."
579
	[ -n "$DISTRVERSION" ] || DISTRVERSION=$($DISTRVENDOR -v)
580
	if [ -z "$DISTRARCH" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
581
		DISTRARCH=$($DISTRVENDOR --distro-arch)
582
	fi
583
	DISTRCONTROL="$($DISTRVENDOR -y)"
584 585 586 587 588 589 590
}

# FIXME: detect if not recognized
set_pm_type()
{
	local CMD
	set_distro_info
591 592
	set_target_pkg_env

593 594 595 596 597 598
# override package manager detection result
if [ -n "$FORCEPM" ] ; then
	PMTYPE=$FORCEPM
	return
fi

599
	PMTYPE="$($DISTRVENDOR -g $DISTRNAME/$DISTRVERSION)"
600
}
601

602 603
is_active_systemd()
{
604
	[ "$DISTRCONTROL" = "systemd" ]
605
}
606 607 608 609 610 611 612

assure_distr()
{
	local TEXT="this option"
	[ -n "$2" ] && TEXT="$2"
	[ "$DISTRNAME" = "$1" ] || fatal "$TEXT supported only for $1 distro"
}
613 614 615 616 617 618 619 620 621 622

# return delimiter sign in depend of package type
get_pkg_name_delimiter()
{
   local pkgtype="$1"
   [ -n "$pkgtype" ] || pkgtype="$($DISTRVENDOR -p)"

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

has_space()
{
626
    estrlist -- has_space "$@"
627
}