epm.sh 111 KB
Newer Older
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1 2
#!/bin/sh
#
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3 4
# Copyright (C) 2012-2014  Etersoft
# Copyright (C) 2012-2014  Vitaly Lipatov <lav@etersoft.ru>
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

PROGDIR=$(dirname $0)
21 22
[ "$PROGDIR" = "." ] && PROGDIR=$(pwd)

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

load_helper()
{
    local CMD="$SHAREDIR/$1"
    # do not use fatal() here, it can be initial state
    [ -r "$CMD" ] || { echo "FATAL: Have no $CMD helper file" ; exit 1; }
    . $CMD
}



# File bin/epm-sh-functions:

Vitaly Lipatov's avatar
Vitaly Lipatov committed
38 39 40 41

inputisatty()
{
	# check stdin
Vitaly Lipatov's avatar
Vitaly Lipatov committed
42
	tty -s 2>/dev/null
Vitaly Lipatov's avatar
Vitaly Lipatov committed
43 44
}

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

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

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

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

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

Vitaly Lipatov's avatar
Vitaly Lipatov committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
	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
	export USETTY=1
}

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

set_boldcolor()
{
	[ "$USETTY" = "1" ] || return
	{
		echo bold
		echo setaf $1
	} |tput -S
}

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

echover()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
100
    [ -z "$verbose" ] && return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
101 102 103
    echo "$*" >&2
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
104 105 106 107 108 109 110
echon()
{
	# default /bin/sh on MacOS does not recognize -n
	/bin/echo -n "$@"
}


Vitaly Lipatov's avatar
Vitaly Lipatov committed
111 112
set_target_pkg_env()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
113
	[ -n "$DISTRNAME" ] || fatal "Missing DISTRNAME in set_target_pkg_env."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
114 115 116 117 118 119 120 121 122 123
	PKGFORMAT=$($DISTRVENDOR -p "$DISTRNAME")
	PKGVENDOR=$($DISTRVENDOR -s "$DISTRNAME")
	RPMVENDOR=$($DISTRVENDOR -n "$DISTRNAME")
}

showcmd()
{
	if [ -z "$quiet" ] ; then
		set_boldcolor $GREEN
		local PROMTSIG="\$"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
124
		[ "$EFFUID" = 0 ] && PROMTSIG="#"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
125 126 127 128 129 130 131
		echo " $PROMTSIG $@"
		restore_color
	fi >&2
}

docmd()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
132
	showcmd "$@$EXTRA_SHOWDOCMD"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
133
	$@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
134 135 136 137
}

docmd_foreach()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
138
	local cmd pkg
Vitaly Lipatov's avatar
Vitaly Lipatov committed
139 140 141 142
	cmd="$1"
	#showcmd "$@"
	shift
	for pkg in "$@" ; do
Vitaly Lipatov's avatar
Vitaly Lipatov committed
143
		docmd "$cmd" $pkg
Vitaly Lipatov's avatar
Vitaly Lipatov committed
144 145 146 147 148 149
	done
}

sudocmd()
{
	showcmd "$SUDO $@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
150
	$SUDO $@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
151 152
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
153 154
sudocmd_foreach()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
155
	local cmd pkg
Vitaly Lipatov's avatar
Vitaly Lipatov committed
156 157 158 159
	cmd="$1"
	#showcmd "$@"
	shift
	for pkg in "$@" ; do
Vitaly Lipatov's avatar
Vitaly Lipatov committed
160
		sudocmd "$cmd" $pkg || return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
161 162 163
	done
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
164 165
get_firstarg()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
166
	echon "$1"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
167 168 169 170 171 172
}

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

Vitaly Lipatov's avatar
Vitaly Lipatov committed
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
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
190 191 192 193 194 195 196 197 198
subst_option()
{
	eval "[ -n \"\$$1\" ]" && echo "$2" || echo "$3"
}

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

clean_store_output()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
211
    rm -f $RC_STDOUT $RC_STDOUT.pipestatus
Vitaly Lipatov's avatar
Vitaly Lipatov committed
212 213
}

214 215 216 217
epm()
{
	$PROGDIR/epm $@
}
Vitaly Lipatov's avatar
Vitaly Lipatov committed
218 219 220 221 222 223 224 225 226

fatal()
{
	if [ -z "$TEXTDOMAIN" ] ; then
		echo "Error: $@" >&2
	fi
	exit 1
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
227 228 229 230 231 232 233
warning()
{
	if [ -z "$TEXTDOMAIN" ] ; then
		echo "Warning: $@" >&2
	fi
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
234 235 236 237 238 239 240 241 242 243 244 245 246
info()
{
	[ -n "$quiet" ] && return

	# 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
247 248 249 250 251
set_sudo()
{
	SUDO=""
	# skip SUDO if disabled
	[ -n "$EPMNOSUDO" ] && return
252 253 254 255
	if [ "$DISTRNAME" = "Cygwin" ] || [ "$DISTRNAME" = "Windows" ] ; then
		# skip sudo using on Windows
		return
	fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
256

Vitaly Lipatov's avatar
Vitaly Lipatov committed
257
	EFFUID=`id -u`
Vitaly Lipatov's avatar
Vitaly Lipatov committed
258 259

	# do not need sudo
Vitaly Lipatov's avatar
Vitaly Lipatov committed
260
	[ $EFFUID = "0" ] && return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
261 262

	# use sudo if possible
Vitaly Lipatov's avatar
Vitaly Lipatov committed
263
	which sudo >/dev/null 2>/dev/null && SUDO="sudo --" && return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
264 265 266 267

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

Vitaly Lipatov's avatar
Vitaly Lipatov committed
268 269 270
withtimeout()
{
	local TO=$(which timeout 2>/dev/null || which gtimeout 2>/dev/null)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
271 272 273 274 275
	if [ -x "$TO" ] ; then
		$TO $@
		return
	fi
	# fallback: drop time arg and run without timeout
Vitaly Lipatov's avatar
Vitaly Lipatov committed
276 277
	shift
	$@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
278 279
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
280 281 282 283 284 285 286
set_eatmydata()
{
	# skip if disabled
	[ -n "$EPMNOEATMYDATA" ] && return
	# use if possible
	which eatmydata >/dev/null 2>/dev/null || return
	SUDO="$SUDO eatmydata"
287
	[ -n "$verbose" ] && info "Uwaga! eatmydata is installed, we will use it for disable all sync operations."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
288
	return 0
Vitaly Lipatov's avatar
Vitaly Lipatov committed
289 290
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
291
__get_package_for_command()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
292
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
293
	case "$1" in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
294
		equery|revdep-rebuild)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
295
			echo 'gentoolkit'
Vitaly Lipatov's avatar
Vitaly Lipatov committed
296 297
			;;
		update-kernel|remove-old-kernels)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
298
			echo 'update-kernel'
Vitaly Lipatov's avatar
Vitaly Lipatov committed
299 300 301 302
			;;
	esac
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
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
329 330 331 332
assure_exists()
{
	load_helper epm-assure
	local package="$2"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
333
	local textpackage=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
334
	[ -n "$package" ] || package="$(__get_package_for_command "$1")"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
335 336
	[ -n "$3" ] && textpackage=" >= $3"
	__epm_assure "$1" $package $3 || fatal "Can't assure in '$1' command from $package$textpackage package"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
337 338 339 340
}

eget()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
341
	$SHAREDIR/tools-eget "$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
342 343
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
344 345 346
get_package_type()
{
	local i
Vitaly Lipatov's avatar
Vitaly Lipatov committed
347 348 349 350 351 352 353 354 355
	case $1 in
		*.deb)
			echo "deb"
			return
			;;
		*.rpm)
			echo "rpm"
			return
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
356 357 358 359 360 361 362 363
		*.txz)
			echo "txz"
			return
			;;
		*.tbz)
			echo "tbz"
			return
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
364 365 366 367 368 369 370 371
		*.exe)
			echo "exe"
			return
			;;
		*.msi)
			echo "msi"
			return
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
372 373 374 375 376 377 378
		*)
			#fatal "Don't know type of $1"
			# return package name for info
			echo "$1"
			return 1
			;;
	esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
379 380 381
}


Vitaly Lipatov's avatar
Vitaly Lipatov committed
382 383
get_help()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
384
    grep -v -- "^#" $0 | grep -- "# $1" | while read n ; do
Vitaly Lipatov's avatar
Vitaly Lipatov committed
385 386 387 388 389 390 391 392 393 394 395 396 397
        opt=$(echo $n | sed -e "s|) # $1:.*||g")
        desc=$(echo $n | sed -e "s|.*) # $1:||g")
        printf "    %-20s %s\n" $opt "$desc"
    done
}


set_pm_type()
{
	local CMD

	# Fill for use: PMTYPE, DISTRNAME, DISTRVERSION, PKGFORMAT, PKGVENDOR, RPMVENDOR
	DISTRVENDOR=internal_distr_info
Vitaly Lipatov's avatar
Vitaly Lipatov committed
398
	[ -n "$DISTRNAME" ] || DISTRNAME=$($DISTRVENDOR -d) || fatal "Can't get distro name."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
399 400 401 402 403 404 405 406 407
	[ -n "$DISTRVERSION" ] || DISTRVERSION=$($DISTRVENDOR -v)
	set_target_pkg_env

if [ -n "$FORCEPM" ] ; then
	PMTYPE=$FORCEPM
	return
fi

case $DISTRNAME in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
408
	ALTLinux)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
409
		CMD="apt-rpm"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
410
		#which ds-install 2>/dev/null >/dev/null && CMD=deepsolver-rpm
Vitaly Lipatov's avatar
Vitaly Lipatov committed
411 412 413 414 415 416
		;;
	PCLinux)
		CMD="apt-rpm"
		;;
	Ubuntu|Debian|Mint)
		CMD="apt-dpkg"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
417
		#which aptitude 2>/dev/null >/dev/null && CMD=aptitude-dpkg
Vitaly Lipatov's avatar
Vitaly Lipatov committed
418
		which snappy 2>/dev/null >/dev/null && CMD=snappy
Vitaly Lipatov's avatar
Vitaly Lipatov committed
419 420 421 422 423 424
		;;
	Mandriva|ROSA)
		CMD="urpm-rpm"
		;;
	FreeBSD|NetBSD|OpenBSD|Solaris)
		CMD="pkgsrc"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
425
		which pkg 2>/dev/null >/dev/null && CMD=pkgng
Vitaly Lipatov's avatar
Vitaly Lipatov committed
426 427 428 429 430 431 432 433 434
		;;
	Gentoo)
		CMD="emerge"
		;;
	ArchLinux)
		CMD="pacman"
		;;
	Fedora|LinuxXP|ASPLinux|CentOS|RHEL|Scientific)
		CMD="yum-rpm"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
435
		which dnf 2>/dev/null >/dev/null && test -d /var/lib/dnf/yumdb && CMD=dnf-rpm
Vitaly Lipatov's avatar
Vitaly Lipatov committed
436 437 438 439 440 441 442
		;;
	Slackware)
		CMD="slackpkg"
		;;
	SUSE|SLED|SLES)
		CMD="zypper-rpm"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
443 444 445
	ForesightLinux|rPathLinux)
		CMD="conary"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
446 447 448
	Windows)
		CMD="chocolatey"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
449 450 451 452 453 454
	MacOS)
		CMD="homebrew"
		;;
	OpenWRT)
		CMD="ipkg"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
455 456 457
	GNU/Linux/Guix)
		CMD="guix"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
458 459 460
	Android)
		CMD="android"
		;;
461 462 463
	Cygwin)
		CMD="aptcyg"
		;;
464 465 466
	alpine)
		CMD="apk"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
467
	*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
468
		fatal "Have no suitable DISTRNAME $DISTRNAME"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
469 470 471 472 473 474
		;;
esac
PMTYPE=$CMD
}


Vitaly Lipatov's avatar
Vitaly Lipatov committed
475 476 477 478 479 480 481 482 483 484 485 486
is_active_systemd()
{
	local a
	SYSTEMCTL=/bin/systemctl
	SYSTEMD_CGROUP_DIR=/sys/fs/cgroup/systemd
	[ -x "$SYSTEMCTL" ] || return
	[ -d "$SYSTEMD_CGROUP_DIR" ] || return
	a= mountpoint -q "$SYSTEMD_CGROUP_DIR" || return
	# some hack
	pidof systemd >/dev/null
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
487 488 489 490 491 492
# File bin/epm-addrepo:

epm_addrepo()
{
case $PMTYPE in
	apt-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
493
		assure_exists apt-repo
Vitaly Lipatov's avatar
Vitaly Lipatov committed
494
		sudocmd apt-repo add "$pkg_filenames"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
495
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
496
	apt-dpkg|aptitude-dpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
497
		info "You need manually add repo to /etc/apt/sources.list"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
498 499
		;;
	yum-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
500 501
		assure_exists yum-utils
		sudocmd yum-config-manager --add-repo "$pkg_filenames"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
502 503
		;;
	urpm-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
504
		sudocmd urpmi.addmedia "$pkg_filenames"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
505 506
		;;
	zypper-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
507
		sudocmd zypper ar "$pkg_filenames"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
508 509
		;;
	emerge)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
510
		sudocmd layman -a $"pkg_filenames"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
511 512
		;;
	pacman)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
513
		info "You need manually add repo to /etc/pacman.conf"
514 515
		# Only for alone packages:
		#sudocmd repo-add $pkg_filenames
Vitaly Lipatov's avatar
Vitaly Lipatov committed
516 517
		;;
	npackd)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
518
		sudocmd npackdcl add-repo --url="$pkg_filenames"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
519 520
		;;
	slackpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
521
		info "You need manually add repo to /etc/slackpkg/mirrors"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
522 523
		;;
	*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
524
		fatal "Have no suitable command for $PMTYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
525 526 527 528 529
		;;
esac

}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
530 531 532 533 534 535 536 537
# File bin/epm-assure:

__check_command_in_path()
{
    PATH=$PATH:/sbin:/usr/sbin which "$1" 2>/dev/null
}


Vitaly Lipatov's avatar
Vitaly Lipatov committed
538 539 540 541 542 543 544 545 546 547 548
rhas()
{
	echo "$1" | egrep -q -- "$2"
}

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

Vitaly Lipatov's avatar
Vitaly Lipatov committed
549 550 551 552
__epm_need_update()
{
    local PACKAGE="$1"
    local PACKAGEVERSION="$2"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
553

Vitaly Lipatov's avatar
Vitaly Lipatov committed
554
    [ -n "$PACKAGEVERSION" ] || return 0
Vitaly Lipatov's avatar
Vitaly Lipatov committed
555

Vitaly Lipatov's avatar
Vitaly Lipatov committed
556 557 558 559 560 561 562 563 564 565 566
    is_installed "$PACKAGE" || return 0

    # epm print version for package N
    local INSTALLEDVERSION=$(query_package_field "version" "$PACKAGE")
    # if needed >= installed, return 0
    [ "$(compare_version "$PACKAGEVERSION" "$INSTALLEDVERSION")" -gt 0 ] && return 0

    return 1
}

__epm_assure_checking()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
567
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
568 569 570 571 572
    local CMD="$1"
    local PACKAGE="$2"
    local PACKAGEVERSION="$3"

    [ -n "$PACKAGEVERSION" ] && return 1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
573

Vitaly Lipatov's avatar
Vitaly Lipatov committed
574 575
    if is_dirpath "$CMD" ; then
        if [ -e "$CMD" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
576
            if [ -n "$verbose" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
577 578
                info "File or directory $CMD is already exists."
                epm qf "$CMD"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
579
            fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
580
            return 0
Vitaly Lipatov's avatar
Vitaly Lipatov committed
581 582
        fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
583 584
        [ -n "$PACKAGE" ] || fatal "You need run with package name param when use with absolute path"
        return 0
Vitaly Lipatov's avatar
Vitaly Lipatov committed
585 586
    fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
587
    if __check_command_in_path "$CMD" >/dev/null ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
588 589
        if [ -n "$verbose" ] ; then
            local compath="$(__check_command_in_path "$1")"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
590
            info "Command $CMD is exists: $compath"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
591 592
            epm qf "$compath"
        fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
593
        return 0
Vitaly Lipatov's avatar
Vitaly Lipatov committed
594 595
    fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
596 597 598 599
    return 1
}


Vitaly Lipatov's avatar
Vitaly Lipatov committed
600

Vitaly Lipatov's avatar
Vitaly Lipatov committed
601 602 603
__epm_assure()
{
    local CMD="$1"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
604
    local PACKAGE="$2"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
605
    local PACKAGEVERSION="$3"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
606 607
    [ -n "$PACKAGE" ] || PACKAGE="$1"

Vitaly Lipatov's avatar
Vitaly Lipatov committed
608 609 610 611
    __epm_assure_checking $CMD $PACKAGE $PACKAGEVERSION && return 0

    info "Installing appropriate package for $CMD command..."
    __epm_need_update $PACKAGE $PACKAGEVERSION || return 0
Vitaly Lipatov's avatar
Vitaly Lipatov committed
612

Vitaly Lipatov's avatar
Vitaly Lipatov committed
613 614 615 616 617 618
    docmd epm --auto install $PACKAGE || return

    [ -n "$PACKAGEVERSION" ] || return 0
    # check if we couldn't update and still need update
    __epm_need_update $PACKAGE $PACKAGEVERSION && return 1
    return 0
Vitaly Lipatov's avatar
Vitaly Lipatov committed
619 620 621 622 623
}


epm_assure()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
624
    [ -n "$pkg_filenames" ] || fatal "Assure: Missing params. Check $0 --help for info."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
625 626

    # use helper func for extract separate params
Vitaly Lipatov's avatar
Vitaly Lipatov committed
627
    __epm_assure $(eval echo $quoted_args)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
628 629
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
630 631 632 633
# File bin/epm-audit:

epm_audit()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
634 635 636

[ -z "$pkg_filenames" ] || fatal "No arguments are allowed here"

Vitaly Lipatov's avatar
Vitaly Lipatov committed
637 638 639 640 641 642 643 644 645 646 647
case $PMTYPE in
	pkgng)
		sudocmd pkg audit -F
		;;
	*)
		fatal "Have no suitable command for $PMTYPE"
		;;
esac

}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
648 649 650 651
# File bin/epm-autoorphans:

epm_autoorphans()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
652 653 654 655

[ -z "$pkg_filenames" ] || fatal "No arguments are allowed here"


Vitaly Lipatov's avatar
Vitaly Lipatov committed
656 657 658 659 660 661 662 663 664 665 666 667
case $PMTYPE in
	#apt-rpm)
		# ALT Linux only
		#__epm_autoremove_altrpm

		# ALT Linux only
		#assure_exists remove-old-kernels
		#sudocmd remove-old-kernels
	#	;;
	apt-dpkg|aptitude-dpkg)
		assure_exists deborphan
		showcmd deborphan
Vitaly Lipatov's avatar
Vitaly Lipatov committed
668
		a= deborphan | sudocmd epm remove
Vitaly Lipatov's avatar
Vitaly Lipatov committed
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
		;;
	#aura)
	#	sudocmd aura -Oj
	#	;;
	yum-rpm)
		showcmd package-cleanup --orphans
		local PKGLIST=$(package-cleanup --orphans)
		sudocmd epm remove $PKGLIST
		;;
	urpm-rpm)
		showcmd urpmq --auto-orphans
		sudocmd urpme --auto-orphans
		;;
	#emerge)
	#	sudocmd emerge --depclean
	#	assure_exists revdep-rebuild
	#	sudocmd revdep-rebuild
	#	;;
	pacman)
		sudocmd pacman -Qdtq | sudocmd pacman -Rs -
		;;
	#slackpkg)
		# clean-system removes non official packages
		#sudocmd slackpkg clean-system
	#	;;
	#guix)
	#	sudocmd guix gc
	#	;;
	#pkgng)
	#	sudocmd pkg autoremove
	#	;;
	#zypper-rpm)
	#	sudocmd zypper clean
	#	;;
	*)
		fatal "Have no suitable command for $PMTYPE"
		;;
esac

}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
710 711
# File bin/epm-autoremove:

Vitaly Lipatov's avatar
Vitaly Lipatov committed
712 713 714 715 716
__epm_autoremove_altrpm()
{
	local pkg
	local flag=
	load_helper epm-packages
Vitaly Lipatov's avatar
Vitaly Lipatov committed
717 718
	info
	info "Just removing all non -devel libs packages not need by anything"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
719
	for pkg in $(short=1 pkg_filenames= epm_packages | grep -- "^lib" | grep -v -- "-devel$" | grep -v -- "-debuginfo$" | grep -v -- ^libreoffice | grep -v -- libnss- ) ; do
Vitaly Lipatov's avatar
Vitaly Lipatov committed
720 721 722 723 724 725 726 727 728 729
		sudocmd rpm -v -e $pkg && flag=1
	done

	# call again for next cycle until all libs will removed
	[ -n "$flag" ] && __epm_autoremove_altrpm

	return 0
}


Vitaly Lipatov's avatar
Vitaly Lipatov committed
730 731
epm_autoremove()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
732 733 734 735

[ -z "$pkg_filenames" ] || fatal "No arguments are allowed here"


Vitaly Lipatov's avatar
Vitaly Lipatov committed
736
case $PMTYPE in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
737 738
	apt-rpm)
		# ALT Linux only
Vitaly Lipatov's avatar
Vitaly Lipatov committed
739
		__epm_autoremove_altrpm
Vitaly Lipatov's avatar
Vitaly Lipatov committed
740

Vitaly Lipatov's avatar
Vitaly Lipatov committed
741
		# ALT Linux only
Vitaly Lipatov's avatar
Vitaly Lipatov committed
742
		assure_exists remove-old-kernels
Vitaly Lipatov's avatar
Vitaly Lipatov committed
743 744
		sudocmd remove-old-kernels
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
745
	apt-dpkg|aptitude-dpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
746 747
		sudocmd apt-get autoremove
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
748 749 750 751 752 753
	aura)
		sudocmd aura -Oj
		;;
	yum-rpm)
		# cleanup orphanes?
		while true ; do
Vitaly Lipatov's avatar
Vitaly Lipatov committed
754
			docmd package-cleanup --leaves $(subst_option non_interactive --assumeyes)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
755 756 757 758 759 760
			# FIXME: package-cleanup have to use stderr for errors
			local PKGLIST=$(package-cleanup --leaves | grep -v "Loaded plugins" | grep -v "Unable to")
			[ -n "$PKGLIST" ] || break
			sudocmd yum remove $PKGLIST
		done
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
761 762 763
	dnf-rpm)
		sudocmd dnf autoremove
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
764 765 766 767
	# see autoorhans
	#urpm-rpm)
	#	sudocmd urpme --auto-orphans
	#	;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
768 769
	emerge)
		sudocmd emerge --depclean
Vitaly Lipatov's avatar
Vitaly Lipatov committed
770
		assure_exists revdep-rebuild
Vitaly Lipatov's avatar
Vitaly Lipatov committed
771 772
		sudocmd revdep-rebuild
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
773 774 775 776
	# see autoorhans
	#pacman)
	#	sudocmd pacman -Qdtq | sudocmd pacman -Rs -
	#	;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
777 778 779 780
	slackpkg)
		# clean-system removes non official packages
		#sudocmd slackpkg clean-system
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
781 782 783
	guix)
		sudocmd guix gc
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
784 785 786
	pkgng)
		sudocmd pkg autoremove
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
787 788 789 790
	#zypper-rpm)
	#	sudocmd zypper clean
	#	;;
	*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
791
		fatal "Have no suitable command for $PMTYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
792 793 794 795 796 797 798 799
		;;
esac

}

# File bin/epm-changelog:


Vitaly Lipatov's avatar
Vitaly Lipatov committed
800 801 802 803 804 805 806 807
__epm_changelog_apt()
{
	local i
	for i in $@ ; do
		docmd apt-cache show $i | grep -A 1000 "^Changelog:"
	done
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
808 809 810 811
__epm_changelog_files()
{
	[ -z "$*" ] && return

Vitaly Lipatov's avatar
Vitaly Lipatov committed
812 813 814
	# TODO: detect every file
	case $(get_package_type $1) in
		rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
815
			assure_exists rpm
816
			docmd_foreach "rpm -q -p --changelog" $@ | less
Vitaly Lipatov's avatar
Vitaly Lipatov committed
817 818
			;;
		*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
819
			fatal "Have no suitable command for $1"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
820 821 822 823 824 825 826 827 828
			;;
	esac
}

__epm_changelog_local_names()
{
	[ -z "$*" ] && return

	case $PMTYPE in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
829
		apt-rpm|yum-rpm|dnf-rpm|urpm-rpm|zypper-rpm)
830
			docmd_foreach "rpm -q --changelog" $@ | less
Vitaly Lipatov's avatar
Vitaly Lipatov committed
831
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
832
		apt-dpkg|aptitude-dpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
833 834 835
			docmd zcat /usr/share/doc/$1/changelog.Debian.gz | less
			;;
		emerge)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
836 837
			assure_exists equery
			docmd equery changes -f $1 | less
Vitaly Lipatov's avatar
Vitaly Lipatov committed
838 839
			;;
		pacman)
840
			docmd pacman -Qc $1 | less
Vitaly Lipatov's avatar
Vitaly Lipatov committed
841 842
			;;
		*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
843
			fatal "Have no suitable command for $PMTYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
844 845 846 847 848 849 850 851 852
			;;
	esac
}

__epm_changelog_unlocal_names()
{
	[ -z "$*" ] && return

	case $PMTYPE in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
853 854 855
		apt-rpm)
			__epm_changelog_apt $@ | less
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
856 857 858 859 860 861 862 863 864 865 866 867 868
		#apt-dpkg)
		#	# FIXME: only first pkg
		#	docmd zcat /usr/share/doc/$1/changelog.Debian.gz | less
		#	;;
		#yum-rpm)
		#	sudocmd yum clean all
		#	;;
		#urpm-rpm)
		#	sudocmd urpmi --clean
		#	;;
		#zypper-rpm)
		#	sudocmd zypper clean
		#	;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
869 870 871 872
		emerge)
			assure_exists equery
			docmd equery changes -f $1 | less
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
873
		*)
874
			fatal "Have no suitable command for $PMTYPE. Try install the package firstly."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
875 876 877 878 879 880 881 882
			;;
	esac

}


epm_changelog()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
883
	[ -n "$pkg_filenames" ] || fatal "Changelog: Missing package(s) name"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904

	__epm_changelog_files $pkg_files

	local pkg
	for pkg in $pkg_names ; do
		if is_installed $pkg ; then
			__epm_changelog_local_names $pkg
		else
			__epm_changelog_unlocal_names $pkg
		fi
	done
}

# File bin/epm-check:

epm_check()
{
case $PMTYPE in
	apt-rpm|apt-dpkg)
		#sudocmd apt-get check || exit
		#sudocmd apt-get update || exit
Vitaly Lipatov's avatar
Vitaly Lipatov committed
905
		sudocmd apt-get -f install
Vitaly Lipatov's avatar
Vitaly Lipatov committed
906 907 908 909 910 911 912
		;;
	apt-dpkg)
		#sudocmd apt-get update || exit
		#sudocmd apt-get check || exit
		sudocmd apt-get -f install || exit
		sudocmd apt-get autoremove
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
913 914 915 916
	aptitude-dpkg)
		sudocmd aptitude -f install || exit
		#sudocmd apt-get autoremove
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
917 918 919 920 921 922 923 924 925
	yum-rpm)
		docmd yum check
		docmd package-cleanup --problems

		#docmd package-cleanup --dupes
		sudocmd package-cleanup --cleandupes

		docmd rpm -Va --nofiles --nodigest
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
926
	emerge)
927 928
		sudocmd revdep-rebuild
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
929 930 931
	#urpm-rpm)
	#	sudocmd urpme --auto-orphans
	#	;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
932
	zypper-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
933 934 935 936
		sudocmd zypper verify
		;;
	conary)
		sudocmd conary verify
Vitaly Lipatov's avatar
Vitaly Lipatov committed
937
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
938 939 940
	pkgng)
		sudocmd pkg check -d -a
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
941 942 943
	homebrew)
		sudocmd brew doctor
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
944
	*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
945
		fatal "Have no suitable command for $PMTYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
946 947 948 949 950 951 952 953 954 955 956
		;;
esac

}

# File bin/epm-checkpkg:

check_pkg_integrity()
{
	local PKG="$1"
	local RET
Vitaly Lipatov's avatar
Vitaly Lipatov committed
957

Vitaly Lipatov's avatar
Vitaly Lipatov committed
958
	case $(get_package_type $PKG) in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
959
	rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
960
		assure_exists rpm
Vitaly Lipatov's avatar
Vitaly Lipatov committed
961
		docmd rpm --checksig $PKG
Vitaly Lipatov's avatar
Vitaly Lipatov committed
962 963
		;;
	deb)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
964
		assure_exists dpkg
Vitaly Lipatov's avatar
Vitaly Lipatov committed
965
		# FIXME: debsums -ca package ?
Vitaly Lipatov's avatar
Vitaly Lipatov committed
966
		docmd dpkg --contents $PKG >/dev/null && echo "Package $PKG is correct."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
967
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
968
	exe)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
969 970 971 972 973 974
		file $PKG | grep -q "executable for MS Windows"
		;;
	msi)
		# TODO: add to patool via cabextract
		assure_exists cabextract
		#file $PKG | grep -q "Microsoft Office Document"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
975
		docmd cabextract -t $PKG
Vitaly Lipatov's avatar
Vitaly Lipatov committed
976 977 978
		;;
	ebuild)
		true
Vitaly Lipatov's avatar
Vitaly Lipatov committed
979
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
980
	*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
981
		assure_exists erc
Vitaly Lipatov's avatar
Vitaly Lipatov committed
982
		docmd erc test "$PKG" && return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
983
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
984
	esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
985 986
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
987 988 989 990 991 992 993 994 995
__epm_check_installed_pkg()
{
case $PMTYPE in
	*-rpm)
		docmd rpm -V $@
		;;
	*-dpkg)
		docmd debsums $@
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
996 997 998 999
	emerge)
		assure_exists equery
		docmd equery check $@
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1000
	*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1001
		fatal "Have no suitable command for $PMTYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1002 1003 1004 1005 1006
		;;
esac

}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1007 1008 1009

epm_checkpkg()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1010
	if [ -n "$pkg_names" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1011
		info "Suggest $pkg_names are name(s) of installed packages"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1012 1013 1014 1015
		__epm_check_installed_pkg $pkg_names
		return
	fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1016
	[ -n "$pkg_files" ] || fatal "Checkpkg: missing file or package name(s)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1017 1018 1019 1020 1021 1022
	local pkg
	for pkg in $pkg_files ; do
		check_pkg_integrity $pkg || fatal "Broken package $pkg"
	done
}

1023 1024 1025 1026 1027 1028
# File bin/epm-checksystem:


epm_checksystem_ALTLinux()
{
	local TDIR=$(mktemp -d)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1029
	assure_exists time
1030 1031 1032
	touch $TDIR/added
	for ft in $(ls /usr/lib/rpm/*.filetrigger | sort) ; do
		echo "Try run $ft ..."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1033
		echo $TDIR/added $TDIR/removed | a= time $ft
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
	done
	rm -f $TDIR/added fatal
	rmdir $TDIR || fatal
	echo "Count lines:"
	wc -l /var/lib/rpm/files-awaiting-filetriggers
}


epm_checksystem()
{

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1045
[ $EFFUID = "0" ] && fatal "Do not use checksystem under root"
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057

case $DISTRNAME in
	ALTLinux)
		epm_checksystem_$DISTRNAME
		;;
	*)
		fatal "Have no suitable command for $PMTYPE"
		;;
esac

}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1058 1059 1060 1061 1062 1063
if [ "$1" = "--debug" ] ; then
	shift
	SUDO=sudo
	DISTRNAME=ALTLinux
	epm_checksystem
fi
1064

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1065 1066
# File bin/epm-check_updated_repo:

1067
__is_repo_info_downloaded()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1068 1069 1070
{
    case $PMTYPE in
        apt-*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1071
            if [ -r /var/cache/apt ] ; then
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
                $SUDO test -r /var/cache/apt/pkgcache.bin || return
            fi
            ;;
        *)
            ;;
    esac
    return 0
}

__is_repo_info_uptodate()
{
    case $PMTYPE in
        apt-rpm)
            # apt-deb do not update lock file date
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1086
            if $SUDO test -r /var/lib/apt/lists ; then
1087 1088 1089 1090 1091
                local LOCKFILE=/var/lib/apt/lists/lock
                $SUDO test -r $LOCKFILE || return
                # if repo older than 1 day, return false
                # find print string if file is obsoleted
                test -z "$($SUDO find $LOCKFILE -mtime +1)" || return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1092
            fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1093 1094 1095 1096 1097 1098 1099 1100 1101
            ;;
        *)
            ;;
    esac
    return 0
}

update_repo_if_needed()
{
1102 1103 1104 1105 1106 1107
    # check if we need skip update checking
    if [ "$1" = "soft" ] && [ -n "$SUDO" ] ; then
        # if sudo requires a password, skip autoupdate
        sudo -n true 2>/dev/null || { info "sudo requires a password, skip repo status checking" ; return 0 ; }
    fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1108
    cd /
1109
    if ! __is_repo_info_downloaded || ! __is_repo_info_uptodate ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1110
        pkg_filenames= epm_update
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1111
    fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1112
    cd - >/dev/null
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1113 1114 1115

}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1116 1117
# File bin/epm-clean:

1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
__remove_alt_apt_cache_file()
{
	sudocmd rm -vf /var/cache/apt/*.bin
	sudocmd rm -vf /var/cache/apt/partial/*
	sudocmd rm -vf /var/lib/apt/lists/*pkglist*
	sudocmd rm -vf /var/lib/apt/lists/*release*
}

__remove_deb_apt_cache_file()
{
	sudocmd rm -vf /var/cache/apt/*.bin
	sudocmd rm -vf /var/cache/apt/archives/partial/*
	sudocmd rm -vf /var/lib/apt/lists/*Packages*
	sudocmd rm -vf /var/lib/apt/lists/*Release*
	sudocmd rm -vf /var/lib/apt/lists/*Translation*
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1135 1136
epm_clean()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1137 1138 1139 1140

[ -z "$pkg_filenames" ] || fatal "No arguments are allowed here"


Vitaly Lipatov's avatar
Vitaly Lipatov committed
1141
case $PMTYPE in
1142 1143 1144 1145 1146
	apt-rpm)
		sudocmd apt-get clean
		__remove_alt_apt_cache_file
		;;
	apt-dpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1147
		sudocmd apt-get clean
1148
		__remove_deb_apt_cache_file
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1149
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1150 1151
	aptitude-dpkg)
		sudocmd aptitude clean
1152
		__remove_deb_apt_cache_file
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1153
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1154 1155
	yum-rpm)
		sudocmd yum clean all
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1156
		#sudocmd yum makecache
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1157 1158 1159 1160 1161 1162 1163
		;;
	dnf-rpm)
		sudocmd dnf clean all
		;;
	urpm-rpm)
		sudocmd urpmi --clean
		;;
1164 1165 1166
	pacman)
		sudocmd pacman -Sc
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1167 1168 1169
	zypper-rpm)
		sudocmd zypper clean
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1170 1171 1172 1173 1174
	nix)
		sudocmd nix-collect-garbage
		;;
	slackpkg)
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1175
	pkgng)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1176
		sudocmd pkg clean -a
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1177
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1178
	*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1179
		fatal "Have no suitable command for $PMTYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1180 1181
		;;
esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1182
	info "It is recommend to run 'epm autoremove' also"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1183 1184 1185

}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1186 1187 1188 1189 1190 1191 1192 1193 1194
# File bin/epm-conflicts:


epm_conflicts_files()
{
	[ -n "$pkg_files" ] || return

	case $(get_package_type $pkg_files) in
		rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1195
			assure_exists rpm
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1196
			docmd "rpm -q --conflicts -p" $pkg_files
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 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 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
			;;
		#deb)
		#	a= docmd dpkg -I $pkg_files | grep "^ *Depends:" | sed "s|^ *Depends:||g"
		#	;;
		*)
			fatal "Have no suitable command for $PMTYPE"
			;;
	esac
}

epm_conflicts_names()
{
	local CMD
	[ -n "$pkg_names" ] || return

case $PMTYPE in
	apt-rpm)
		# FIXME: need fix for a few names case
		# FIXME: too low level of requires name (libSOME.so)
		if is_installed $pkg_names ; then
			CMD="rpm -q --conflicts"
		else
			EXTRA_SHOWDOCMD=' | grep "Conflicts:"'
			docmd apt-cache show $pkg_names | grep "Conflicts:"
			return
		fi

		;;
	urpm-rpm|zypper-rpm)
		# FIXME: use hi level commands
		CMD="rpm -q --conflicts"
		;;
	#yum-rpm)
	#	CMD="yum deplist"
	#	;;
	#pacman)
	#	CMD="pactree"
	#	;;
	apt-dpkg)
		# FIXME: need fix for a few names case
		if is_installed $pkg_names ; then
			showcmd dpkg -s $pkg_names
			a= dpkg -s $pkg_names | grep "^Conflicts:" | sed "s|^Conflicts:||g"
			return
		else
			EXTRA_SHOWDOCMD=' | grep "Conflicts:"'
			docmd apt-cache show $pkg_names | grep "Conflicts:"
			return
		fi
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1247 1248 1249 1250 1251
	# TODO: why-not show who conflicts with us
	#aptitude-dpkg)
	#	docmd aptitude why-not $pkg_names
	#	;;

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
	#emerge)
	#	assure_exists equery
	#	CMD="equery depgraph"
	#	;;
	*)
		fatal "Have no suitable command for $PMTYPE"
		;;
esac


docmd $CMD $pkg_names

}

epm_conflicts()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1268
	[ -n "$pkg_filenames" ] || fatal "Conflicts: Missing package(s) name"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1269 1270 1271 1272
	epm_conflicts_files
	epm_conflicts_names
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1273 1274 1275
# File bin/epm-downgrade:


Vitaly Lipatov's avatar
Vitaly Lipatov committed
1276
__epm_add_alt_apt_downgrade_preferences()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1277 1278 1279 1280
{
	[ -r /etc/apt/preferences ] && fatal "/etc/apt/preferences already exists"
	cat <<EOF | $SUDO tee /etc/apt/preferences
Package: *
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1281
Pin: release c=classic
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1282
Pin-Priority: 1001
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305

Package: *
Pin: release c=addon
Pin-Priority: 1101
EOF
}

__epm_add_deb_apt_downgrade_preferences()
{
	[ -r /etc/apt/preferences ] && fatal "/etc/apt/preferences already exists"
	info "Running with /etc/apt/preferences:"
	cat <<EOF | $SUDO tee /etc/apt/preferences
Package: *
Pin: release a=stable
Pin-Priority: 1001

Package: *
Pin: release a=testing
Pin-Priority: 900

Package: *
Pin: release a=unstable
Pin-Priority: 800
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
EOF
}

__epm_remove_apt_downgrade_preferences()
{
	sudocmd rm -f /etc/apt/preferences
}

epm_downgrade()
{
	local CMD

	# it is useful for first time running
	update_repo_if_needed

	info "Running command for downgrade packages"

	case $PMTYPE in
	apt-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1325 1326 1327 1328 1329 1330
		__epm_add_alt_apt_downgrade_preferences || return
		if [ -n "$pkg_filenames" ] ; then
			sudocmd apt-get install $pkg_filenames
		else
			sudocmd apt-get dist-upgrade
		fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1331 1332 1333
		__epm_remove_apt_downgrade_preferences
		;;
	apt-dpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1334 1335 1336 1337 1338 1339
		__epm_add_deb_apt_downgrade_preferences || return
		if [ -n "$pkg_filenames" ] ; then
			sudocmd apt-get install $pkg_filenames
		else
			sudocmd apt-get dist-upgrade
		fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1340 1341 1342 1343
		__epm_remove_apt_downgrade_preferences
		;;
	yum-rpm)
		# can do update repobase automagically
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1344
		sudocmd yum downgrade $pkg_filenames
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1345
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1346
	dnf-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1347
		sudocmd dnf downgrade $pkg_filenames
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1348
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
	urpm-rpm)
		assure_exists urpm-reposync urpm-tools
		sudocmd urpm-reposync -v
		;;
	*)
		fatal "Have no suitable command for $PMTYPE"
		;;
	esac
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1359 1360 1361 1362 1363 1364 1365 1366
# File bin/epm-download:

epm_download()
{
	local CMD

	case $PMTYPE in
	dnf-rpm)
1367 1368 1369 1370
		sudocmd dnf download $pkg_filenames
		;;
	aptcyg)
		sudocmd apt-cyg download $pkg_filenames
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1371 1372 1373 1374 1375 1376 1377
		;;
	*)
		fatal "Have no suitable command for $PMTYPE"
		;;
	esac
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1378 1379 1380
# File bin/epm-filelist:


Vitaly Lipatov's avatar
Vitaly Lipatov committed
1381
__alt_local_content_filelist()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1382
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1383 1384

    local CI="$(get_local_alt_contents_index)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1385

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1386
    # TODO: safe way to use less
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1387 1388 1389 1390 1391
    #local OUTCMD="less"
    #[ -n "$USETTY" ] || OUTCMD="cat"
    OUTCMD="cat"

    {
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1392 1393
        [ -n "$USETTY" ] && echo "Search in $CI for $1..."
        grep -h -- ".*$1$" $CI | sed -e "s|\(.*\)\t\(.*\)|\1|g"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1394 1395 1396
    } | $OUTCMD
}

1397 1398 1399
__deb_local_content_filelist()
{
    showcmd "apt-file list $1 | grep '^$1: ' | sed -e 's|$1: ||g'"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1400
    a= apt-file list "$1" | grep "^$1: " | sed -e "s|$1: ||g"
1401 1402
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1403 1404 1405 1406 1407 1408 1409 1410

__epm_filelist_remote()
{
	[ -z "$*" ] && return

	case $PMTYPE in
		apt-rpm)
			# TODO: use RESTful interface to prometeus? See ALT bug #29496
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1411
			docmd_foreach __alt_local_content_filelist $@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1412
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1413
		apt-dpkg)
1414 1415 1416 1417
			assure_exists apt-file || return
			# if sudo requires a password, skip autoupdate
			sudo -n true 2>/dev/null && sudocmd apt-file update || info "sudo requires a password, skip apt-file update"
			docmd_foreach __deb_local_content_filelist $@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1418
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1419 1420 1421 1422 1423 1424
		*)
			fatal "Query filelist for non installed packages does not realized"
			;;
	esac
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1425 1426 1427 1428 1429 1430
__epm_filelist_file()
{
	local CMD

	[ -z "$*" ] && return

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1431 1432 1433
	# TODO: allow a new packages
	case $(get_package_type $1) in
		rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1434
			assure_exists rpm
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1435 1436
			CMD="rpm -qlp"
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1437
		deb)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1438
			assure_exists dpkg
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1439 1440 1441
			CMD="dpkg --contents"
			;;
		*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1442
			fatal "Have no suitable query command for $PMTYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1443 1444 1445
			;;
	esac

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1446
	docmd $CMD $@ | less
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1447 1448 1449 1450 1451 1452 1453 1454 1455
}

__epm_filelist_name()
{
	local CMD

	[ -z "$*" ] && return

	case $PMTYPE in
1456
		*-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1457 1458
			CMD="rpm -ql"
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1459
		*-dpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1460 1461
			CMD="dpkg -L"
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1462 1463 1464
		android)
			CMD="pm list packages -f"
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1465 1466 1467
		conary)
			CMD="conary query --ls"
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1468
		pacman)
1469
			docmd pacman -Ql $@ | sed -e "s|.* ||g" | less
1470
			return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1471
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1472 1473 1474 1475
		emerge)
			assure_exists equery
			CMD="equery files"
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1476 1477 1478
		pkgng)
			CMD="pkg info -l"
			;;
1479 1480 1481 1482
		aptcyg)
			docmd apt-cyg listfiles $@ | sed -e "s|^|/|g"
			return
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1483
		slackpkg)
1484
			is_installed $@ || fatal "Query filelist for non installed packages does not realized"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1485
			docmd awk 'BEGIN{desk=1}{if(/^FILE LIST:$/){desk=0} else if (desk==0) {print}}' /var/log/packages/${pkg_filenames}* | less
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1486 1487 1488
			return
			;;
		*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1489
			fatal "Have no suitable query command for $PMTYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1490 1491 1492
			;;
	esac

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1493
	# TODO: add less
1494
	docmd $CMD $@ && return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1495
	# TODO: may be we need check is installed before prev. line?
1496
	is_installed $@ || __epm_filelist_remote $@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1497 1498 1499 1500 1501
}


epm_filelist()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1502
	[ -n "$pkg_filenames" ] || fatal "Filelist: missing package(s) name"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1503 1504 1505


	__epm_filelist_file $pkg_files || return
1506
	__epm_filelist_name $(print_name $pkg_names) || return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523

}

# File bin/epm-info:


__epm_info_rpm_low()
{
	if [ -n "$pkg_files" ] ; then
		docmd rpm -qip $pkg_files
	fi
	[ -z "$pkg_names" ] && return
	is_installed $pkg_names && docmd rpm -qi $pkg_names && return
}

epm_info()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1524

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1525
[ -n "$pkg_filenames" ] || fatal "Info: missing package(s) name"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1526

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
case $PMTYPE in
	apt-rpm)
		__epm_info_rpm_low && return
		docmd apt-cache show $pkg_names
		;;
	apt-dpkg)
		if [ -n "$pkg_files" ] ; then
			docmd dpkg -I $pkg_files
		fi
		[ -z "$pkg_names" ] && return
		is_installed $pkg_names && docmd dpkg -p $pkg_names && return
		docmd apt-cache show $pkg_names
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1540 1541 1542 1543 1544 1545 1546
	aptitude-dpkg)
		if [ -n "$pkg_files" ] ; then
			docmd dpkg -I $pkg_files
		fi
		[ -z "$pkg_names" ] && return
		docmd aptitude show $pkg_names
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
	yum-rpm)
		__epm_info_rpm_low && return
		docmd yum info $pkg_names
		;;
	dnf-rpm)
		__epm_info_rpm_low && return
		docmd dnf info $pkg_names
		;;
	zypper-rpm)
		__epm_info_rpm_low && return
		docmd zypper info $pkg_names
		;;
	pacman)
1560
		is_installed $pkg_names && docmd pacman -Qi $pkg_names && return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1561 1562
		docmd pacman -Si $pkg_names
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1563 1564 1565 1566
	aura)
		is_installed $pkg_names && docmd pacman -Qi $pkg_names && return
		docmd aura -Ai $pkg_names
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1567 1568 1569 1570
	npackd)
		# FIXME: --version=
		docmd npackdcl info --package=$pkg_names
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1571 1572 1573 1574
	conary)
		is_installed $pkg_names && docmd conary query $pkg_names --info && return
		docmd conary repquery $pkg_names --info
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1575 1576 1577 1578 1579 1580 1581
	emerge)
		assure_exists equery
		docmd equery meta $pkg_names
		docmd equery which $pkg_names
		docmd equery uses $pkg_names
		docmd equery size $pkg_names
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1582 1583 1584
	slackpkg)
		docmd /usr/sbin/slackpkg info $pkg_names
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1585 1586 1587
	ipkg)
		docmd ipkg info $pkg_names
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1588 1589 1590
	pkgng)
		docmd pkg info $pkg_names
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1591 1592 1593
	homebrew)
		docmd brew info $pkg_names
		;;
1594 1595 1596
	aptcyg)
		docmd apt-cyg show $pkg_names
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1597
	*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1598
		fatal "Have no suitable command for $PMTYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1599 1600 1601 1602 1603 1604 1605
		;;
esac

}

# File bin/epm-install:

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1606

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1607 1608 1609 1610 1611 1612 1613 1614 1615
filter_out_installed_packages()
{
	[ -z "$skip_installed" ] && cat && return

	case $PKGFORMAT in
		"rpm")
			LANG=C LC_ALL=C xargs -n1 rpm -q 2>&1 | grep 'is not installed' |
				sed -e 's|^.*package \(.*\) is not installed.*|\1|g'
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1616 1617 1618 1619 1620
		# dpkg -l lists some non ii status (un, etc)
		#"deb")
		#	LANG=C LC_ALL=C xargs -n1 dpkg -l 2>&1 | grep -i 'no packages found matching' |
		#		sed -e 's|\.\+$||g' -e 's|^.*[Nn]o packages found matching \(.*\)|\1|g'
		#	;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1621
		*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1622 1623 1624
			for i in $(cat) ; do
				is_installed $i || echo $i
			done
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1625 1626 1627 1628
			;;
	esac | sed -e "s|rpm-build-altlinux-compat[^ ]*||g" | filter_strip_spaces
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1629 1630 1631 1632
__use_zypper_no_gpg_checks()
{
    a= zypper install --help 2>&1 | grep -q -- "--no-gpg-checks" && echo "--no-gpg-checks"
}
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1633

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
__separate_sudocmd_foreach()
{
    local cmd_re=$1
    local cmd_in=$2
    shift 2
    separate_installed $@
    if [ -n "$pkg_noninstalled" ] ; then
        sudocmd_foreach "$cmd_re" $pkg_noninstalled || return
    fi
    if [ -n "$pkg_installed" ] ; then
        sudocmd_foreach "$cmd_in" $pkg_installed || return
    fi
    return 0
}

__separate_sudocmd()
{
    local cmd_re=$1
    local cmd_in=$2
    shift 2
    separate_installed $@
    if [ -n "$pkg_noninstalled" ] ; then
        sudocmd "$cmd_re" $pkg_noninstalled || return
    fi
    if [ -n "$pkg_installed" ] ; then
        sudocmd "$cmd_in" $pkg_installed || return
    fi
    return 0
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1664 1665 1666 1667 1668 1669 1670 1671 1672
download_pkg_urls()
{
	local url
	[ -z "$1" ] && return
	for url in $* ; do
	    eget $url || warning "Skipped"
	done
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684
epm_install_names()
{
	if [ -n "$non_interactive" ] ; then
		epm_ni_install_names "$@"
		return
	fi

	[ -z "$1" ] && return
	case $PMTYPE in
		apt-rpm|apt-dpkg)
			sudocmd apt-get $APTOPTIONS install $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1685 1686 1687
		aptitude-dpkg)
			sudocmd aptitude install $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1688 1689 1690 1691 1692 1693 1694 1695 1696
		deepsolver-rpm)
			sudocmd ds-install $@
			return ;;
		urpm-rpm)
			sudocmd urpmi $URPMOPTIONS $@
			return ;;
		pkgsrc)
			sudocmd pkg_add -r $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1697 1698 1699
		pkgng)
			sudocmd pkg install $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1700 1701 1702 1703
		emerge)
			sudocmd emerge -uD $@
			return ;;
		pacman)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1704
			sudocmd pacman -S $force $nodeps $@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1705
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1706 1707 1708
		aura)
			sudocmd aura -A $force $nodeps $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1709 1710 1711 1712 1713 1714
		yum-rpm)
			sudocmd yum $YUMOPTIONS install $@
			return ;;
		dnf-rpm)
			sudocmd dnf install $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1715 1716 1717
		snappy)
			sudocmd snappy install $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1718 1719 1720 1721 1722 1723
		zypper-rpm)
			sudocmd zypper install $ZYPPEROPTIONS $@
			return ;;
		mpkg)
			sudocmd mpkg install $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1724 1725 1726
		conary)
			sudocmd conary update $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1727
		npackd)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1728 1729
			# FIXME: correct arg
			__separate_sudocmd_foreach "npackdcl add --package=" "npackdcl update --package=" $@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1730
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1731
		slackpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1732
			__separate_sudocmd_foreach "/usr/sbin/slackpkg install" "/usr/sbin/slackpkg upgrade" $@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1733 1734
			return ;;
		homebrew)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1735
			# FIXME: sudo and quote
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1736
			__separate_sudocmd "brew install" "brew upgrade" $@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1737 1738 1739 1740
			return ;;
		ipkg)
			[ -n "$force" ] && force=-force-depends
			sudocmd ipkg $force install $@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1741
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1742
		nix)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1743
			__separate_sudocmd "nix-env --install" "nix-env --upgrade" $@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1744
			return ;;
1745 1746 1747
		apk)
			sudocmd apk add $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1748 1749 1750
		guix)
			__separate_sudocmd "guix package -i" "guix package -i" $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1751
		android)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1752
			fatal "We still have no idea how to use package repository, ever if it is F-Droid."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1753
			return ;;
1754 1755 1756
		aptcyg)
			sudocmd apt-cyg install $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1757
		*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1758
			fatal "Have no suitable install command for $PMTYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1759 1760 1761 1762 1763 1764 1765 1766 1767
			;;
	esac
}

epm_ni_install_names()
{
	[ -z "$1" ] && return
	case $PMTYPE in
		apt-rpm|apt-dpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1768
			export DEBIAN_FRONTEND=noninteractive
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1769 1770
			sudocmd apt-get -y --force-yes -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" $APTOPTIONS install $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1771 1772 1773
		aptitude-dpkg)
			sudocmd aptitde -y install $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1774 1775 1776
		yum-rpm)
			sudocmd yum -y $YUMOPTIONS install $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1777 1778 1779
		dnf-rpm)
			sudocmd dnf -y $YUMOPTIONS install $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1780 1781 1782 1783 1784 1785 1786 1787 1788 1789
		urpm-rpm)
			sudocmd urpmi --auto $URPMOPTIONS $@
			return ;;
		zypper-rpm)
			# FIXME: returns true ever no package found, need check for "no found", "Nothing to do."
			yes | sudocmd zypper --non-interactive $ZYPPEROPTIONS install $@
			return ;;
		pkgsrc)
			sudocmd pkg_add -r $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1790 1791 1792
		pkgng)
			sudocmd pkg install -y $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1793 1794 1795
		emerge)
			sudocmd emerge -uD $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1796
		pacman)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1797
			sudocmd pacman -S --noconfirm $force $nodeps $@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1798
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1799 1800 1801
		aura)
			sudocmd aura -A $force $nodeps $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1802 1803
		npackd)
			#  npackdcl update --package=<package> (remove old and install new)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1804
			sudocmd npackdcl add --package=$@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1805 1806 1807 1808
			return ;;
		chocolatey)
			docmd chocolatey install $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1809 1810 1811
		ipkg)
			sudocmd ipkg -force-defaults install $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1812 1813 1814
		nix)
			sudocmd nix-env --install $@
			return ;;
1815 1816 1817
		apk)
			sudocmd apk add $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1818 1819 1820
		#android)
		#	sudocmd pm install $@
		#	return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1821
		slackpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1822
			# FIXME: broken status when use batch and default answer
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1823
			__separate_sudocmd_foreach "/usr/sbin/slackpkg -batch=on -default_answer=yes install" "/usr/sbin/slackpkg -batch=on -default_answer=yes upgrade" $@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1824 1825
			return ;;
		*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1826
			fatal "Have no suitable appropriate install command for $PMTYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1827 1828 1829 1830
			;;
	esac
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1831 1832 1833 1834 1835 1836 1837
__epm_check_if_rpm_already_installed()
{
	# Not: we can make optimize if just check version?
	LANG=C $SUDO rpm -Uvh $force $nodeps $@ 2>&1 | grep -q "is already installed"
}


Vitaly Lipatov's avatar
Vitaly Lipatov committed
1838 1839 1840 1841
epm_install_files()
{
    [ -z "$1" ] && return

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1842 1843 1844 1845
    # TODO: check read permissions
    # sudo test -r FILE
    # do not fallback to install_names if we have no permissions

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1846 1847 1848
    case $PMTYPE in
        apt-rpm)
            sudocmd rpm -Uvh $force $nodeps $@ && return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1849 1850 1851 1852
            local RES=$?

            __epm_check_if_rpm_already_installed $@ && return

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1853
            # if run with --nodeps, do not fallback on hi level
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1854
            [ -n "$nodeps" ] && return $RES
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1855 1856 1857

            # use install_names
            ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1858
        apt-dpkg|aptitude-dpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1859 1860 1861 1862
            # the new version of the conf. file is installed with a .dpkg-dist suffix
            if [ -n "$non_interactive" ] ; then
                DPKGOPTIONS="--force-confdef --force-confold"
            fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1863
            # FIXME: return false in case no install and in case install with broken deps
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1864
            sudocmd dpkg $DPKGOPTIONS -i $@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1865
            local RES=$?
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1866 1867
            # if run with --nodeps, do not fallback on hi level

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1868
            [ -n "$nodeps" ] && return $RES
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1869
            # fall to apt-get -f install for fix deps
1870 1871
            # can't use APTOPTIONS with empty install args
            epm_install_names -f
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1872 1873 1874

            # repeat install for get correct status
            sudocmd dpkg $DPKGOPTIONS -i $@
1875
            return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1876 1877 1878 1879 1880
            ;;
        yum-rpm|dnf-rpm)
            sudocmd rpm -Uvh $force $nodeps $@ && return
            # if run with --nodeps, do not fallback on hi level

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1881 1882
            __epm_check_if_rpm_already_installed $@ && return

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1883 1884 1885 1886 1887 1888
            [ -n "$nodeps" ] && return
            YUMOPTIONS=--nogpgcheck
            # use install_names
            ;;
        zypper-rpm)
            sudocmd rpm -Uvh $force $nodeps $@ && return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1889 1890 1891 1892
            local RES=$?

            __epm_check_if_rpm_already_installed $@ && return

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1893 1894
            # if run with --nodeps, do not fallback on hi level

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1895 1896
            [ -n "$nodeps" ] && return $RES
            ZYPPEROPTIONS=$(__use_zypper_no_gpg_checks)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1897 1898 1899 1900
            # use install_names
            ;;
        urpm-rpm)
            sudocmd rpm -Uvh $force $nodeps $@ && return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1901 1902 1903 1904
            local RES=$?

            __epm_check_if_rpm_already_installed $@ && return

Vitaly Lipatov's avatar
Vitaly Lipatov committed
1905
            # if run with --nodeps, do not fallback on hi level
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1906
            [ -n "$nodeps" ] && return $RES
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1907 1908 1909 1910 1911 1912 1913

            URPMOPTIONS=--no-verify-rpm
            # use install_names
            ;;
        pkgsrc)
            sudocmd pkg_add $@
            return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924
        pkgng)
            local PKGTYPE="$(get_package_type $@)"
            case "$PKGTYPE" in
                tbz)
                    sudocmd pkg_add $@
                    ;;
                *)
                    sudocmd pkg add $@
                    ;;
            esac
            return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1925 1926 1927
        android)
            sudocmd pm install $@
            return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1928 1929 1930
        emerge)
            sudocmd epm_install_emerge $@
            return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1931
        pacman)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1932
            sudocmd pacman -U --noconfirm $force $nodeps $@ && return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1933 1934 1935
            local RES=$?

            [ -n "$nodeps" ] && return $RES
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1936
            sudocmd pacman -U $force $@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1937 1938
            return ;;
        slackpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1939
            # FIXME: check for full package name
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1940
            # FIXME: broken status when use batch and default answer
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1941
            __separate_sudocmd_foreach "/sbin/installpkg" "/sbin/upgradepkg" $@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954
            return ;;
    esac

    # other systems can install file package via ordinary command
    epm_install_names "$@"
}

epm_print_install_command()
{
    case $PMTYPE in
        apt-rpm|yum-rpm|urpm-rpm|zypper-rpm|dnf-rpm)
            echo "rpm -Uvh --force $nodeps $@"
            ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1955
        apt-dpkg|aptitude-dpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1956 1957 1958 1959 1960
            echo "dpkg -i $@"
            ;;
        pkgsrc)
            echo "pkg_add $@"
            ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1961 1962 1963
        pkgng)
            echo "pkg add $@"
            ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1964 1965 1966 1967
        emerge)
            # need be placed in /usr/portage/packages/somewhere
            echo "emerge --usepkg $@"
            ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1968
        pacman)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1969
            echo "pacman -U --noconfirm --force $nodeps $@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1970 1971 1972 1973 1974 1975 1976
            ;;
        slackpkg)
            echo "/sbin/installpkg $@"
            ;;
        npackd)
            echo "npackdcl add --package=$@"
            ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1977 1978 1979
        ipkg)
            echo "ipkg install $@"
            ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1980 1981 1982
        android)
            echo "pm install $@"
            ;;
1983 1984 1985
        aptcyg)
            echo "apt-cyg install $@"
            ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1986
        *)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1987
            fatal "Have no suitable appropriate install command for $PMTYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999
            ;;
    esac
}


epm_install()
{
    if [ -n "$show_command_only" ] ; then
        epm_print_install_command $pkg_filenames
        return
    fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2000 2001
    # Download urls via eget pkg_urls and use eget
    # TODO: use optimization (rpm can download packages by url, yum too?)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2002 2003 2004 2005 2006
    #[ -n "$pkg_urls" ] && warning "URL using does not realize yet"
    #download_pkg_urls "$pkg_urls"
    # temp. hack
    pkg_files="$pkg_files $pkg_urls"
    # TODO: add downloaded files to $pkg_files
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2007

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2008
    [ -z "$pkg_files$pkg_names$pkg_urls" ] && info "Skip empty install list" && return 22
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2009 2010 2011

    local names="$(echo $pkg_names | filter_out_installed_packages)"
    local files="$(echo $pkg_files | filter_out_installed_packages)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2012
    local urls="$(echo $pkg_urls | filter_out_installed_packages)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2013

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2014
    [ -z "$files$names" ] && info "Skip empty install list" && return 22
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2015

2016 2017 2018 2019
    if [ -z "$files" ] ; then
        # it is useful for first time running
        update_repo_if_needed
    fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2020

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2021 2022 2023 2024 2025
    epm_install_names $names || return
    epm_install_files $files
}

# File bin/epm-Install:
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2026

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2027 2028 2029 2030

epm_Install()
{
    # copied from epm_install
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2031 2032 2033
    local names="$(echo $pkg_names | filter_out_installed_packages)"
    local files="$(echo $pkg_files | filter_out_installed_packages)"

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2034
    [ -z "$files$names" ] && info "Install: Skip empty install list." && return 22
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2035

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2036 2037 2038 2039 2040
	# do update only if really need install something
	case $PMTYPE in
		yum-rpm)
			;;
		*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2041
			pkg_filenames= epm_update || return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2042 2043 2044
			;;
	esac

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2045 2046
    epm_install_names $names || return
    epm_install_files $files
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084

}

# File bin/epm-install-emerge:



__emerge_install_ebuild()
{
	local EBUILD="$1"
	[ -s "$EBUILD" ] || fatal ".ebuild file '$EBUILD' is missing"

	# load ebuild and get vars
	. $(pwd)/$EBUILD
	[ -n "$SRC_URI" ] || fatal "Can't load SRC_URI from $EBUILD"

	# try to detect tarballs
	local TARBALLS=
	local BASEDIR=$(dirname $EBUILD)
	for i in $SRC_URI ; do
		[ -s "$BASEDIR/$(basename $i)" ] || continue
		TARBALLS="$TARBALLS $BASEDIR/$(basename $i)"
	done

	local PORTAGENAME=epm
	local LP=/usr/local/portage/$PORTAGENAME
	docmd mkdir -p $LP/
	MAKECONF=/etc/portage/make.conf
	[ -r "$MAKECONF" ] || MAKECONF=/etc/make.conf
	if ! grep -v "^#" $MAKECONF | grep -q $LP ; then
		echo "PORTDIR_OVERLAY=\"$LP \${PORTDIR_OVERLAY}\"" >>$MAKECONF
		# Overlay name
		mkdir -p $LP/profiles/
		echo "$PORTAGENAME" > $LP/profiles/repo_name
	fi

	# copy tarballs
	local DDIR=/usr/portage/distfiles
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2085
	# FIXME: use independent dir
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122
	[ -d /var/calculate/remote/distfiles ] && DDIR=/var/calculate/remote/distfiles
	docmd cp -f $TARBALLS $DDIR/ || return

	# copy ebuild
	docmd cp -f $EBUILD $LP/ || return
	cd $LP
	docmd ebuild $(basename $EBUILD) digest
	cd -
	# FIXME: more correcty get name
	local PKGNAME=$(echo $EBUILD | sed -e "s|-[0-9].*||g")
	docmd emerge -av $PKGNAME || return
}

__emerge_install_tbz2()
{
	local TGDIR=/usr/portage/packages/app-arch
	mkdir -p $TGDIR
	cp $i $TGDIR || return
	docmd emerge --usepkg $TGDIR/$(basename $i) || return
}

epm_install_emerge()
{
	local EBUILD=
	#local TARBALLS=
	local i

	# search ebuild in the args
	for i in $* ; do
		if echo $i | grep -q ebuild ; then
			__emerge_install_ebuild $i || return
		elif echo $i | grep -q "\.tbz2$" ; then
			__emerge_install_tbz2 $i || return
	#	else
	#		TARBALLS="$TARBALLS $i"
		fi
	done
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2123 2124
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2125 2126 2127 2128
# File bin/epm-kernel_update:

epm_kernel_update()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2129
	info "Starting update system kernel to the latest version"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2130 2131 2132

	case $DISTRNAME in
	ALTLinux)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2133 2134 2135 2136 2137
		load_helper epm-query_package
		if ! __epm_query_package kernel-image >/dev/null ; then
			info "No installed kernel packages, skipping update"
			return
		fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2138 2139 2140
		assure_exists update-kernel update-kernel 0.9.9
		sudocmd update-kernel $pkg_filenames || return
		sudocmd remove-old-kernels $pkg_filenames
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2141 2142 2143 2144 2145
		return ;;
	esac

	case $PMTYPE in
	*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2146
		fatal "Have no suitable command for $PMTYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2147 2148 2149 2150
		;;
	esac
}

2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166
# File bin/epm-optimize:

__repack_rpm_base()
{
	assure_exists db_dump || fatal
	assure_exists db_load || fatal
	cd /var/lib/rpm || fatal
	mv Packages Packages.BACKUP || fatal
	# mask dependencies with a=
	a= db_dump Packages.BACKUP | a= db_load Packages || fatal
	rm Packages.BACKUP
}

epm_optimize()
{

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2167 2168
[ -z "$pkg_filenames" ] || fatal "No arguments are allowed here"

2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181
case $PMTYPE in
	*-rpm)
		#__repack_rpm_base
		#rm -f /var/lib/rpm/__db*
		rpm --rebuilddb
		;;
	*)
		fatal "Have no suitable command for $PMTYPE"
		;;
esac

}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2182 2183
# File bin/epm-packages:

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2184 2185 2186 2187
__epm_packages_sort()
{
case $PMTYPE in
	apt-rpm|yum-rpm|urpm-rpm|zypper-rpm|dnf-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2188 2189
		# FIXME: space with quotes problems, use point instead
		docmd rpm -qa --queryformat "%{size}.%{name}-%{version}-%{release}\n" $pkg_filenames | sort -n
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2190 2191
		;;
	apt-dpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2192
		docmd dpkg-query -W --showformat="\${Size}.\${Package}-\${Version}\n" $pkg_filenames | sort -n
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2193 2194 2195 2196 2197 2198 2199
		;;
	*)
		fatal "Sorted package list are not realized for $PMTYPE"
		;;
esac
}

2200 2201 2202 2203 2204 2205 2206
__aptcyg_print_full()
{
	#showcmd apt-cyg show
	local VERSION=$(apt-cyg show "$1" | grep -m1 "^version: " | sed -e "s|^version: ||g")
	echo "$1-$VERSION"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2207 2208 2209
epm_packages()
{
	local CMD
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2210
	[ -n "$sort" ] && __epm_packages_sort && return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2211 2212 2213 2214 2215 2216

case $PMTYPE in
	apt-rpm)
		CMD="rpm -qa $pkg_filenames"
		[ -n "$short" ] && CMD="rpm -qa --queryformat %{name}\n $pkg_filenames"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2217
	*-dpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2218
		#CMD="dpkg -l $pkg_filenames"
2219 2220 2221 2222
		CMD="dpkg-query -W --showformat=\${db:Status-Abbrev}\${Package}-\${Version}\n $pkg_filenames"
		[ -n "$short" ] && CMD="dpkg-query -W --showformat=\${db:Status-Abbrev}\${Package}\n $pkg_filenames"
		docmd $CMD | grep "^i" | sed -e "s|.* ||g"
		return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2223
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2224 2225 2226
	snappy)
		CMD="snappy info"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2227 2228
	yum-rpm|urpm-rpm|zypper-rpm|dnf-rpm)
		CMD="rpm -qa $pkg_filenames"
2229
		[ -n "$short" ] && CMD="rpm -qa --queryformat %{name}\n $pkg_filenames"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2230 2231
		;;
	emerge)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2232 2233 2234
		CMD="qlist -I -C"
		# print with colors for console output
		isatty && CMD="qlist -I"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2235 2236 2237
		;;
	pkgsrc)
		CMD="pkg_info"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2238 2239 2240 2241
		docmd $CMD | sed -e "s| .*||g"
		return
		;;
	pkgng)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2242 2243 2244 2245 2246
		if [ -n "$pkg_filenames" ] ; then
			CMD="pkg info -E $pkg_filenames"
		else
			CMD="pkg info"
		fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2247 2248 2249 2250 2251 2252
		if [ -n "$short" ] ; then
		    docmd $CMD | sed -e "s| .*||g" | sed -e "s|-[0-9].*||g"
		else
		    docmd $CMD | sed -e "s| .*||g"
		fi
		return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2253 2254
		;;
	pacman)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2255 2256 2257 2258 2259
		CMD="pacman -Qs $pkg_filenames"
		if [ -n "$short" ] ; then
			docmd $CMD | sed -e "s| .*||g" -e "s|.*/||g" | grep -v "^$"
			return
		fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2260 2261
		;;
	npackd)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2262 2263
		CMD="npackdcl list --status=installed"
		# TODO: use search if pkg_filenames is not empty
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2264
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2265 2266 2267
	conary)
		CMD="conary query"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2268 2269 2270
	chocolatey)
		CMD="chocolatey list"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2271 2272
	slackpkg)
		CMD="ls -1 /var/log/packages/"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2273
		if [ -n "$short" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2274 2275 2276 2277
			# FIXME: does not work for libjpeg-v8a
			# TODO: remove last 3 elements (if arch is second from the last?)
			# FIXME this hack
			docmd ls -1 /var/log/packages/ | sed -e "s|-[0-9].*||g" | sed -e "s|libjpeg-v8a.*|libjpeg|g"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2278 2279
			return
		fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2280
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2281
	homebrew)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2282
		CMD="brew list $pkg_filenames"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2283 2284 2285 2286
		;;
	ipkg)
		CMD="ipkg list"
		;;
2287 2288 2289
	apk)
		CMD="apk info"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2290 2291 2292
	guix)
		CMD="guix package -I"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2293 2294 2295 2296 2297
	android)
		CMD="pm list packages"
		docmd $CMD | sed -e "s|^package:||g"
		return
		;;
2298 2299 2300 2301
	aptcyg)
		CMD="apt-cyg list $pkg_filenames"
		if [ -z "$short" ] ; then
			showcmd $CMD
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2302
			# TODO: fix this slow way
2303 2304 2305 2306 2307 2308
			for i in $($CMD) ; do
				__aptcyg_print_full $i
			done
			return
		fi
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2309
	*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2310
		fatal "Have no suitable query command for $PMTYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2311 2312 2313 2314 2315 2316 2317
		;;
esac

docmd $CMD

}

2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392
# File bin/epm-policy:

epm_policy()
{

[ -n "$pkg_names" ] || fatal "Info: missing package(s) name"

case $PMTYPE in
	apt-rpm)
		docmd apt-cache policy $pkg_names
		;;
	apt-dpkg)
		docmd apt-cache policy $pkg_names
		;;
	*)
		fatal "Have no suitable command for $PMTYPE"
		;;
esac

}

# File bin/epm-print:


query_package_field()
{
	local FORMAT="%{$1}\n"
	shift
	local INSTALLED="-p"
	# if not file, drop -p for get from rpm base
	[ -e "$1" ] || INSTALLED=""
	rpmquery $INSTALLED --queryformat "$FORMAT" "$@"
}


print_binpkgfilelist()
{
	local PKGDIR=$1
	local PKGNAME=$(basename $2)
	find "$PKGDIR" ! -name '*\.src\.rpm' -name '*\.rpm' -execdir \
		rpmquery -p --qf='%{sourcerpm}\t%{name}-%{version}-%{release}.%{arch}.rpm\n' "{}" \; \
		| grep "^$PKGNAME[[:space:]].*" | cut -f2 | xargs -n1 -I "{}" echo -n "$PKGDIR/{} "
}

PKGNAMEMASK="\(.*\)-\([0-9].*\)-\(.*[0-9].*\)"

print_name()
{
    echo "$@" | xargs -n1 echo | sed -e "s|$PKGNAMEMASK|\1|g"
}

print_version()
{
    echo "$1" | xargs -n1 echo | sed -e "s|$PKGNAMEMASK|\2|g"
}

print_release()
{
    echo "$1" | xargs -n1 echo | sed -e "s|$PKGNAMEMASK|\3|g"
}

print_pkgname()
{
    local i
    for i in $@ ; do
        # TODO: deb and other, arch string
        echo "$(basename "$i") " | sed -e "s|\.[a-z_0-9]*\.rpm||g" -e "s|\(.*\)_\(.*\)_[a-z_0-9]*\.deb|\1-\2|g"
    done
}

print_srcname()
{
    print_name $(print_srcpkgname "$@")
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2393 2394 2395 2396 2397 2398
print_specname()
{
    # CHECKME: it is possible to have two or more specs in one package?
    rpm -qlp "$@" | grep "\.spec\$"
}

2399 2400 2401 2402 2403
print_srcpkgname()
{
    query_package_field sourcerpm "$@"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2404 2405 2406 2407 2408
compare_version()
{
    which rpmevrcmp 2>/dev/null >/dev/null || fatal "rpmevrcmp exists in ALT Linux only"
    rpmevrcmp "$@"
}
2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439

__epm_print()
{
    local WHAT="$1"
    shift
    local FNFLAG=
    local PKFLAG=
    [ "$1" = "from" ] && shift
    [ "$1" = "for" ] && shift
    [ "$1" = "in" ] && shift
    if [ "$1" = "filename" ] ; then
        FNFLAG="$1"
        shift
    fi

    if [ "$1" = "package" ] ; then
        PKFLAG="$1"
        shift
    fi

    case "$WHAT" in
        "")
            fatal "Use epm print help for get help."
            ;;
        "-h"|"--help"|"help")
cat <<EOF
  Examples:
    epm print name [from filename|for package] NN        print only name of package name or package file
    epm print version [from filename|for package] NN     print only version of package name or package file
    epm print release [from filename|for package] NN     print only release of package name or package file
    epm print field FF for package NN        print field of the package
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2440 2441
    epm print pkgname from filename NN       print package name for the package file
    epm print srcname from filename NN       print source name for the package file
2442
    epm print srcpkgname from [filename|package] NN    print source package name for the binary package file
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2443
    epm print specname from filename NN      print spec filename for the source package file
2444
    epm print binpkgfilelist in DIR for NN   list binary package(s) filename(s) from DIR for the source package file
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2445
    epm print compare [package] version N1 N2          compare (package) versions and print -1, 0, 1
2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500
EOF
            ;;
        "name")
            [ -n "$1" ] || fatal "Arg is missed"
            if [ -n "$FNFLAG" ] ; then
                print_name $(print_pkgname "$@")
            elif [ -n "$PKFLAG" ] ; then
                query_package_field "name" "$@"
            else
                print_name "$@"
            fi
            ;;
        "version")
            [ -n "$1" ] || fatal "Arg is missed"
            if [ -n "$FNFLAG" ] ; then
                print_version $(print_pkgname "$@")
            elif [ -n "$PKFLAG" ] ; then
                query_package_field "version" "$@"
            else
                print_version "$@"
            fi
            ;;
        "release")
            [ -n "$1" ] || fatal "Arg is missed"
            if [ -n "$FNFLAG" ] ; then
                print_release $(print_pkgname "$@")
            elif [ -n "$PKFLAG" ] ; then
                query_package_field "release" "$@"
            else
                print_release "$@"
            fi
            ;;
        "field")
            [ -n "$1" ] || fatal "Arg is missed"
            local FIELD="$1"
            shift
            [ "$1" = "for" ] && shift
            query_package_field "$FIELD" "$@"
            ;;
        "pkgname")
            [ -n "$FNFLAG" ] || fatal "print $WHAT works only for filename(s)"
            [ -n "$1" ] || fatal "Arg is missed"
            # TODO: drop_pkg_extensions
            print_pkgname "$@"
            ;;
        "srcname")
            [ -n "$FNFLAG" ] || fatal "print $WHAT works only for filename(s)"
            [ -n "$1" ] || fatal "Arg is missed"
            print_srcname "$@"
            ;;
        "srcpkgname")
            [ -n "$FNFLAG" ] || [ -n "$PKFLAG" ] || fatal "print $WHAT works only for filename(s)"
            [ -n "$1" ] || fatal "Arg is missed"
            print_srcpkgname "$@"
            ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2501 2502 2503 2504 2505
        "specname")
            [ -n "$FNFLAG" ] || [ -n "$PKFLAG" ] || fatal "print $WHAT works only for filename(s)"
            [ -n "$1" ] || fatal "Arg is missed"
            print_specname "$@"
            ;;
2506 2507 2508 2509 2510 2511 2512 2513 2514 2515
        "binpkgfilelist")
            # TODO: rpm only
            # TODO: replace get_binpkg_list
            local DIR="$1"
            shift
            [ "$1" = "for" ] && shift
            [ -n "$DIR" ] || fatal "DIR arg is missed"
            [ -n "$1" ] || fatal "source package filename is missed"
            print_binpkgfilelist "$DIR" "$1"
            ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2516 2517 2518 2519 2520 2521 2522 2523 2524
        "compare")
            [ "$1" = "version" ] && shift
            [ -n "$1" ] || fatal "Arg is missed"
            #if [ -n "$PKFLAG" ] ; then
            #    query_package_field "name" "$@"
            #else
                 compare_version "$1" "$2"
            #fi
            ;;
2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539
        *)
            fatal "Unknown command $ epm print $WHAT. Use epm print help for get help."
            ;;
    esac
}


epm_print()
{

    [ -n "$pkg_filenames" ] || fatal "Missed args. Use epm print help for get help."
    __epm_print $(eval echo $quoted_args)
}


Vitaly Lipatov's avatar
Vitaly Lipatov committed
2540 2541 2542 2543 2544
# File bin/epm-programs:


epm_programs()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2545 2546 2547 2548 2549 2550 2551 2552 2553
	case $DISTRNAME in
		FreeBSD|NetBSD|OpenBSD|Solaris)
			local DESKTOPDIR=/usr/local/share/applications
			;;
		*)
			local DESKTOPDIR=/usr/share/applications
			;;
	esac

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2554 2555
	[ -d "$DESKTOPDIR" ] || fatal "There is no $DESKTOPDIR dir on the system."
	#find /usr/share/applications -type f -name "*.desktop" | while read f; do pkg_files="$f" quiet=1 short=1 epm_query_file ; done | sort -u
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2556
	showcmd "find $DESKTOPDIR -type f -name "*.desktop" | xargs $0 -qf --quiet --short | sort -u"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2557
	find $DESKTOPDIR -type f -name "*.desktop" | \
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2558 2559 2560
		xargs $0 -qf --quiet --short | sort -u
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2561 2562
# File bin/epm-provides:

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2563

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2564
epm_provides_files()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2565
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2566
	local pkg_files="$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2567 2568
	[ -n "$pkg_files" ] || return

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2569 2570 2571
	local PKGTYPE="$(get_package_type $pkg_files)"

	case $PKGTYPE in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2572
		rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2573
			assure_exists rpm
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2574
			docmd rpm -q --provides -p $pkg_files
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2575 2576
			;;
		deb)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2577
			assure_exists dpkg
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2578 2579 2580 2581 2582 2583 2584 2585
			# FIXME: will we provide ourself?
			docmd dpkg -I $pkg_files | grep "^ *Provides:" | sed "s|^ *Provides:||g"
			;;
		*)
			fatal "Have no suitable command for $PMTYPE"
			;;
	esac
}
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2586 2587


Vitaly Lipatov's avatar
Vitaly Lipatov committed
2588 2589
epm_provides_names()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2590
	local pkg_names="$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2591 2592
	local CMD
	[ -n "$pkg_names" ] || return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2593 2594 2595

case $PMTYPE in
	apt-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2596 2597
		# FIXME: need fix for a few names case
		# TODO: separate this function to two section
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2598 2599 2600
		if is_installed $pkg_names ; then
			CMD="rpm -q --provides"
		else
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2601 2602 2603
			EXTRA_SHOWDOCMD=' | grep "Provides:"'
			docmd apt-cache show $pkg_names | grep "Provides:"
			return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2604 2605
		fi
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2606
	urpm-rpm|zypper-rpm|yum-rpm|dnf-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2607 2608 2609 2610 2611 2612 2613
		if is_installed $pkg_names ; then
			CMD="rpm -q --provides"
		else
			fatal "FIXME: use hi level commands"
		fi
		;;
	emerge)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2614
		assure_exists equery
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2615 2616
		CMD="equery files"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2617 2618 2619
	pkgng)
		CMD="pkg info -b"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2620 2621 2622
	apt-dpkg)
		# FIXME: need fix for a few names case
		if is_installed $pkg_names ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2623
			info "Please inform the author how to get provides from dpkg"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2624 2625 2626
		fi
		#	CMD="rpm -q --provides"
		#else
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2627 2628 2629
			EXTRA_SHOWDOCMD=' | grep "Provides:"'
			docmd apt-cache show $pkg_names | grep "Provides:"
			return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2630
		#fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2631
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2632 2633 2634 2635 2636
	*)
		fatal "Have no suitable command for $PMTYPE"
		;;
esac

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2637 2638 2639
docmd $CMD $pkg_names

}
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2640

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2641 2642
epm_provides()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2643 2644
	[ -n "$pkg_filenames" ] || fatal "Provides: missing package(s) name"

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2645
	epm_provides_files $pkg_files
2646
	epm_provides_names $(print_name $pkg_names)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2647 2648
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2649 2650 2651 2652
# File bin/epm-query:



Vitaly Lipatov's avatar
Vitaly Lipatov committed
2653 2654 2655 2656 2657 2658 2659 2660 2661
_get_grep_exp()
{
	local def="^$1$"
	[ "$PMTYPE" != "emerge" ] && echo "$def" && return
	# Gentoo hack: support for short package form
	echo "$1" | grep -q "/" && echo "$def" && return
	echo "/$1$"
}

2662
_shortquery_via_packages_list()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2663 2664
{
	local res=0
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2665
	local grepexp
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2666 2667
	local firstpkg=$1
	shift
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2668

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2669 2670
	grepexp=$(_get_grep_exp $firstpkg)

2671
	# Note: we use short=1 here due grep by ^name$
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2672
	# separate first line for print out command
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2673
	short=1 pkg_filenames=$firstpkg epm_packages | grep -- "$grepexp" || res=1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2674

2675
	local pkg
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2676
	for pkg in "$@" ; do
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2677 2678
		grepexp=$(_get_grep_exp $pkg)
		short=1 pkg_filenames=$pkg epm_packages 2>/dev/null | grep -- "$grepexp" || res=1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2679
	done
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2680

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2681 2682 2683
	return $res
}

2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694
_query_via_packages_list()
{
	local res=0
	local grepexp
	local firstpkg=$1
	shift

	grepexp=$(_get_grep_exp $firstpkg)

	# Note: we use short=1 here due grep by ^name$
	# separate first line for print out command
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2695
	short=1 pkg_filenames=$firstpkg epm_packages | grep -q -- "$grepexp" && quiet=1 pkg_filenames=$firstpkg epm_packages || res=1
2696 2697 2698 2699

	local pkg
	for pkg in "$@" ; do
		grepexp=$(_get_grep_exp $pkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2700
		short=1 pkg_filenames=$pkg epm_packages 2>/dev/null | grep -q -- "$grepexp" && quiet=1 pkg_filenames=$pkg epm_packages || res=1
2701 2702 2703 2704 2705
	done

	return $res
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740
__epm_get_hilevel_nameform()
{
	[ -n "$*" ] || return

	case $PMTYPE in
		apt-rpm)
			# use # as delimeter for apt
			local pkg
			pkg=$(rpm -q --queryformat "%{NAME}#%{SERIAL}:%{VERSION}-%{RELEASE}\n" $1)
			echo $pkg | grep -q "(none)" && pkg=$(rpm -q --queryformat "%{NAME}#%{VERSION}-%{RELEASE}\n" $1)
			# HACK: can use only for multiple install packages like kernel
			echo $pkg | grep -q kernel || return 1
			echo $pkg
			return
			;;
		yum-rpm)
			# just use strict version with Epoch and Serial
			local pkg
			pkg=$(rpm -q --queryformat "%{EPOCH}:%{NAME}%{VERSION}-%{RELEASE}.${ARCH}\n" $1)
			echo $pkg | grep -q "(none)" && pkg=$(rpm -q --queryformat "%{NAME}-%{VERSION}-%{RELEASE}.${ARCH}\n" $1)
			echo $pkg
			return
			;;
		*)
			return 1
			;;
	esac
}

__epm_get_hilevel_name()
{
	local i
	for i in $@ ; do
		local pkg
		# get short form in pkg
2741
		quiet=1 pkg=$(__epm_query_shortname $i) || continue # drop not installed packages
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2742 2743 2744 2745 2746 2747 2748
		# if already short form, skipped
		[ "$pkg" = "$i" ] && echo "$i" && continue
		# try get long form or use short form
		__epm_get_hilevel_nameform $i || echo $pkg
	done
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2749 2750 2751 2752 2753 2754 2755
__epm_query_file()
{
	local CMD

	[ -z "$*" ] && return

	case $PMTYPE in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2756
		*-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2757
			CMD="rpm -qp"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2758
			[ -n "$short" ] && CMD="rpm -qp --queryformat %{name}\n"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2759
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2760
		*-dpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2761 2762
			CMD="dpkg-deb --show --showformat=\${Package}-\${Version}\n"
			[ -n "$short" ] && CMD="dpkg-query --show --showformat=\${Package}\n"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778
			;;
		*)
			fatal "Do not know command for query file package"
			;;
	esac

	docmd $CMD $@
}

__epm_query_name()
{
	local CMD

	[ -z "$*" ] && return

	case $PMTYPE in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2779
		*-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2780 2781
			CMD="rpm -q"
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2782
		*-dpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2783 2784
			#docmd dpkg -l $@ | grep "^ii"
			CMD="dpkg-query -W --showformat=\${Package}-\${Version}\n"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2785 2786
			;;
		npackd)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2787 2788 2789 2790 2791
			docmd "npackdcl path --package=$@"
			return
			;;
		conary)
			CMD="conary query"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2792
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2793
		homebrew)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2794 2795 2796
			warning "fix query"
			return 1
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2797 2798 2799 2800
		# TODO: need to print name if exists
		#pkgng)
		#	CMD="pkg info -e"
		#	;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2801
		# Note: slackpkg info pkgname
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2802
		*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2803
			# default slow workaround
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2804 2805 2806 2807 2808 2809 2810 2811
			_query_via_packages_list $@
			return
			;;
	esac

	docmd $CMD $@
}

2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850
__epm_query_shortname()
{
	local CMD

	[ -z "$*" ] && return

	case $PMTYPE in
		*-rpm)
			CMD="rpm -q --queryformat %{name}\n"
			;;
		*-dpkg)
			CMD="dpkg-query -W --showformat=\${Package}\n"
			;;
		npackd)
			docmd "npackdcl path --package=$@"
			return
			;;
		conary)
			CMD="conary query"
			;;
		homebrew)
			warning "fix query"
			return 1
			;;
		# TODO: need to print name if exists
		#pkgng)
		#	CMD="pkg info -e"
		#	;;
		# Note: slackpkg info pkgname
		*)
			# default slow workaround
			_shortquery_via_packages_list $@
			return
			;;
	esac

	docmd $CMD $@
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
2851 2852
is_installed()
{
2853
	short=1 pkg_filenames="$@" pkg_names="$@" epm_query >/dev/null 2>/dev/null
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2854 2855
	# broken way to recursive call here (overhead!)
	#epm installed $@ >/dev/null 2>/dev/null
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2856 2857 2858 2859 2860 2861 2862 2863 2864
}

separate_installed()
{
	pkg_installed=
	pkg_noninstalled=
	for i in $* ; do
		is_installed $i && pkg_installed="$pkg_installed $i" || pkg_noninstalled="$pkg_noninstalled $i"
	done
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2865 2866 2867 2868
}

epm_query()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2869
	[ -n "$pkg_filenames" ] || fatal "Query: missing package(s) name"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2870 2871 2872

	__epm_query_file $pkg_files || return

2873 2874 2875 2876 2877
	if [ -n "$short" ] ; then
		__epm_query_shortname $(print_name $pkg_names) || return
	else
		__epm_query_name $(print_name $pkg_names) || return
	fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888
}

# File bin/epm-query_file:


__do_query_real_file()
{
	local TOFILE
	
	# get canonical path
	if [ -e "$1" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2889
		TOFILE="$1"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2890
	else
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2891
		TOFILE=$(which "$1" 2>/dev/null || echo "$1")
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2892
		if [ "$TOFILE" != "$1" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2893
			info "Note: $1 is placed as $TOFILE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2894 2895 2896 2897 2898
		fi
	fi
	
	# get value of symbolic link
	if [ -L "$TOFILE" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2899 2900
		local LINKTO
		__do_query "$TOFILE"
2901
		LINKTO=$(readlink -f "$TOFILE")
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2902
		info "Note: $TOFILE is link to $LINKTO"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2903
		__do_query_real_file "$LINKTO"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2904
		return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2905
	fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2906 2907

	FULLFILEPATH="$TOFILE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930
}

dpkg_print_name_version()
{
	local ver i
	for i in $* ; do
		ver=$(dpkg -s $i 2>/dev/null | grep "Version:" | sed -e "s|Version: ||g")
		if [ -z "$ver" ] ; then
			echo "$i"
		else
			echo "$i-$ver"
		fi
	done
}


__do_query()
{
    local CMD
    case $PMTYPE in
        apt-rpm)
            CMD="rpm -qf"
            ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2931
        *-dpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2932
            showcmd dpkg -S $1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2933
            dpkg_print_name_version $(dpkg -S $1 | grep -v "^diversion by" | sed -e "s|:.*||")
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2934
            return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2935
        yum-rpm|dnf-rpm|urpm-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2936 2937 2938 2939 2940 2941
            CMD="rpm -qf"
            ;;
        zypper-rpm)
            CMD="rpm -qf"
            ;;
        emerge)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2942
            assure_exists equery
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2943 2944 2945 2946 2947
            CMD="equery belongs"
            ;;
        pacman)
            CMD="pacman -Qo"
            ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2948 2949 2950
        pkgng)
            CMD="pkg which"
            ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2951 2952 2953
        conary)
            CMD="conary query --path"
            ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2954 2955 2956 2957 2958
        slackpkg)
            # note: need remove leading slash for grep
            docmd grep -R -- "$(echo $@ | sed -e 's|^/\+||g')" /var/log/packages | sed -e "s|/var/log/packages/||g"
            return
            ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2959 2960 2961
        ipkg)
            CMD="ipkg files"
            ;;
2962 2963 2964 2965 2966
        aptcyg)
            #CMD="apt-cyg packageof"
            # do not realized locally
            return 1
            ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2967
        *)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2968
            fatal "Have no suitable query command for $PMTYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984
            ;;
    esac

    docmd $CMD $@
}


__do_short_query()
{
    local CMD
    case $PMTYPE in
        *-rpm)
            CMD="rpm -qf --queryformat %{NAME}\n"
            ;;
        NOapt-dpkg)
            showcmd dpkg -S $1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2985
            dpkg_print_name_version $(dpkg -S $1 | sed -e "s|:.*||" | grep -v "^diversion by")
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2986 2987
            return ;;
        NOemerge)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2988
            assure_exists equery
Vitaly Lipatov's avatar
Vitaly Lipatov committed
2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999
            CMD="equery belongs"
            ;;
        NOpacman)
            CMD="pacman -Qo"
            ;;
        NOslackpkg)
            # note: need remove leading slash for grep
            docmd grep -R "$(echo $@ | sed -e 's|^/\+||g')" /var/log/packages | sed -e "s|/var/log/packages/||g"
            return
            ;;
        *)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3000
            fatal "Have no suitable query command for $PMTYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3001 3002 3003 3004 3005 3006 3007 3008 3009
            ;;
    esac

    docmd $CMD $@
}


epm_query_file()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3010
    # И где это используется?
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3011 3012
    # in short mode print handle only real names and do short output
    # TODO: move to separate command?
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3013
    # FIXME: it is possible use query
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3014
    if [ -n "$short" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3015 3016
        [ -n "$pkg_files$pkg_dirs" ] || fatal "Run query without file names (needed path to files)"
        __do_short_query $pkg_files $pkg_dirs
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3017 3018 3019 3020 3021 3022
         return
    fi

    # file can exists or not
    [ -n "$pkg_filenames" ] || fatal "Run query without file names"

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3023 3024


Vitaly Lipatov's avatar
Vitaly Lipatov committed
3025 3026
    for pkg in $pkg_filenames ; do
        __do_query_real_file "$pkg"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3027
        __do_query "$FULLFILEPATH" || pkg_filenames="$FULLFILEPATH" epm_search_file
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3028 3029 3030 3031 3032 3033 3034
    done

}

# File bin/epm-query_package:


Vitaly Lipatov's avatar
Vitaly Lipatov committed
3035 3036 3037 3038 3039
__epm_query_package()
{
	pkg_filenames="$@" quoted_args="$@" quiet=1 epm_query_package
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3040 3041
epm_query_package()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3042
	[ -n "$pkg_filenames" ] || fatal "Please, use search with some argument or run epmqa for get all packages."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3043
	# FIXME: do it better
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3044 3045
	local MGS
	MGS=$(eval __epm_search_make_grep $quoted_args)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3046
	EXTRA_SHOWDOCMD=$MGS
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3047
	# Note: get all packages list and do grep
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3048
	eval "pkg_filenames= epm_packages \"$(eval get_firstarg $quoted_args)\" $MGS"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3049 3050 3051 3052
}

# File bin/epm-reinstall:

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3053

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3054 3055 3056 3057 3058
epm_reinstall_names()
{
	[ -n "$1" ] || return
	case $PMTYPE in
		apt-rpm|apt-dpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3059 3060
			local APTOPTIONS="$(subst_option non_interactive -y)"
			sudocmd apt-get --reinstall $APTOPTIONS install $@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3061
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3062 3063 3064
		aptitude-dpkg)
			sudocmd aptitude reinstall $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3065 3066 3067
		dnf-rpm)
			sudocmd dnf reinstall $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3068
		pkgng)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3069
			sudocmd pkg install -f $@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3070
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3071
		slackpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3072
			sudocmd_foreach "/usr/sbin/slackpkg reinstall" $@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3073 3074
			return ;;
	esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3075 3076

	# fallback to generic install
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3077
	epm_install_names $@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088
}

epm_reinstall_files()
{
    [ -z "$1" ] && return

    case $PMTYPE in
        apt-rpm)
            sudocmd rpm -Uvh --force $@ && return
            sudocmd apt-get --reinstall install $@
            return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3089
        apt-dpkg|aptitude-dpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3090 3091 3092
            sudocmd dpkg -i $@
            return ;;
        slackpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3093
            sudocmd_foreach "/sbin/installpkg" $@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3094 3095 3096 3097 3098 3099 3100 3101 3102 3103
            return ;;
    esac

    # other systems can install file package via ordinary command
    epm_reinstall_names $@
}


epm_reinstall()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3104
    [ -n "$pkg_filenames" ] || fatal "Reinstall: missing package(s) name."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3105

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3106 3107 3108
    # get package name for hi level package management command (with version if supported and if possible)
    pkg_names=$(__epm_get_hilevel_name $pkg_names)

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3109 3110 3111 3112 3113
    epm_reinstall_names $pkg_names
    epm_reinstall_files $pkg_files
}


Vitaly Lipatov's avatar
Vitaly Lipatov committed
3114 3115
# File bin/epm-release_upgrade:

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3116 3117 3118 3119 3120 3121 3122 3123 3124 3125

__replace_text_in_alt_repo()
{
	local i
	for i in /etc/apt/sources.list /etc/apt/sources.list.d/*.list ; do
		[ -s "$i" ] || continue
		regexp_subst "$1" "$i"
	done
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149
__wcount()
{
	echo "$*" | wc -w
}

__detect_alt_release_by_repo()
{
	local BRD=$(cat /etc/apt/sources.list /etc/apt/sources.list.d/*.list | \
		grep -v "^#" | grep "p[5-9]/branch/" | sed -e "s|.*\(p[5-9]\)/branch.*|\1|g" | sort -u )
	if [ $(__wcount $BRD) = "1" ] ; then
		echo "$BRD"
		return
	fi

	local BRD=$(cat /etc/apt/sources.list /etc/apt/sources.list.d/*.list | \
		grep -v "^#" | grep "Sisyphus/" | sed -e "s|.*\(Sisyphus\).*|\1|g" | sort -u )
	if [ $(__wcount $BRD) = "1" ] ; then
		echo "$BRD"
		return
	fi

	return 1
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3150 3151 3152 3153
__replace_alt_version_in_repo()
{
	local i
	assure_exists apt-repo
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3154 3155 3156 3157
	#echo "Upgrading $DISTRNAME from $1 to $2 ..."
	docmd apt-repo list | sed -e "s|\($1\)|{\1}->{$2}|g" | egrep --color -- "$1"
	confirm "Are these correct changes? [y/N]" || fatal "Exiting"
	__replace_text_in_alt_repo "/^ *#/! s!$1!$2!g"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3158 3159 3160
	docmd apt-repo list
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3161
__alt_repofix()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3162
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173
	load_helper epm-repofix
	showcmd epm repofix
	quiet=1 pkg_filenames= epm_repofix >/dev/null
	__replace_text_in_alt_repo "/^ *#/! s!\[p[6-9]\]![updates]!g"
}

__update_to_the_distro()
{
	__alt_repofix
	case "$1" in
		p7)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3174
			docmd epm update || fatal
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3175 3176
			docmd epm install apt rpm apt-conf-branch altlinux-release-p7 || fatal "Check an error and run epm release-upgrade again"
			__alt_repofix
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3177 3178 3179
			__replace_text_in_alt_repo "/^ *#/! s!\[updates\]![p7]!g"
			docmd epm update || fatal
			docmd epm upgrade || fatal "Check an error and run epm release-upgrade again"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3180
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3181
		p8)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3182
			docmd epm update || fatal
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3183 3184 3185 3186 3187 3188
			if ! docmd epm install apt rpm apt-conf-branch altlinux-release-p8 ; then
				# error: execution of %post scriptlet from glibc-core-2.23-alt1.eter1
				docmd epm erase glibc-core-2.17 || fatal "Check an error and run epm release-upgrade again"
				docmd epm install apt rpm apt-conf-branch altlinux-release-p8 || fatal "Check an error and run epm release-upgrade again"
			fi
			__alt_repofix
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3189 3190 3191 3192 3193 3194
			__replace_text_in_alt_repo "/^ *#/! s!\[updates\]![p8]!g"
			docmd epm update || fatal
			if is_installed systemd && is_active_systemd systemd ; then
				docmd epm install systemd || fatal
			fi
			docmd epm upgrade || fatal "Check an error and run epm release-upgrade again"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243
			;;
		Sisyphus)
			docmd epm update || fatal
			docmd epm install apt rpm apt-conf-sisyphus altlinux-release-sisyphus || fatal "Check an error and run again"
			docmd epm upgrade || fatal "Check an error and run epm release-upgrade again"
			;;
		*)
	esac
}

__update_alt_to_next_distro()
{
	local FROMTO=$(echo "$*" | sed -e "s| | to |")
	info
 	case "$*" in
		"p6"|"p6 p7")
			info "Upgrade $DISTRNAME from p6 to p7 ..."
			docmd epm install apt-conf-branch || fatal
			__replace_alt_version_in_repo p6/branch/ p7/branch/
			__update_to_the_distro p7
			docmd epm update-kernel
			info "Done."
			info "Run epm release-upgrade again for update to p8"
			;;
		"p7"|"p7 p8")
			info "Upgrade $DISTRNAME from p7 to p8 ..."
			docmd epm install apt-conf-branch altlinux-release-p7 || fatal
			__replace_alt_version_in_repo p7/branch/ p8/branch/
			__update_to_the_distro p8
			docmd epm update-kernel || fatal
			info "Done."
			;;
		"Sisyphus p8")
			info "Downgrade $DISTRNAME from Sisyphus to p8 ..."
			docmd epm install apt-conf-branch || fatal
			__replace_alt_version_in_repo Sisyphus/ p8/branch/
			__replace_text_in_alt_repo "/^ *#/! s!\[alt\]![p8]!g"
			__update_to_the_distro p8
			docmd epm downgrade || fatal
			info "Done."
			;;
		"p8 Sisyphus")
			info "Upgrade $DISTRNAME from p8 to Sisyphus ..."
			docmd epm install apt-conf-branch || fatal
			docmd epm upgrade || fatal
			__replace_alt_version_in_repo p8/branch/ Sisyphus/
			__alt_repofix
			__replace_text_in_alt_repo "/^ *#/! s!\[updates\]![alt]!g"
			__update_to_the_distro Sisyphus
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3244 3245
			docmd epm update-kernel || fatal
			info "Done."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3246 3247
			;;
		*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3248 3249
			warning "Have no idea how to update from $DISTRNAME $DISTRVERSION."
			info "Try run f.i. # epm release-upgrade p8 or # epm release-upgrade Sisyphus"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3250 3251 3252
			return 1
	esac
}
3253 3254 3255

epm_release_upgrade()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3256
	assure_root
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3257 3258
	info "Starting upgrade whole system to the next release"
	info "Check also http://wiki.etersoft.ru/Admin/UpdateLinux"
3259

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3260 3261
	case $DISTRNAME in
	ALTLinux)
3262 3263
		docmd epm update
		docmd epm install apt rpm
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278

		# try to detect current release by repo
		if [ "$DISTRVERSION" = "Sisyphus" ] || [ -z "$DISTRVERSION" ] ; then
			DISTRVERSION="$(__detect_alt_release_by_repo)"
			[ "$DISTRVERSION" != "Sisyphus" ] && info "Detected running $DISTRNAME $DISTRVERSION (according to using repos)"
		fi

		__alt_repofix

		# check forced target
		if [ -n "$pkg_filenames" ] ; then
			[ "$(__wcount $pkg_filenames)" = "1" ] || fatal "Too many args: $pkg_filenames"
		fi

		__update_alt_to_next_distro $DISTRVERSION $pkg_filenames
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3279 3280 3281 3282 3283 3284 3285 3286 3287 3288
		return
		;;
	*)
		;;
	esac

	case $PMTYPE in
	apt-rpm)
		docmd epm update
		info "Have no idea how to upgrade $DISTRNAME"
3289
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3290
	*-dpkg)
3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317
		sudocmd do-release-upgrade -d
		;;
	yum-rpm)
		docmd epm install rpm yum
		sudocmd yum clean all
		# TODO
		showcmd rpm -Uvh http://mirror.yandex.ru/fedora/linux/releases/16/Fedora/x86_64/os/Packages/fedora-release-16-1.noarch.rpm
		docmd epm Upgrade
		;;
	urpm-rpm)
		sudocmd urpmi.removemedia -av
		# TODO
		showcmd urpmi.addmedia --distrib http://mirror.yandex.ru/mandriva/devel/2010.2/i586/
		sudocmd urpmi --auto-update --replacefiles
		;;
	zypper-rpm)
		docmd epm repolist
		# TODO
		# sudocmd zypper rr <номер_репозитория>
		showcmd rr N
		showcmd epm ar http://mirror.yandex.ru/opensuse/distribution/11.1/repo/oss 11.1oss
		showcmd zypper ref
		docmd epm update
		docmd epm install rpm zypper
		docmd epm upgrade
		;;
	pacman)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3318
		epm Upgrade
3319
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3320 3321 3322
	conary)
		epm Upgrade
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3323 3324 3325 3326 3327 3328
	emerge)
		epm Upgrade
		;;
	guix)
		sudocmd guix pull --verbose
		;;
3329
	*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3330
		fatal "Have no suitable command for $PMTYPE"
3331 3332 3333 3334 3335
		;;
	esac

}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3336 3337
# File bin/epm-remove:

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3338

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3339 3340 3341 3342 3343 3344 3345
epm_remove_low()
{
	[ -z "$1" ] && return
	case $PMTYPE in
		apt-rpm|yum-rpm|zypper-rpm|urpm-rpm|dnf-rpm)
			sudocmd rpm -ev $nodeps $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3346
		apt-dpkg|aptitude-dpkg)
3347
			sudocmd dpkg -P $(subst_option nodeps --force-all) $(print_name "$@")
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3348 3349 3350 3351
			return ;;
		pkgsrc)
			sudocmd pkg_delete -r $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3352 3353 3354
		pkgng)
			sudocmd pkg delete -R $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3355 3356 3357
		emerge)
			sudocmd emerge --unmerge $@
			return ;;
3358 3359 3360
		pacman)
			sudocmd pacman -R $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372
		slackpkg)
			sudocmd /sbin/removepkg $@
			return ;;
	esac
	return 1
}

epm_remove_names()
{
	[ -z "$1" ] && return

	case $PMTYPE in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3373
		apt-dpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3374 3375
			sudocmd apt-get remove --purge $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3376 3377 3378
		aptitude-dpkg)
			sudocmd aptitude purge $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3379 3380 3381
		apt-rpm)
			sudocmd apt-get remove $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3382 3383 3384 3385 3386 3387 3388 3389 3390
		deepsolver-rpm)
			sudocmd ds-remove $@
			return ;;
		urpm-rpm)
			sudocmd urpme $@
			return ;;
		pkgsrc) # without dependencies
			sudocmd pkg_delete $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3391 3392 3393
		pkgng)
			sudocmd pkg delete -R $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3394 3395 3396 3397 3398
		emerge)
			#sudocmd emerge --unmerge $@
			sudocmd emerge -aC $@
			return ;;
		pacman)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3399
			sudocmd pacman -Rc $@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3400 3401 3402 3403 3404 3405 3406
			return ;;
		yum-rpm)
			sudocmd yum remove $@
			return ;;
		dnf-rpm)
			sudocmd dnf remove $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3407 3408 3409
		snappy)
			sudocmd snappy uninstall $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3410 3411 3412 3413 3414 3415
		zypper-rpm)
			sudocmd zypper remove $@
			return ;;
		mpkg)
			sudocmd mpkg remove $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3416 3417 3418
		conary)
			sudocmd conary erase $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3419
		npackd)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3420 3421 3422 3423
			sudocmd npackdcl remove --package=$@
			return ;;
		nix)
			sudocmd nix-env --uninstall $@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3424
			return ;;
3425 3426 3427
		apk)
			sudocmd apk del $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3428 3429 3430
		guix)
			sudocmd guix package -r $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3431 3432 3433
		android)
			sudocmd pm uninstall $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3434
		chocolatey)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3435
			sudocmd chocolatey uninstall $@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3436 3437 3438 3439
			return ;;
		slackpkg)
			sudocmd /usr/sbin/slackpkg remove $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3440 3441 3442
		homebrew)
			sudocmd brew remove $@
			return ;;
3443 3444 3445
		aptcyg)
			sudocmd apt-cyg remove $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3446
		ipkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3447
			sudocmd ipkg $(subst_option force -force-depends) remove $@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3448
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3449
		*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3450
			fatal "Have no suitable command for $PMTYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3451 3452 3453 3454 3455 3456 3457
			;;
	esac
}

epm_remove_nonint()
{
	case $PMTYPE in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3458
		apt-dpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3459 3460
			sudocmd apt-get -y --force-yes remove --purge $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3461 3462 3463
		aptitude-dpkg)
			sudocmd aptitude -y purge $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3464 3465 3466
		apt-rpm)
			sudocmd apt-get -y --force-yes remove $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3467 3468 3469 3470
		urpm-rpm)
			sudocmd urpme --auto $@
			return ;;
		pacman)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3471
			sudocmd pacman -Rc --noconfirm $@
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3472 3473 3474 3475 3476 3477 3478 3479 3480 3481
			return ;;
		yum-rpm)
			sudocmd yum -y remove $@
			return ;;
		zypper-rpm)
			sudocmd zypper --non-interactive remove $@
			return ;;
		slackpkg)
			sudocmd /usr/sbin/slackpkg -batch=on -default_answer=yes remove $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3482 3483 3484
		pkgng)
			sudocmd pkg delete -y -R $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3485 3486 3487
		ipkg)
			sudocmd ipkg -force-defaults remove $@
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3488 3489 3490 3491 3492 3493 3494 3495 3496 3497
	esac
	return 5
}

epm_print_remove_command()
{
	case $PMTYPE in
		apt-rpm|yum-rpm|zypper-rpm|urpm-rpm|dnf-rpm)
			echo "rpm -ev $nodeps $@"
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3498
		apt-dpkg|aptitude-dpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3499 3500 3501 3502 3503
			echo "dpkg -P $@"
			;;
		pkgsrc)
			echo "pkg_delete -r $@"
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3504 3505 3506
		pkgng)
			echo "pkg delete -R $@"
			;;
3507 3508 3509
		pacman)
			echo "pacman -R $@"
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3510 3511 3512 3513 3514 3515
		emerge)
			echo "emerge --unmerge $@"
			;;
		slackpkg)
			echo "/sbin/removepkg $@"
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3516 3517 3518
		ipkg)
			echo "ipkg remove $@"
			;;
3519 3520 3521
		aptcyg)
			echo "apt-cyg remove $@"
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3522
		*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3523
			fatal "Have no suitable appropriate remove command for $PMTYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535
			;;
	esac
}


epm_remove()
{
	if [ -n "$show_command_only" ] ; then
		epm_print_remove_command $pkg_filenames
		return
	fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3536 3537
	# get full package name(s) from the package file(s)
	[ -n "$pkg_files" ] && pkg_names="$pkg_names $(epm query $pkg_files)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3538

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3539
	[ -n "$pkg_names" ] || fatal "Remove: missing package(s) name."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3540 3541 3542 3543
	epm_remove_low $pkg_names && return

	# get package name for hi level package management command (with version if supported and if possible)
	pkg_names=$(__epm_get_hilevel_name $pkg_names)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3544 3545

	if [ -n "$non_interactive" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3546
		epm_remove_nonint $pkg_names
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3547 3548 3549 3550 3551
		local RET=$?
		# if not separate command, use usual command
		[ "$RET" = "5" ] || return $RET
	fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3552
	epm_remove_names $pkg_names
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3553 3554 3555 3556 3557 3558 3559 3560 3561
}


# File bin/epm-removerepo:

epm_removerepo()
{
case $PMTYPE in
	apt-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3562
		assure_exists apt-repo
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3563
		sudocmd apt-repo rm "$quoted_args"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3564
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3565
	apt-dpkg|aptitude-dpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3566
		info "You need remove repo from /etc/apt/sources.list"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3567 3568
		;;
	yum-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3569 3570
		assure_exists yum-utils
		sudocmd yum-config-manager --disable "$pkg_filenames"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3571 3572
		;;
	urpm-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3573
		sudocmd urpmi.removemedia "$pkg_filenames"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3574 3575
		;;
	zypper-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3576
		sudocmd zypper removerepo "$pkg_filenames"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3577 3578
		;;
	emerge)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3579
		sudocmd layman "-d$pkg_filenames"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3580 3581
		;;
	pacman)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3582
		info "You need remove repo from /etc/pacman.conf"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3583 3584
		;;
	npackd)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3585
		sudocmd npackdcl remove-repo --url="$pkg_filenames"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3586 3587
		;;
	slackpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3588
		info "You need remove repo from /etc/slackpkg/mirrors"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3589 3590
		;;
	*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3591
		fatal "Have no suitable command for $PMTYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3592 3593 3594 3595 3596
		;;
esac

}

3597 3598 3599 3600 3601
# File bin/epm-repofix:


__fix_apt_sources_list()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3602
	local SUBST_ALT_RULE='s!^(.*)[/ ](ALTLinux|LINUX\@Etersoft)[/ ](Sisyphus|p8[/ ]branch|p7[/ ]branch|p6[/ ]branch)[/ ](x86_64|i586|x86_64-i586|noarch) !\1 \2/\3/\4 !gi'
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3603 3604
	local i
	assure_root
3605 3606
	for i in "$@" ; do
		[ -s "$i" ] || continue
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3607
		#perl -i.bak -pe "$SUBST_ALT_RULE" $i
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3608 3609 3610
		# TODO: only for uncommented strings
		#sed -i -r -e "$SUBST_ALT_RULE" $i
		regexp_subst "/^ *#/! $SUBST_ALT_RULE" $i
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3611 3612

		# add signs
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3613
		local br
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3614
		for br in $DISTRVERSION ; do
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3615 3616 3617
			regexp_subst "/ALTLinux\/$br\/branch/s/^rpm *([fhr])/rpm [$br] \1/" $i
			regexp_subst "/Etersoft\/$br\/branch/s/^rpm *([fhr])/rpm [etersoft] \1/" $i
		done
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3618
		regexp_subst "/ALTLinux\/Sisyphus\//s/^rpm *([fhr])/rpm [alt] \1/" $i
3619 3620 3621 3622 3623
	done
}

epm_repofix()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3624 3625 3626

[ -z "$pkg_filenames" ] || fatal "No arguments are allowed here"

3627 3628 3629
case $PMTYPE in
	apt-rpm)
		assure_exists apt-repo
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3630
		[ -n "$quiet" ] || docmd apt-repo list
3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649
		__fix_apt_sources_list /etc/apt/sources.list
		__fix_apt_sources_list /etc/apt/sources.list.d/*.list
		docmd apt-repo list

		# rebuild rpm database
		#sudocmd rm -fv /var/lib/rpm/__db*
		#sudocmd rpm --rebuilddb
		;;
	yum-rpm|dnf-rpm)
		sudocmd rm -fv /var/lib/rpm/__db*
		sudocmd rpm --rebuilddb
		;;
	*)
		fatal "Have no suitable command for $PMTYPE"
		;;
esac

}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666
# File bin/epm-repolist:

print_apt_sources_list()
{
    local i
    for i in $@ ; do
        test -r "$i" || continue
        #echo
        #echo "$i:"
        grep -v -- "^#" $i
    done | grep -v -- "^ *\$"
}

epm_repolist()
{
case $PMTYPE in
	apt-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3667
		assure_exists apt-repo
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3668 3669 3670 3671 3672
		docmd apt-repo list
		;;
	deepsolver-rpm)
		docmd ds-conf
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3673
	apt-dpkg|aptitude-dpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689
		showcmd cat /etc/apt/sources.list*
		print_apt_sources_list /etc/apt/sources.list /etc/apt/sources.list.d/*.list
		;;
	yum-rpm)
		docmd yum repolist
		;;
	dnf-rpm)
		docmd dnf repolist -v
		;;
	urpm-rpm)
		docmd urpmq --list-url
		;;
	zypper-rpm)
		docmd zypper sl -d
		;;
	emerge)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3690
		docmd eselect profile list
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3691 3692 3693 3694 3695 3696 3697 3698 3699
		docmd layman -L
		;;
	pacman)
		docmd grep -v -- "^#\|^$" /etc/pacman.conf
		;;
	slackpkg)
		docmd grep -v -- "^#\|^$" /etc/slackpkg/mirrors
		;;
	*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3700
		fatal "Have no suitable command for $PMTYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3701 3702 3703 3704 3705 3706 3707
		;;
esac

}

# File bin/epm-requires:

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3708 3709

epm_requires_files()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3710
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3711
	local pkg_files="$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3712
	[ -n "$pkg_files" ] || return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3713

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3714 3715 3716
	local PKGTYPE="$(get_package_type $pkg_files)"

	case "$PKGTYPE" in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3717
		rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3718
			assure_exists rpm
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3719
			docmd rpm -q --requires -p $pkg_files
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3720 3721
			;;
		deb)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3722
			assure_exists dpkg
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3723 3724 3725
			a= docmd dpkg -I $pkg_files | grep "^ *Depends:" | sed "s|^ *Depends:||g"
			;;
		*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3726
			fatal "Have no suitable command for $PKGTYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3727 3728 3729
			;;
	esac
}
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3730

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3731 3732
epm_requires_names()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3733
	local pkg_names="$@"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3734 3735
	local CMD
	[ -n "$pkg_names" ] || return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3736 3737

case $PMTYPE in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751
	apt-rpm)
		# FIXME: need fix for a few names case
		# FIXME: too low level of requires name (libSOME.so)
		if is_installed $pkg_names ; then
			CMD="rpm -q --requires"
		else
			#EXTRA_SHOWDOCMD=' | grep "Depends:"'
			#docmd apt-cache show $pkg_names | grep "Depends:"
			#return
			CMD="apt-cache depends"
		fi

		;;
	urpm-rpm|zypper-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3752 3753 3754 3755
		# FIXME: use hi level commands
		CMD="rpm -q --requires"
		;;
	yum-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767
		if is_installed $pkg_names ; then
			CMD="rpm -q --requires"
		else
			CMD="yum deplist"
		fi
		;;
	dnf-rpm)
		if is_installed $pkg_names ; then
			CMD="rpm -q --requires"
		else
			CMD="dnf repoquery --requires"
		fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3768
		;;
3769 3770 3771
	pacman)
		CMD="pactree"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3772
	apt-dpkg|aptitude-dpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784
		# FIXME: need fix for a few names case
		if is_installed $pkg_names ; then
			showcmd dpkg -s $pkg_names
			a= dpkg -s $pkg_names | grep "^Depends:" | sed "s|^Depends:||g"
			return
		else
			CMD="apt-cache depends"
		fi
		;;
	emerge)
		assure_exists equery
		CMD="equery depgraph"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3785
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3786 3787 3788 3789
	pkgng)
		#CMD="pkg rquery '%dn-%dv'"
		CMD="pkg info -d"
		;;
3790 3791 3792 3793 3794 3795
	aptcyg)
		#CMD="apt-cyg depends"
		# print show version
		docmd apt-cyg show $pkg_names | grep "^requires: " | sed "s|^requires: ||g"
		return
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3796
	*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3797
		fatal "Have no suitable command for $PMTYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3798 3799 3800 3801
		;;
esac


Vitaly Lipatov's avatar
Vitaly Lipatov committed
3802
docmd $CMD $pkg_names
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3803 3804 3805

}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3806 3807
epm_requires()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3808
	[ -n "$pkg_filenames" ] || fatal "Requires: missing package(s) name"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3809
	epm_requires_files $pkg_files
3810
	epm_requires_names $(print_name $pkg_names)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3811 3812
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3813 3814
# File bin/epm-search:

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3815

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3816
__epm_search_output()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3817
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3818 3819
local CMD
local string="$1"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3820 3821
case $PMTYPE in
	apt-rpm|apt-dpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3822
		CMD="apt-cache search --"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3823
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3824 3825 3826 3827 3828 3829
	aptitude-dpkg)
		CMD="aptitude search --"
		;;
	deepsolver-rpm)
		CMD="ds-require --"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3830
	urpm-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3831 3832
		# urpmq does not support --
		CMD="urpmq -y"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3833 3834
		;;
	pkgsrc)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3835
		CMD="pkg_info -x --"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3836
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3837 3838 3839
	pkgng)
		CMD="pkg search -i --"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3840
	emerge)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3841
		CMD="emerge --search --"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3842 3843
		;;
	pacman)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3844
		CMD="pacman -Ss --"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3845
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3846
	aura)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3847
		CMD="aura -As --"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3848
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3849
	yum-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3850
		CMD="yum search --"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3851 3852
		;;
	dnf-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3853
		CMD="dnf search --"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3854 3855
		;;
	zypper-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3856
		CMD="zypper search --"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3857 3858 3859 3860
		;;
	mpkg)
		CMD="mpkg search"
		;;
3861 3862 3863
	apk)
		CMD="apk search"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3864 3865 3866
	conary)
		CMD="conary repquery"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3867
	npackd)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3868
		docmd npackdcl search --query="$string" --status=all
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3869
		return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3870 3871 3872 3873 3874 3875
		;;
	chocolatey)
		CMD="chocolatey list"
		;;
	slackpkg)
		# FIXME
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3876
		echo "Note: case sensitive search"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3877
		CMD="/usr/sbin/slackpkg search"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3878
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3879 3880 3881
	homebrew)
		CMD="brew search"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3882 3883 3884
	guix)
		CMD="guix package -A"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3885 3886 3887
	android)
		CMD="pm list packages"
		;;
3888 3889 3890
	aptcyg)
		CMD="apt-cyg searchall"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3891
	*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3892
		fatal "Have no suitable search command for $PMTYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3893 3894 3895
		;;
esac

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3896
LANG=C docmd $CMD $string
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3897 3898 3899 3900 3901 3902 3903 3904 3905 3906
}

__epm_search_make_grep()
{
	local i
	[ -z "$*" ] && return

	local list=
	local listN=
	for i in $@ ; do
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3907 3908 3909 3910 3911 3912 3913 3914 3915
		case "$i" in
			^*)
				# will clean from ^ later (and have the bug here with empty arg if run with one ^ only)
				listN="$listN $i"
				;;
			*)
				list="$list $i"
				;;
		esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3916 3917 3918
	done

	#list=$(strip_spaces $list | sed -e "s/ /|/g")
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3919
	listN=$(strip_spaces $listN | sed -e "s/ /|/g" | sed -e "s/\^//g")
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3920

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3921 3922 3923 3924
	if [ "$short" ] ; then
		echon " | sed -e \"s| .*||g\""
	fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3925
	[ -n "$listN" ] && echon " | egrep -i -v -- \"$listN\""
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3926 3927 3928

	# FIXME: The World has not idea how to do grep both string
	# http://stackoverflow.com/questions/10110051/grep-with-two-strings-logical-and-in-regex?rq=1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3929

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3930 3931 3932 3933 3934 3935
	# Need only if we have more than one word (with one word we will grep for colorify)
	if [ "$(echo "$list" | wc -w)" -gt 1 ] ; then
		for i in $list ; do
			# FIXME -n on MacOS?
			echon " | egrep -i -- \"$i\""
		done
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3936 3937 3938 3939 3940
	fi

	# FIXME: move from it
	#isatty || return

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3941 3942
	# TODO: sorts word by length from large to short

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3943 3944 3945 3946 3947
	local COLO=""
	# rule for colorife
	for i in $list $listN; do
		[ -n "$COLO" ] && COLO="$COLO|"
		COLO="$COLO$i"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3948
	done
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3949

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3950
	# TODO: use some colorifer instead grep (check grep adove too)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3951
	if [ -n "$list" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3952
		echon " | egrep -i $EGREPCOLOR -- \"($COLO)\""
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3953
	fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3954 3955 3956 3957 3958
}


epm_search()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3959
	[ -n "$pkg_filenames" ] || fatal "Search: missing search argument(s)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3960

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3961
	# it is useful for first time running
3962
	update_repo_if_needed soft
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3963

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3964
	# FIXME: do it better
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3965 3966
	local MGS
	MGS=$(eval __epm_search_make_grep $quoted_args)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3967 3968
	EXTRA_SHOWDOCMD="$MGS"
	eval "__epm_search_output \"$(eval get_firstarg $quoted_args)\" $MGS"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3969 3970 3971 3972
}

# File bin/epm-search_file:

Vitaly Lipatov's avatar
Vitaly Lipatov committed
3973
__alt_local_content_search()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3974
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3975 3976

    local CI="$(get_local_alt_contents_index)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3977 3978 3979 3980 3981 3982

    #local OUTCMD="less"
    #[ -n "$USETTY" ] || OUTCMD="cat"
    OUTCMD="cat"

    {
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3983
        [ -n "$USETTY" ] && echo "Search in $CI for $1..."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3984 3985
        # note! tabulation below!
        grep -h -- ".*$1.*	" $CI | sed -e "s|\(.*\)\t\(.*\)|\2: \1|g"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3986 3987 3988 3989 3990 3991
    } | $OUTCMD
}

epm_search_file()
{
	local CMD
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3992
	[ -n "$pkg_filenames" ] || fatal "Search file: missing file name(s)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3993 3994 3995

case $PMTYPE in
	apt-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3996
		__alt_local_content_search $pkg_filenames
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3997
		return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3998
	apt-dpkg|aptitude-dpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3999
		assure_exists apt-file
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4000 4001 4002 4003
		sudocmd apt-file update
		docmd apt-file search $pkg_filenames
		return ;;
	yum-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4004 4005
		# TODO
		info "Search by full packages list does not realized"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4006 4007 4008
		CMD="yum provides"
		;;
	dnf-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4009 4010
		# TODO
		info "Search by full packages list does not realized"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024
		CMD="dnf provides"
		;;
	urpm-rpm)
		CMD="urpmf"
		;;
	zypper-rpm)
		CMD="zypper wp vi"
		;;
	pacman)
		CMD="pacman -Qo"
		;;
	slackpkg)
		CMD="/usr/sbin/slackpkg file-search"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4025 4026 4027
	ipkg)
		CMD="ipkg search"
		;;
4028 4029 4030 4031
	aptcyg)
		docmd apt-cyg searchall $(echo " $pkg_filenames" | sed -e "s| /| |g")
		return
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4032
	*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4033
		fatal "Have no suitable search file command for $PMTYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4034 4035 4036 4037 4038 4039 4040
		;;
esac

docmd $CMD $pkg_filenames

}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056
# File bin/epm-sh-altlinux:

get_local_alt_contents_index()
{

    epm_repolist | grep "rpm.*file:/" | sed -e "s|^rpm.*file:||g" | while read URL ARCH other ; do
        test -d "$URL/$ARCH" || continue # fatal "Local mirror is not accessible via $URL/$ARCH"
        FILE="$URL/$ARCH/base/contents_index"
        if [ -r "$FILE" ] ; then
            echo "$FILE"
        else
            info "TODO for girar server: There is no $(basename $FILE) file in $(dirname $FILE)"
        fi
    done
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
4057 4058 4059 4060 4061 4062 4063 4064
# File bin/epm-simulate:


__use_zypper_dry_run()
{
    a= zypper install --help 2>&1 | grep -q -- "--dry-run" && echo "--dry-run"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
4065 4066 4067 4068 4069
__use_yum_assumeno()
{
    a= yum --help 2>&1 | grep -q -- "--assumeno"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
4070 4071 4072 4073 4074

__check_yum_result()
{
    grep "^No package" $1 && return 1
    grep "^Complete!" $1 && return 0
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4075 4076 4077
    grep "Exiting on user [Cc]ommand" $1 && return 0
    # dnf issue
    grep "^Operation aborted." $1 && return 0
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4078 4079 4080 4081
    # return default result by default
    return $2
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
4082 4083 4084 4085 4086 4087 4088 4089 4090
__check_pacman_result()
{
    grep "^error: target not found:" $1 && return 1
    grep "^Total Installed Size:" $1 && return 0
    grep "^Total Download Size:" $1 && return 0
    # return default result by default
    return $2
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
4091

Vitaly Lipatov's avatar
Vitaly Lipatov committed
4092 4093
_epm_do_simulate()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4094
    local CMD
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4095
    local RES=0
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4096
    local filenames="$*"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4097 4098 4099 4100 4101

    case $PMTYPE in
    	apt-rpm|apt-dpkg)
    		CMD="apt-get --simulate install"
    		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4102 4103 4104
    	aptitude-dpkg)
    		CMD="aptitude -s install"
    		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4105
    	yum-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4106
    		if __use_yum_assumeno ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4107 4108
    			LC_ALL=C store_output sudocmd yum --assumeno install $filenames
    			__check_yum_result $RC_STDOUT $?
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4109
    		else
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4110 4111 4112
    			LC_ALL=C store_output sudocmd yum install $filenames <<EOF
n
EOF
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4113
    			__check_yum_result $RC_STDOUT $?
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4114
    		fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4115 4116 4117
    		RES=$?
    		clean_store_output
    		return $RES ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4118 4119 4120 4121 4122 4123
    	dnf-rpm)
    		LC_ALL=C store_output sudocmd dnf --assumeno install $filenames
    		__check_yum_result $RC_STDOUT $?
    		RES=$?
    		clean_store_output
    		return $RES ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4124 4125 4126 4127 4128
    	urpm-rpm)
    		CMD="urpmi --test --auto"
    		;;
    	zypper-rpm)
    		if ! __use_zypper_dry_run >/dev/null ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4129
    			fatal "zypper is too old: does not support --dry-run"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4130
    		fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4131
    		CMD="zypper --non-interactive install --dry-run"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4132 4133
    		;;
    	emerge)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4134 4135 4136 4137 4138 4139 4140 4141
    		local res=0
    		for pkg in $filenames ; do
			is_installed $pkg && continue
			docmd emerge --pretend $pkg && continue
			pkg=1
			break
    		done
    		return $res ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4142
    	pacman)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4143 4144 4145 4146 4147 4148 4149
    		LC_ALL=C store_output sudocmd pacman -v -S $filenames <<EOF
no
EOF
    		__check_pacman_result $RC_STDOUT $?
    		RES=$?
    		clean_store_output
    		return $RES ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4150 4151 4152 4153 4154 4155 4156
    	slackpkg)
    		#docmd /usr/sbin/slackpkg -batch=on -default_answer=yes download
    		# just try search every package
    		# FIXME: epm_search have to return false status code if the package does not found
    		local pkg res
    		res=0
    		for pkg in $filenames ; do
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4157 4158
    			# FIXME: -[0-0] does not work in search!
    			# FIXME: we need strict search here (not find gst-plugins-base if search for gst-plugins
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4159 4160 4161
    			pkg_filenames="$pkg-[0-9]" epm_search | grep -E "(installed|upgrade)" && continue
    			pkg_filenames="$pkg" epm_search | grep -E "(installed|upgrade)" && continue
    			res=1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4162
    			info "Package '$pkg' does not found in repository."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4163 4164 4165
    		done
    		return $res ;;
    	*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4166
    		fatal "Have no suitable simulate command for $PMTYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4167 4168 4169 4170 4171 4172 4173 4174
    		;;
    esac

    sudocmd $CMD $filenames
}

epm_simulate()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4175
    [ -z "$pkg_filenames" ] && info "Simulate: Skip empty list" && return 22
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4176 4177 4178

    local filenames="$(echo $pkg_filenames | filter_out_installed_packages)"

Vitaly Lipatov's avatar
Vitaly Lipatov committed
4179
    [ -z "$filenames" ] && info "Simulate: All packages are already installed" && return 0
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4180 4181 4182 4183

    _epm_do_simulate $filenames
    local RES=$?
    if [ -z "$quiet" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4184
        [ "$RES" = 0 ] && info "Simulate result: $filenames package(s) CAN BE installed" || info "Simulate result: There are PROBLEMS with install some package(s)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4185 4186 4187 4188 4189
    fi
    return $RES
}


4190 4191 4192
# File bin/epm-site:


Vitaly Lipatov's avatar
Vitaly Lipatov committed
4193 4194
PAOURL="https://packages.altlinux.org"

4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215
run_command_if_exists()
{
	local CMD="$1"
	shift
	if which "$CMD" 2>/dev/null >/dev/null ; then
		docmd "$CMD" "$@"
		return 0
	fi
	return 1
}

open_browser()
{
	local i
	for i in xdg-open firefox chromium links ; do
		run_command_if_exists $i "$@" && return
	done
}

__query_package_hl_url()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4216
	local PAOAPI="$PAOURL/api"
4217 4218 4219 4220
	case $DISTRNAME in
		ALTLinux)
			# http://petstore.swagger.io/?url=http://packages.altlinux.org/api/docs
			epm assure curl || return 1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4221 4222
			showcmd curl "$PAOAPI/srpms/$1"
			curl -s --header "Accept: application/json" "$PAOAPI/srpms/$1" | grep '"url"' | sed -e 's|.*"url":"||g' | sed -e 's|".*||g'
4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262
			return 0
			;;
	esac
	return 1
}

query_package_url()
{
	local URL

	case $PMTYPE in
		*-rpm)
			# TODO: for binary packages?
			query_package_field URL "$1" || __query_package_hl_url "$1"
			#LANG=C epm info "$1"
			return
			;;
	esac
	fatal "rpm based distro supported only. TODO: Realize via web service?"
}

get_locale()
{
	local loc
	loc=$(a= natspec --locale 2>/dev/null)
	[ -n "$loc" ] || loc=$LANG
	echo $loc
}

get_pao_url()
{
	local loc
	loc=$(get_locale | cut -c1-2)
	case $loc in
		en|ru|uk|br)
			loc=$loc
			;;
		*)
			loc=en
	esac
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4263
	echo "$PAOURL/$loc/Sisyphus/srpms"
4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284
}

query_altlinux_url()
{
	local URL
	case $PMTYPE in
		*-rpm)
			local srpm=$(print_srcname "$1")
			[ -n "$srpm" ] || fatal "Can't get source name for $1"
			echo "$(get_pao_url)/$srpm"
			return
			;;
	esac
	fatal "rpm based distro supported only. TODO: Realize via web service?"
}

epm_site()
{

[ -n "$pkg_filenames" ] || fatal "Info: missing package(s) name"

Vitaly Lipatov's avatar
Vitaly Lipatov committed
4285
local PAO=""
4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300
for f in $pkg_names $pkg_files ; do
	[ "$f" = "-p" ] && PAO="$f" && continue
	if [ -n "$PAO" ] ; then
		pkg_url=$(query_altlinux_url $f)
	else
		pkg_url=$(query_package_url $f)
	fi
	[ -n "$pkg_url" ] && open_browser "$pkg_url" && continue
	warning "Can't get URL for $f package"
done



}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
4301 4302 4303 4304 4305
# File bin/epm-update:


epm_update()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4306
	[ -z "$pkg_filenames" ] || fatal "No arguments are allowed here"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4307
	info "Running command for update remote package repository database"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318

case $PMTYPE in
	apt-rpm)
		sudocmd apt-get update || exit
		#sudocmd apt-get -f install || exit
		;;
	apt-dpkg)
		sudocmd apt-get update || exit
		#sudocmd apt-get -f install || exit
		#sudocmd apt-get autoremove
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4319 4320 4321
	#snappy)
	#	sudocmd snappy
	#	;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4322 4323 4324
	aptitude-dpkg)
		sudocmd aptitude update || exit
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4325
	yum-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4326
		info "update command is stubbed for yum"
4327
		# yum makecache
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4328 4329 4330 4331
		#sudocmd yum check-update
		;;
	dnf-rpm)
		info "update command is stubbed for dnf"
4332
		# dnf makecache
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4333
		#sudocmd dnf check-update
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4334 4335 4336 4337 4338 4339 4340
		;;
	urpm-rpm)
		sudocmd urpmi.update -a
		;;
	pacman)
		sudocmd pacman -S -y
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4341 4342 4343
	aura)
		sudocmd aura -A -y
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4344 4345 4346 4347 4348 4349 4350
	zypper-rpm)
		sudocmd zypper refresh
		;;
	emerge)
		sudocmd emerge --sync
		;;
	slackpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4351
		sudocmd /usr/sbin/slackpkg -batch=on update
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4352 4353 4354 4355
		;;
	deepsolver-rpm)
		sudocmd ds-update
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4356 4357 4358
	npackd)
		sudocmd packdcl detect # get packages from MSI database
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4359 4360 4361 4362 4363 4364
	homebrew)
		sudocmd brew update
		;;
	ipkg)
		sudocmd ipkg update
		;;
4365 4366 4367
	apk)
		sudocmd apk update
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4368 4369 4370 4371
	pkgsrc)
		# portsnap extract for the first time?
		sudocmd portsnap fetch update
		;;
4372 4373 4374
	aptcyg)
		sudocmd apt-cyg update
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4375
	*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4376
		fatal "Have no suitable update command for $PMTYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4377 4378 4379 4380 4381 4382 4383
		;;
esac

}

# File bin/epm-upgrade:

Vitaly Lipatov's avatar
Vitaly Lipatov committed
4384

Vitaly Lipatov's avatar
Vitaly Lipatov committed
4385 4386 4387
epm_upgrade()
{
	local CMD
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4388

Vitaly Lipatov's avatar
Vitaly Lipatov committed
4389 4390
	[ -z "$pkg_filenames" ] || fatal "No arguments are allowed here"

Vitaly Lipatov's avatar
Vitaly Lipatov committed
4391 4392 4393
	# it is useful for first time running
	update_repo_if_needed

Vitaly Lipatov's avatar
Vitaly Lipatov committed
4394
	info "Running command for upgrade packages"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4395 4396 4397

	case $PMTYPE in
	apt-rpm|apt-dpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4398 4399
		# non_interactive
		# Функцию добавления параметра при условии
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4400 4401
		CMD="apt-get dist-upgrade"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4402 4403 4404
	aptitude-dpkg)
		CMD="aptitude dist-upgrade"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4405
	yum-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4406
		# can do update repobase automagically
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4407 4408 4409 4410 4411
		CMD="yum update"
		;;
	dnf-rpm)
		CMD="dnf update"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4412 4413 4414
	snappy)
		CMD="snappy update"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4415 4416 4417 4418 4419 4420 4421 4422
	urpm-rpm)
		# or --auto-select --replace-files
		CMD="urpmi --auto-update"
		;;
	zypper-rpm)
		CMD="zypper dist-upgrade"
		;;
	pacman)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4423 4424 4425 4426
		CMD="pacman -S -u $force"
		;;
	aura)
		CMD="aura -A -u"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4427 4428 4429 4430
		;;
	emerge)
		CMD="emerge -NuDa world"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4431 4432 4433
	conary)
		CMD="conary updateall"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4434 4435 4436
	pkgsrc)
		CMD="freebsd-update fetch install"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4437 4438 4439
	pkgng)
		CMD="pkg upgrade"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4440 4441 4442
	chocolatey)
		CMD="chocolatey update all"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4443
	homebrew)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4444 4445 4446
		#CMD="brew upgrade"
		sudocmd "brew upgrade `brew outdated`"
		return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4447 4448 4449 4450
		;;
	ipkg)
		CMD="ipkg upgrade"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4451 4452 4453
	slackpkg)
		CMD="/usr/sbin/slackpkg upgrade-all"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4454 4455 4456
	guix)
		CMD="guix package -u"
		;;
4457 4458 4459 4460
	aptcyg)
		docmd_foreach "epm install" $(short=1 epm packages)
		return
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4461
	*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4462
		fatal "Have no suitable command for $PMTYPE"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477
		;;
	esac

	sudocmd $CMD $pkg_filenames
}

# File bin/epm-Upgrade:


epm_Upgrade()
{
	case $PMTYPE in
		yum-rpm)
			;;
		*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4478
			pkg_filenames= epm_update || return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4479 4480 4481
			;;
	esac

Vitaly Lipatov's avatar
Vitaly Lipatov committed
4482 4483 4484 4485 4486
	epm_upgrade
}

# File bin/epm-whatdepends:

4487

Vitaly Lipatov's avatar
Vitaly Lipatov committed
4488 4489 4490
epm_whatdepends()
{
	local CMD
4491 4492 4493
	[ -n "$pkg_files" ] && fatal "whatdepends do not handle files"
	[ -n "$pkg_names" ] || fatal "whatdepends: missing package(s) name"
	local pkg=$(print_name $pkg_names)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4494 4495

case $PMTYPE in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4496
	apt-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4497 4498
		CMD="apt-cache whatdepends"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4499
	apt-dpkg|aptitude-dpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4500 4501
		CMD="apt-cache rdepends"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4502 4503 4504
	aptitude-dpkg)
		CMD="aptitude why"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4505 4506 4507
	yum-rpm)
		CMD="repoquery --whatrequires"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4508 4509 4510
	dnf-rpm)
		CMD="repoquery --whatrequires"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4511
	emerge)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4512
		assure_exists equery
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4513 4514
		CMD="equery depends -a"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4515 4516 4517
	pkgng)
		CMD="pkg info -r"
		;;
4518 4519 4520
	aptcyg)
		CMD="apt-cyg rdepends"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4521 4522 4523 4524 4525
	*)
		fatal "Have no suitable command for $PMTYPE"
		;;
esac

4526
docmd $CMD $pkg
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4527 4528 4529 4530 4531

}

# File bin/epm-whatprovides:

4532

Vitaly Lipatov's avatar
Vitaly Lipatov committed
4533 4534 4535
epm_whatprovides()
{
	local CMD
4536 4537 4538
	[ -n "$pkg_files" ] && fatal "whatprovides does not handle files"
	[ -n "$pkg_names" ] || fatal "whatprovides: missing package(s) name"
	local pkg=$(print_name $pkg_names)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4539 4540 4541 4542 4543

case $PMTYPE in
	conary)
		CMD="conary repquery --what-provides"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4544
	apt-rpm|apt-dpkg|aptitude-dpkg)
4545
		LANG=C docmd apt-get install --print-uris $pkg | grep "^Selecting" | cut -f2 -d" "
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4546 4547 4548 4549 4550
		return
		;;
	yum-rpm)
		CMD="yum whatprovides"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4551 4552 4553
	dnf-rpm)
		CMD="yum provides"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4554 4555 4556
	zypper-rpm)
		CMD="zypper what-provides"
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4557 4558 4559 4560 4561
	*)
		fatal "Have no suitable command for $PMTYPE"
		;;
esac

4562
docmd $CMD $pkg
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4563

Vitaly Lipatov's avatar
Vitaly Lipatov committed
4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615
}
internal_distr_info()
{
#!/bin/sh
# Author: Vitaly Lipatov <lav@etersoft.ru>
# 2007, 2009, 2010, 2012 (c) Etersoft
# 2007 Public domain

# Detect the distro and version
# Welcome to send updates!

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

# Check for DISTRO specific file in /etc
distro()
{
	#[ -n "$ROOTDIR" ] || return
	# fill global DISTROFILE
	DISTROFILE="$ROOTDIR/etc/$1"
	[ -f "$DISTROFILE" ]
}

# Has a distro file the specified word?
has()
{
	[ -n "$DISTROFILE" ] || exit 1
	grep "$*" "$DISTROFILE" >/dev/null 2>&1
}

# Translate DISTRIB_ID to vendor name (like %_vendor does)
rpmvendor()
{
	[ "$DISTRIB_ID" = "ALTLinux" ] && echo "alt" && return
	[ "$DISTRIB_ID" = "LinuxXP" ] && echo "lxp" && return
	echo "$DISTRIB_ID" | tr "[A-Z]" "[a-z]"
}

# Translate DISTRIB_ID name to package manner (like in the package release name)
pkgvendor()
{
	[ "$DISTRIB_ID" = "Mandriva" ] && echo "mdv" && return
	rpmvendor
}

# Print pkgtype (need DISTRIB_ID var)
pkgtype()
{
    case `pkgvendor` in
		freebsd) echo "tbz" ;;
		sunos) echo "pkg.gz" ;;
		slackware|mopslinux) echo "tgz" ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4616
		archlinux) echo "pkg.tar.xz" ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4617 4618
		gentoo) echo "tbz2" ;;
		windows) echo "exe" ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4619
		android) echo "apk" ;;
4620
		alpine) echo "apk" ;;
4621 4622
		cygwin) echo "tar.xz" ;;
		debian|ubuntu|mint|runtu|mcst) echo "deb" ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655
		alt|asplinux|suse|mandriva|rosa|mandrake|pclinux|sled|sles)
			echo "rpm" ;;
		fedora|redhat|scientific|centos|rhel)
			echo "rpm" ;;
		*)  echo "rpm" ;;
	esac
}

get_var()
{
	grep -i "^$1 *=" | head -n 1 | sed -e "s/^[^=]*[ \t]*=[ \t]*//"

}

# 2010.1 -> 2010
get_major_version()
{
	echo "$1" | sed -e "s/\..*//g"
}

# Default values
DISTRIB_ID="Generic"
DISTRIB_RELEASE=""

# Default with LSB
if distro lsb-release ; then
	DISTRIB_ID=`cat $DISTROFILE | get_var DISTRIB_ID`
	DISTRIB_RELEASE=`cat $DISTROFILE | get_var DISTRIB_RELEASE`
fi

# ALT Linux based
if distro altlinux-release ; then
	DISTRIB_ID="ALTLinux"
4656
	if has Sisyphus ; then DISTRIB_RELEASE="Sisyphus"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4657 4658
	elif has "ALT Linux 7." ; then DISTRIB_RELEASE="p7"
	elif has "ALT Linux 8." ; then DISTRIB_RELEASE="p8"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4659
	elif has "Simply Linux 6." ; then DISTRIB_RELEASE="p6"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4660 4661
	elif has "Simply Linux 7." ; then DISTRIB_RELEASE="p7"
	elif has "Simply Linux 8." ; then DISTRIB_RELEASE="p8"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4662
	elif has "ALT Linux 6." ; then DISTRIB_RELEASE="p6"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4663
	elif has "ALT Linux p8"  ; then DISTRIB_RELEASE="p8"
4664 4665 4666 4667 4668
	elif has "ALT Linux p7"  ; then DISTRIB_RELEASE="p7"
	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"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4669
	elif has "ALT Linux 4.1" ; then DISTRIB_RELEASE="4.1"
4670 4671 4672 4673 4674 4675
	elif has "ALT Linux 4.0" ; then DISTRIB_RELEASE="4.0"
	elif has Walnut          ; then DISTRIB_RELEASE="4.0"
	elif has 20070810 ; then DISTRIB_RELEASE="4.0"
	elif has Ajuga    ; then DISTRIB_RELEASE="4.0"
	elif has 20050723 ; then DISTRIB_RELEASE="3.0"
	elif has Citron   ; then DISTRIB_RELEASE="2.4"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4676 4677
	fi

4678 4679 4680 4681
elif [ `uname -o` = "Cygwin" ] ; then
        DISTRIB_ID="Cygwin"
        DISTRIB_RELEASE="all"

Vitaly Lipatov's avatar
Vitaly Lipatov committed
4682 4683
elif distro gentoo-release ; then
	DISTRIB_ID="Gentoo"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4684 4685 4686
	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)`
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700

# Slackware based
elif distro mopslinux-version ; then
	DISTRIB_ID="MOPSLinux"
	if   has 4.0 ; then DISTRIB_RELEASE="4.0"
	elif has 5.0 ; then DISTRIB_RELEASE="5.0"
	elif has 5.1 ; then DISTRIB_RELEASE="5.1"
	elif has 6.0 ; then DISTRIB_RELEASE="6.0"
	elif has 6.1 ; then DISTRIB_RELEASE="6.1"
	fi
elif distro slackware-version ; then
	DISTRIB_ID="Slackware"
	DISTRIB_RELEASE="$(grep -Eo [0-9]+\.[0-9]+ $DISTROFILE)"

Vitaly Lipatov's avatar
Vitaly Lipatov committed
4701 4702
elif distro os-release && which apk 2>/dev/null >/dev/null ; then
	. $ROOTDIR/etc/os-release
4703 4704 4705
	DISTRIB_ID="$ID"
	DISTRIB_RELEASE="$VERSION_ID"

Vitaly Lipatov's avatar
Vitaly Lipatov committed
4706 4707 4708 4709 4710 4711 4712
elif distro arch-release ; then
	DISTRIB_ID="ArchLinux"
	DISTRIB_RELEASE="2010"
	if grep 2011 -q $ROOTDIR/etc/pacman.d/mirrorlist ; then
		DISTRIB_RELEASE="2011"
	fi

4713 4714 4715 4716
elif distro mcst_version ; then
	DISTRIB_ID="MCST"
	DISTRIB_RELEASE=$(cat "$DISTROFILE" | grep "release" | sed -e "s|.*release \([0-9]*\).*|\1|g")

Vitaly Lipatov's avatar
Vitaly Lipatov committed
4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825
# for Ubuntu use standard LSB info
elif [ "$DISTRIB_ID" = "Ubuntu" ] && [ -n "$DISTRIB_RELEASE" ]; then
	# use LSB version
	true

# Debian based
elif distro debian_version ; then
	DISTRIB_ID="Debian"
	DISTRIB_RELEASE=`cat $DISTROFILE`


# Mandriva based
elif distro pclinuxos-release ; then
	DISTRIB_ID="PCLinux"
	if   has "2007" ; then DISTRIB_RELEASE="2007"
	elif has "2008" ; then DISTRIB_RELEASE="2008"
	elif has "2010" ; then DISTRIB_RELEASE="2010"
	fi

elif distro mandriva-release || distro mandrake-release ; then
	DISTRIB_ID="Mandriva"
	if   has 2005 ; then DISTRIB_RELEASE="2005"
	elif has 2006 ; then DISTRIB_RELEASE="2006"
	elif has 2007 ; then DISTRIB_RELEASE="2007"
	elif has 2008 ; then DISTRIB_RELEASE="2008"
	elif has 2009.0 ; then DISTRIB_RELEASE="2009.0"
	elif has 2009.1 ; then DISTRIB_RELEASE="2009.1"
	else
		# use /etc/lsb-release info by default
		if has ROSA ; then
			DISTRIB_ID="ROSA"
		fi
	fi

# Fedora based
elif distro linux-xp-release || distro lxp-release; then
	DISTRIB_ID="LinuxXP"
	if has "Attack of the Clones" ; then DISTRIB_RELEASE="2006"
	elif has "2007" ; then DISTRIB_RELEASE="2007"
	elif has "2008" ; then DISTRIB_RELEASE="2008"
	elif has "2009" ; then DISTRIB_RELEASE="2009"
	fi

elif distro asplinux-release ; then
	DISTRIB_ID="ASPLinux"
	if   has Karelia ; then DISTRIB_RELEASE="10"
	elif has Seliger ; then DISTRIB_RELEASE="11"
	elif has "11.1" ; then DISTRIB_RELEASE="11.1"
	elif has Ladoga ; then DISTRIB_RELEASE="11.2"
	elif has "11.2" ; then DISTRIB_RELEASE="11.2"
	elif has "12" ; then DISTRIB_RELEASE="12"
	elif has "13" ; then DISTRIB_RELEASE="13"
	elif has "14" ; then DISTRIB_RELEASE="14"
	elif has "15" ; then DISTRIB_RELEASE="15"
	fi

elif distro MCBC-release ; then
	DISTRIB_ID="MCBC"
	if   has 3.0 ; then DISTRIB_RELEASE="3.0"
	elif has 3.1 ; then DISTRIB_RELEASE="3.1"
	fi

elif distro fedora-release ; then
	DISTRIB_ID="Fedora"
	DISTRIB_RELEASE=$(cat "$DISTROFILE" | grep "release" | sed -e "s|.*release \([0-9]*\).*|\1|g")

elif distro redhat-release ; then
	# FIXME if need
	# actually in the original RHEL: Red Hat Enterprise Linux .. release N
	DISTRIB_ID="RHEL"
	if has CentOS ; then
		DISTRIB_ID="CentOS"
	elif has Scientific ; then
		DISTRIB_ID="Scientific"
	fi
	if has Beryllium ; then
		DISTRIB_ID="Scientific"
		DISTRIB_RELEASE="4.1"
	elif has Shrike ; then
		DISTRIB_ID="RedHat"
		DISTRIB_RELEASE="9"
	elif has Taroon ; then 	DISTRIB_RELEASE="3"
	elif has "release 4" ; then DISTRIB_RELEASE="4"
	elif has "release 5" ; then DISTRIB_RELEASE="5"
	elif has "release 6" ; then DISTRIB_RELEASE="6"
	elif has "release 7" ; then DISTRIB_RELEASE="7"
	fi

# SUSE based
elif distro SuSe-release || distro SuSE-release ; then
	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

# fixme: can we detect by some file?
elif [ `uname` = "FreeBSD" ] ; then
	DISTRIB_ID="FreeBSD"
	UNAME=$(uname -r)
	DISTRIB_RELEASE=$(echo "$UNAME" | grep RELEASE | sed -e "s|\([0-9]\.[0-9]\)-RELEASE|\1|g")

# fixme: can we detect by some file?
elif [ `uname` = "SunOS" ] ; then
	DISTRIB_ID="SunOS"
	DISTRIB_RELEASE=$(uname -r)

Vitaly Lipatov's avatar
Vitaly Lipatov committed
4826 4827 4828 4829 4830 4831 4832 4833 4834 4835
# fixme: can we detect by some file?
elif [ `uname` = "Darwin" ] ; then
	DISTRIB_ID="MacOS"
	DISTRIB_RELEASE=$(uname -r)

# fixme: move to up
elif [ `uname` = "Linux" ] && which guix 2>/dev/null >/dev/null ; then
	DISTRIB_ID="GNU/Linux/Guix"
	DISTRIB_RELEASE=$(uname -r)

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

Vitaly Lipatov's avatar
Vitaly Lipatov committed
4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898
# try use standart LSB info by default
elif distro lsb-release && [ -n "$DISTRIB_RELEASE" ]; then
	# use LSB
	true
fi

case $1 in
	-p)
		# override DISTRIB_ID
		test -n "$2" && DISTRIB_ID="$2"
		pkgtype
		exit 0
		;;
	-h)
		echo "distr_vendor - system name and version detection"
		echo "Usage: distr_vendor [options] [args]"
		echo "-p [SystemName] - print type of packaging system"
		echo "-d - print distro name"
		echo "-v - print version of distro"
		echo "-e - print full name of distro with version (by default)"
		echo "-s [SystemName] - print name of distro for build system (like in the package release name)"
		echo "-n [SystemName] - print vendor name (as _vendor macros in rpm)"
		echo "-V - print the version of $0"
		echo "-h - this help"
		exit 0
		;;
	-d)
		echo $DISTRIB_ID
		;;
	-v)
		echo $DISTRIB_RELEASE
		;;
	-s)
		# override DISTRIB_ID
		test -n "$2" && DISTRIB_ID="$2"
		pkgvendor
		exit 0
		;;
	-n)
		# override DISTRIB_ID
		test -n "$2" && DISTRIB_ID="$2"
		rpmvendor
		exit 0
		;;
	-V)
		echo "20120519"
		exit 0
		;;
	*)
		# if run without args, just printout Name/Version of the current system
		[ -n "$DISTRIB_RELEASE" ] && echo $DISTRIB_ID/$DISTRIB_RELEASE || echo $DISTRIB_ID
		;;
esac

}

#PATH=$PATH:/sbin:/usr/sbin

4899 4900
set_pm_type

Vitaly Lipatov's avatar
Vitaly Lipatov committed
4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920
set_sudo

check_tty

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

phelp()
{
	echo "$Descr
$Usage
 Commands:
$(get_help HELPCMD)

 Options:
$(get_help HELPOPT)
"
}

print_version()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4921
        echo "EPM package manager version 1.7.6"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4922
        echo "Running on $($DISTRVENDOR) ('$PMTYPE' package manager uses '$PKGFORMAT' package format)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4923
        echo "Copyright (c) Etersoft 2012-2016"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4924 4925 4926
        echo "This program may be freely redistributed under the terms of the GNU AGPLv3."
}

4927

Vitaly Lipatov's avatar
Vitaly Lipatov committed
4928 4929 4930 4931 4932 4933 4934 4935 4936
Usage="Usage: epm [options] <command> [package name(s), package files]..."
Descr="epm - EPM package manager"


verbose=
quiet=
nodeps=
force=
short=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4937
sort=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4938 4939 4940 4941 4942
non_interactive=
skip_installed=
show_command_only=
epm_cmd=
pkg_files=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4943
pkg_dirs=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4944
pkg_names=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4945
pkg_urls=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4946
quoted_args=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4947 4948

progname="${0##*/}"
4949

Vitaly Lipatov's avatar
Vitaly Lipatov committed
4950 4951 4952 4953
case $progname in
    epmi)
        epm_cmd=install
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4954 4955 4956
    epmI)
        epm_cmd=Install
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4957 4958 4959 4960 4961 4962 4963 4964 4965
    epme)
        epm_cmd=remove
        ;;
    epmcl)
        epm_cmd=changelog
        ;;
    epms)
        epm_cmd=search
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4966 4967 4968
    epmsf)
        epm_cmd=search_file
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983
    epmq)
        epm_cmd=query
        ;;
    epmqi)
        epm_cmd=info
        ;;
    epmqf)
        epm_cmd=query_file
        ;;
    epmqa)
        epm_cmd=packages
        ;;
    epmqp)
        epm_cmd=query_package
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4984 4985 4986
    epmql)
        epm_cmd=filelist
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4987 4988 4989
    epmu)
        epm_cmd=update
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004
    epm|upm|eepm)
        ;;
    *)
        # epm by default
        # fatal "Unknown command: $progname"
        ;;
esac

check_command()
{
    # do not override command
    [ -z "$epm_cmd" ] || return

# Base commands
    case $1 in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5005
    -i|install|add|i)         # HELPCMD: install package(s) from remote repositories or from local file
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5006 5007
        epm_cmd=install
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5008
    -e|-P|remove|delete|uninstall|erase|e)  # HELPCMD: remove (delete) package(s) from the database and the system
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5009 5010
        epm_cmd=remove
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5011
    -s|search|s)                # HELPCMD: search in remote package repositories
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5012 5013
        epm_cmd=search
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5014
    -qp|qp|query_package)     # HELPCMD: search in the list of installed packages
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5015 5016
        epm_cmd=query_package
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5017
    -qf|qf|-S|which|belongs)     # HELPCMD: query package(s) owning file
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5018 5019 5020 5021
        epm_cmd=query_file
        ;;

# Useful commands
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5022
    reinstall)                # HELPCMD: reinstall package(s) from remote repositories or from local file
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5023 5024
        epm_cmd=reinstall
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5025
    Install)                  # HELPCMD: perform update package repo info and install package(s) via install command
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5026 5027
        epm_cmd=Install
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5028
    -q|q|installed|query)     # HELPCMD: check presence of package(s) and print this name (also --short is supported)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5029 5030
        epm_cmd=query
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5031
    -sf|sf|filesearch)        # HELPCMD: search in which package a file is included
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5032 5033
        epm_cmd=search_file
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5034
    -ql|ql|filelist)          # HELPCMD: print package file list
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5035 5036
        epm_cmd=filelist
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5037
    check|fix|verify)         # HELPCMD: check local package base integrity and fix it
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5038 5039
        epm_cmd=check
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5040
    changelog|cl|-cl)         # HELPCMD: show changelog for package
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5041 5042
        epm_cmd=changelog
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5043
    -qi|qi|info|show)         # HELPCMD: print package detail info
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5044 5045
        epm_cmd=info
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5046
    requires|deplist|req)     # HELPCMD: print package requires
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5047 5048
        epm_cmd=requires
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5049
    provides|prov)            # HELPCMD: print package provides
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5050 5051
        epm_cmd=provides
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5052
    whatdepends)              # HELPCMD: print packages dependences on that
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5053 5054
        epm_cmd=whatdepends
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5055
    whatprovides)             # HELPCMD: print packages provides that target
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5056 5057
        epm_cmd=whatprovides
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5058 5059 5060
    conflicts)                # HELPCMD: print package conflicts
        epm_cmd=conflicts
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5061
    -qa|list|packages|-l|qa)  # HELPCMD: print list of installed package(s)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5062 5063
        epm_cmd=packages
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5064
    programs)                 # HELPCMD: print list of installed GUI program(s)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5065 5066
        epm_cmd=programs
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5067 5068 5069
    assure)                   # HELPCMD: <command> [package]: install package if command does not exists
        epm_cmd=assure
        ;;
5070 5071 5072
    policy)                   # HELPCMD: print detailed information about the priority selection of package
        epm_cmd=policy
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5073 5074

# Repository control
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5075
    update)                   # HELPCMD: update remote package repository databases
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5076 5077
        epm_cmd=update
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5078
    addrepo|ar)               # HELPCMD: add package repo
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5079 5080
        epm_cmd=addrepo
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5081
    repolist|sl|rl|listrepo)  # HELPCMD: print repo list
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5082 5083
        epm_cmd=repolist
        ;;
5084 5085 5086
    repofix)                  # HELPCMD: fix paths in sources lists (ALT Linux only)
        epm_cmd=repofix
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5087
    removerepo|rr)            # HELPCMD: remove package repo
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5088 5089
        epm_cmd=removerepo
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5090
    release-upgrade|upgrade-release)          # HELPCMD: update whole system to the next release
5091 5092
        epm_cmd=release_upgrade
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5093
    kernel-update|kernel-upgrade|update-kernel|upgrade-kernel)      # HELPCMD: update system kernel to the last repo version
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5094 5095
        epm_cmd=kernel_update
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5096 5097

# Other commands
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5098
    clean)                    # HELPCMD: clean local package cache
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5099 5100
        epm_cmd=clean
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5101
    autoremove)               # HELPCMD: auto remove unneeded package(s)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5102 5103
        epm_cmd=autoremove
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5104 5105 5106
    autoorphans|--orphans)    # HELPCMD: remove all packages not from the repository
        epm_cmd=autoorphans
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5107
    upgrade|dist-upgrade)     # HELPCMD: performs upgrades of package software distributions
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5108 5109
        epm_cmd=upgrade
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5110
    Upgrade)                  # HELPCMD: force update package base, then run upgrade
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5111 5112
        epm_cmd=Upgrade
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5113 5114 5115
    downgrade)                # HELPCMD: downgrade [all] packages to the repo state
        epm_cmd=downgrade
        ;;
5116
    download)                 # HELPCMD: download package(s) file to the current dir
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5117 5118
        epm_cmd=download
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5119
    simulate)                 # HELPCMD: simulate install with check requires
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5120 5121
        epm_cmd=simulate
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5122 5123 5124
    audit)                    # HELPCMD: audits installed packages against known vulnerabilities
        epm_cmd=audit
        ;;
5125 5126 5127 5128 5129 5130 5131 5132 5133
    #checksystem)              # HELPCMD: check system for known errors (package management related)
    #    epm_cmd=checksystem
    #    ;;
    site|url)                 # HELPCMD: open package's site in a browser (use -p for open packages.altlinux.org site)
        epm_cmd=site
        ;;
    print)                    # HELPCMD: print various info, run epm print help for details
        epm_cmd=print
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5134
    -V|checkpkg|integrity)    # HELPCMD: check package file integrity (checksum)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5135 5136
        epm_cmd=checkpkg
        ;;
5137 5138 5139 5140 5141
    -h|--help|help)           # HELPOPT: print this help
        help=1
        phelp
        exit 0
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158
    *)
        return 1
        ;;
    esac
    return 0
}

check_option()
{
    case $1 in
    -v|--version)         # HELPOPT: print version
        print_version
        exit 0
        ;;
    --verbose)            # HELPOPT: verbose mode
        verbose=1
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5159
    --skip-installed)     # HELPOPT: skip already installed packages during install
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173
        skip_installed=1
        ;;
    --show-command-only)  # HELPOPT: show command only, do not any action (supports install and remove ONLY)
        show_command_only=1
        ;;
    --quiet)              # HELPOPT: quiet mode (do not print commands before exec)
        quiet=1
        ;;
    --nodeps)             # HELPOPT: skip dependency check (during install/simulate and so on)
        nodeps="--nodeps"
        ;;
    --force)              # HELPOPT: force install/remove package (f.i., override)
        force="--force"
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5174
    --short)              # HELPOPT: short output (just 'package' instead 'package-version-release')
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5175 5176
        short="--short"
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5177
    --sort)               # HELPOPT: sort output, f.i. --sort=size (supported only for packages command)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5178
        # TODO: how to read arg?
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5179 5180
        sort="$1"
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5181 5182 5183 5184 5185 5186 5187 5188 5189 5190
    --auto)               # HELPOPT: non interactive mode
        non_interactive=1
        ;;
    *)
        return 1
        ;;
    esac
    return 0
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
5191 5192
check_filenames()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5193 5194 5195 5196 5197
    local opt
    for opt in $* ; do
        # files can be with full path or have extension via .
        if [ -f "$opt" ] && echo "$opt" | grep -q "[/\.]" ; then
            pkg_files="$pkg_files $opt"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5198 5199
        elif [ -d "$opt" ] ; then
            pkg_dirs="$pkg_dirs $opt"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5200
        elif echo "$opt" | grep -q "://" ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5201
            pkg_urls="$pkg_urls $opt"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5202 5203 5204 5205 5206
        else
            pkg_names="$pkg_names $opt"
        fi
        quoted_args="$quoted_args \"$opt\""
    done
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5207 5208 5209 5210 5211 5212 5213 5214 5215
}

FLAGENDOPTS=
for opt in "$@" ; do
    [ "$opt" = "--" ] && FLAGENDOPTS=1 && continue
    if [ -z "$FLAGENDOPTS" ] ; then
        check_command $opt && continue
        check_option $opt && continue
    fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5216
    # Note: will parse all params separately (no package names with spaces!)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5217
    check_filenames $opt
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5218 5219
done

Vitaly Lipatov's avatar
Vitaly Lipatov committed
5220 5221
# if input is not console, get pkg from it too
if ! inputisatty ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5222
    for opt in $(withtimeout 1 cat) ; do
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5223 5224 5225 5226
        check_filenames $opt
    done
fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
5227
pkg_files=$(strip_spaces "$pkg_files")
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5228
pkg_dirs=$(strip_spaces "$pkg_dirs")
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5229 5230
# in common case dirs equals to names only suddenly
pkg_names=$(strip_spaces "$pkg_names $pkg_dirs")
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5231
pkg_urls=$(strip_spaces "$pkg_urls")
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5232

Vitaly Lipatov's avatar
Vitaly Lipatov committed
5233
pkg_filenames=$(strip_spaces "$pkg_files $pkg_names")
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5234 5235 5236 5237 5238 5239 5240 5241 5242 5243

# Just debug
#echover "command: $epm_cmd"
#echover "pkg_files=$pkg_files"
#echover "pkg_names=$pkg_names"

# Just printout help if run without args
if [ -z "$epm_cmd" ] ; then
    print_version
    echo
5244
    fatal "Run $ $progname --help for get help"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5245 5246
fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
5247 5248
# Use eatmydata for write specific operations
case $epm_cmd in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5249
    update|upgrade|Upgrade|install|reinstall|Install|remove|autoremove|kernel_update|release_upgrade|check)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5250
        set_eatmydata
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5251
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
5252 5253
esac

Vitaly Lipatov's avatar
Vitaly Lipatov committed
5254 5255 5256
# Run helper for command
epm_$epm_cmd
# return last error code (from subroutine)