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

20 21
PROGDIR=$(dirname "$0")
PROGNAME=$(basename "$0")
22
[ -n "$EPMCURDIR" ] || export EPMCURDIR="$(pwd)"
23 24
CMDENV="/usr/bin/env"
[ -x "$CMDENV" ] && CMDSHELL="/usr/bin/env bash" || CMDSHELL="$SHELL"
25
# TODO: pwd for ./epm and which for epm
26
[ "$PROGDIR" = "." ] && PROGDIR="$EPMCURDIR"
27 28
if [ "$0" = "/dev/stdin" ] || [ "$0" = "sh" ] ; then
    PROGDIR=""
29
    PROGNAME=""
30
fi
31

32
# will replaced with /usr/share/eepm during install
33
SHAREDIR=$PROGDIR
34 35
# will replaced with /etc/eepm during install
CONFIGDIR=$PROGDIR/../etc
36

37
export EPMVERSION="@VERSION@"
38

39 40 41 42 43 44 45
# package, single (file), pipe, git
EPMMODE="package"
[ "$SHAREDIR" = "$PROGDIR" ] && EPMMODE="single"
[ "$EPMVERSION" = "@""VERSION""@" ] && EPMMODE="git"
[ "$PROGNAME" = "" ] && EPMMODE="pipe"

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

49 50
load_helper()
{
51 52
    local shieldname="loaded$(echo "$1" | sed -e 's|-||g')"
    # already loaded
53
    eval "[ -n \"\$$shieldname\" ]" && debug "Already loaded $1" && return
54

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

63

64 65
load_helper epm-sh-functions

66 67 68
# fast call for tool
if [ "$1" = "tool" ] ; then
        shift
69 70
        load_helper epm-tool
        epm_tool "$@"
71 72 73 74 75
        exit
fi

if [ "$1" = "--inscript" ] && [ "$2" = "tool" ] ; then
        shift 2
76 77
        load_helper epm-tool
        epm_tool "$@"
78 79 80 81
        exit
fi


82 83
set_pm_type

84
check_tty
Vitaly Lipatov's avatar
Vitaly Lipatov committed
85 86 87 88 89

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

phelp()
{
90
    echo "$Descr
Vitaly Lipatov's avatar
Vitaly Lipatov committed
91 92
$Usage

Vitaly Lipatov's avatar
Vitaly Lipatov committed
93
Options:
94
$(get_help HELPOPT)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
95 96 97 98 99 100 101 102 103 104

Short commands:
$(get_help HELPSHORT)

$(get_help HELPCMD)

Examples:
    $ epmi etckeeper      install etckeeper package
    $ epmqp lib           print out all installed packages with 'lib' in a name
    $ epmqf ip            print out a package the command 'ip' from is
Vitaly Lipatov's avatar
Vitaly Lipatov committed
105 106 107
"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
108 109
print_version()
{
110
        echo "EPM package manager version $EPMVERSION  Telegram: https://t.me/useepm  https://wiki.etersoft.ru/Epm"
111
        echo "Running on $DISTRNAME/$DISTRVERSION ('$PMTYPE' package manager uses '$PKGFORMAT' package format)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
112
        echo "Copyright (c) Etersoft 2012-2024"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
113
        echo "This program may be freely redistributed under the terms of the GNU AGPLv3."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
114 115
}

116

Danil Mikhailov's avatar
Danil Mikhailov committed
117 118
Usage="Usage: epm [options] <command> [package name(s), package files]..."
Descr="epm - EPM package manager"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
119

120
debug=
121
verbose=$EPM_VERBOSE
122
quiet=
123
nodeps=
124
noremove=
125
dryrun=
126
force=
127
repack=
128
norepack=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
129
install=
130
inscript=
131
scripts=
132
noscripts=
133
short=
134
direct=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
135
sort=
136
non_interactive=$EPM_AUTO
137
download=
138
download_only=
139
print_url=
140
interactive=
141
force_yes=
142
skip_installed=
143
skip_missed=
144
show_command_only=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
145
epm_cmd=
146
warmup=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
147
pkg_files=
148
pkg_dirs=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
149
pkg_names=
150
pkg_urls=
151
pkg_options=
152
quoted_args=
153
direct_args=
154
ipfs=
155

156
eget_backend=$EGET_BACKEND
Vitaly Lipatov's avatar
Vitaly Lipatov committed
157
epm_vardir=/var/lib/eepm
158
epm_cachedir=/var/cache/eepm
Vitaly Lipatov's avatar
Vitaly Lipatov committed
159
eget_ipfs_db=$epm_vardir/eget-ipfs-db.txt
160

161
# load system wide config
162
[ -f $CONFIGDIR/eepm.conf ] && . $CONFIGDIR/eepm.conf
163 164


165
case $PROGNAME in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
166
    epmi)                      # HELPSHORT: alias for epm install
167 168
        epm_cmd=install
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
169
    epmI)                      # HELPSHORT: alias for epm Install
Vitaly Lipatov's avatar
Vitaly Lipatov committed
170 171
        epm_cmd=Install
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
172
    epme)                      # HELPSHORT: alias for epm remove
Danil Mikhailov's avatar
Danil Mikhailov committed
173 174
        epm_cmd=remove
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
175
    epmcl)                     # HELPSHORT: alias for epm changelog
176 177
        epm_cmd=changelog
        ;;
178 179
    epmp)                      # HELPSHORT: alias for epm play
        epm_cmd=play
180
        direct_args=1
181
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
182
    epms)                      # HELPSHORT: alias for epm search
Danil Mikhailov's avatar
Danil Mikhailov committed
183
        epm_cmd=search
184
        direct_args=1
Danil Mikhailov's avatar
Danil Mikhailov committed
185
        ;;
186
    epmsf)                     # HELPSHORT: alias for epm search-file (epm sf)
187 188
        epm_cmd=search_file
        ;;
189 190 191
    epmwd)                     # HELPSHORT: alias for epm wd
        epm_cmd=whatdepends
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
192
    epmq)                      # HELPSHORT: alias for epm query
193 194
        epm_cmd=query
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
195
    epmqi)                     # HELPSHORT: alias for epm info
196 197
        epm_cmd=info
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
198
    epmqf)                     # HELPSHORT: alias for epm belongs
Danil Mikhailov's avatar
Danil Mikhailov committed
199 200
        epm_cmd=query_file
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
201
    epmqa)                     # HELPSHORT: alias for epm packages
202 203
        epm_cmd=packages
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
204
    epmqp)                     # HELPSHORT: alias for epm qp (epm query package)
205 206
        epm_cmd=query_package
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
207
    epmql)                     # HELPSHORT: alias for epm filelist
208 209
        epm_cmd=filelist
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
210
    epmrl)                     # HELPSHORT: alias for epm repo list
211
        epm_cmd=repolist
212
        direct_args=1
213
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
214
    epmu)                      # HELPSHORT: alias for epm update
215
        epm_cmd=update
216
        direct_args=1
217
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
218
    epm|upm|eepm)              # HELPSHORT: other aliases for epm command
Danil Mikhailov's avatar
Danil Mikhailov committed
219
        ;;
220 221
    epm.sh)
        ;;
222
    *)
223 224
        # epm by default
        # fatal "Unknown command: $progname"
225 226 227
        ;;
esac

228 229 230
# was called with alias name
[ -n "$epm_cmd" ] && PROGNAME="epm"

231 232 233 234 235
check_command()
{
    # do not override command
    [ -z "$epm_cmd" ] || return

Vitaly Lipatov's avatar
Vitaly Lipatov committed
236
# HELPCMD: PART: Base commands:
237
    case $1 in
238
    -i|install|add|i|it)         # HELPCMD: install package(s) from remote repositories or from local file
Vitaly Lipatov's avatar
Vitaly Lipatov committed
239 240
        epm_cmd=install
        ;;
241
    -e|-P|rm|del|remove|delete|uninstall|erase|purge|e)  # HELPCMD: remove (delete) package(s) from the database and the system
Danil Mikhailov's avatar
Danil Mikhailov committed
242 243
        epm_cmd=remove
        ;;
244
    -s|search|s|find|sr)                # HELPCMD: search in remote package repositories
245
        epm_cmd=search
246
        direct_args=1
247
        ;;
248
    -qp|qp|grep|query_package)     # HELPCMD: search in the list of installed packages
249 250
        epm_cmd=query_package
        ;;
251
    -qf|qf|-S|wp|which|belongs)     # HELPCMD: query package(s) owning file
252
        epm_cmd=query_file
253
        ;;
254

Vitaly Lipatov's avatar
Vitaly Lipatov committed
255
# HELPCMD: PART: Useful commands:
Vitaly Lipatov's avatar
Vitaly Lipatov committed
256
    reinstall)                # HELPCMD: reinstall package(s) from remote repositories or from local file
257
        epm_cmd=reinstall
Danil Mikhailov's avatar
Danil Mikhailov committed
258
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
259
    Install)                  # HELPCMD: perform update package repo info and install package(s) via install command
260 261
        epm_cmd=Install
        ;;
262
    -q|q|query)               # HELPCMD: check presence of package(s) and print this name (also --short is supported)
Danil Mikhailov's avatar
Danil Mikhailov committed
263
        epm_cmd=query
Danil Mikhailov's avatar
Danil Mikhailov committed
264
        ;;
265 266 267
    installed)                # HELPCMD: check presence of package(s) (like -q with --quiet)
        epm_cmd=installed
        ;;
268 269
    status)                   # HELPCMD: get status of package(s) (see epm status --help)
        epm_cmd=status
270
        direct_args=1
271
        ;;
272
    -sf|sf|filesearch|search-file)        # HELPCMD: search in which package a file is included
273 274
        epm_cmd=search_file
        ;;
275
    -ql|ql|filelist|get-files)          # HELPCMD: print package file list
276
        epm_cmd=filelist
Danil Mikhailov's avatar
Danil Mikhailov committed
277
        ;;
278
    -cl|cl|changelog)         # HELPCMD: show changelog for package
279 280
        epm_cmd=changelog
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
281
    -qi|qi|info|show)         # HELPCMD: print package detail info
282 283
        epm_cmd=info
        ;;
284
    requires|deplist|depends|req|depends-on)     # HELPCMD: print package requires
285 286
        epm_cmd=requires
        ;;
287
    provides|prov)            # HELPCMD: print package provides
288 289
        epm_cmd=provides
        ;;
290
    whatdepends|rdepends|whatrequires|wd|required-by)   # HELPCMD: print packages dependences on that
291 292
        epm_cmd=whatdepends
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
293
    whatprovides)             # HELPCMD: print packages provides that target
294 295
        epm_cmd=whatprovides
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
296
    conflicts)                # HELPCMD: print package conflicts
297 298
        epm_cmd=conflicts
        ;;
299
    -qa|qa|ls|packages|list-installed|li)  # HELPCMD: print list of all installed packages
300
        epm_cmd=packages
301 302
        direct_args=1
        ;;
303
    list)                     # HELPCMD: print list of packages (see epm list --help)
304 305 306
        epm_cmd=list
        direct_args=1
        ;;
307 308 309 310
    # it is too hard operation, so just list name is very short for it
    list-available)           # HELPCMD: print list of all available packages
        epm_cmd=list_available
        direct_args=1
311
        ;;
312
    programs)                 # HELPCMD: print list of installed packages with GUI program(s) (they have .desktop files)
313
        epm_cmd=programs
314
        direct_args=1
315
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
316
    assure)                   # HELPCMD: <command> [package] [version]: install package if command does not exist
317 318
        epm_cmd=assure
        ;;
319
    policy|resolve)           # HELPCMD: print detailed information about the priority selection of package
320 321
        epm_cmd=policy
        ;;
322

Vitaly Lipatov's avatar
Vitaly Lipatov committed
323
# HELPCMD: PART: Repository control:
324
    update|update-repo|ur)    # HELPCMD: update remote package repository databases (with args, run upgrade)
325
        epm_cmd=update
326
        #direct_args=1
327
        ;;
328
    addrepo|ar|--add-repo)    # HELPCMD: add package repo (etersoft, autoimports, archive 2017/01/31); run with param to get list
329
        epm_cmd=addrepo
330
        direct_args=1
331
        ;;
332
    repolist|sl|rl|listrepo|repo-list|list-repo|lr)  # HELPCMD: print repo list
Vitaly Lipatov's avatar
Vitaly Lipatov committed
333
        epm_cmd=repolist
334
        direct_args=1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
335
        ;;
336
    repofix)                  # HELPCMD: <mirror>: fix paths in sources lists (ALT Linux only). use repofix etersoft/yandex/basealt for rewrite URL to the specified server
337
        epm_cmd=repofix
338
        direct_args=1
339
        ;;
340
    removerepo|remove-repo|rr)            # HELPCMD: remove package repo (shortcut for epm repo remove)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
341
        epm_cmd=removerepo
342
        direct_args=1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
343
        ;;
344
    repo)                     # HELPCMD: manipulate with repository list (see epm repo --help)
345
        epm_cmd=repo
346
        direct_args=1
347
        ;;
348 349 350 351 352 353 354 355
    check|fix|verify)         # HELPCMD: check local package base integrity and fix it
        epm_cmd=check
        direct_args=1
        ;;
    dedup)                    # HELPCMD: remove unallowed duplicated pkgs (after upgrade crash)
        epm_cmd=dedup
        direct_args=1
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
356 357
    full-upgrade)              # HELPCMD: update all system packages and kernel
        epm_cmd=full_upgrade
358
        direct_args=1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
359
        ;;
360
    release-upgrade|upgrade-release|upgrade-system|release-switch)  # HELPCMD: upgrade/switch whole system to the release in arg (default: next (latest) release)
361
        epm_cmd=release_upgrade
362
        direct_args=1
363
        ;;
364 365 366 367
    release-downgrade|downgrade-release|downgrade-system)           # HELPCMD: downgrade whole system to the release in arg (default: previuos release)
        epm_cmd=release_downgrade
        direct_args=1
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
368
    kernel-update|kernel-upgrade|update-kernel|upgrade-kernel)      # HELPCMD: update system kernel to the last repo version
369
        epm_cmd=kernel_update
370
        direct_args=1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
371
        ;;
372 373
    remove-old-kernels|remove-old-kernel)      # HELPCMD: remove old system kernels (exclude current or last two kernels)
        epm_cmd=remove_old_kernels
374
        direct_args=1
375
        ;;
376 377 378 379
    stats)                                      # HELPCMD: show statistics about repositories and installations
        epm_cmd=stats
        direct_args=1
        ;;
380

Vitaly Lipatov's avatar
Vitaly Lipatov committed
381
# HELPCMD: PART: Other commands:
382
    clean|delete-cache|dc)                    # HELPCMD: clean local package cache
383
        epm_cmd=clean
384
        direct_args=1
385
        ;;
386 387
    restore)                  # HELPCMD: install (restore) packages need for the project (f.i. by requirements.txt)
        epm_cmd=restore
388
        direct_args=1
389
        ;;
390
    autoremove|package-cleanup)   # HELPCMD: auto remove unneeded package(s) Supports args for ALT: [--direct [libs|python|perl|libs-devel]]
391
        epm_cmd=autoremove
392
        direct_args=1
393
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
394
    mark)                     # HELPCMD: mark package as manually or automatically installed or hold/unhold it (see epm mark --help)
395
        epm_cmd=mark
396
        direct_args=1
397
        ;;
398
    history)                  # HELPCMD: show a log of actions taken by the software management (see epm history --help)
399
        epm_cmd=history
400
        direct_args=1
401
        ;;
402
    autoorphans|--orphans|remove-orphans)    # HELPCMD: remove all packages not from the repository
403
        epm_cmd=autoorphans
404
        direct_args=1
405
        ;;
406
    upgrade|up|dist-upgrade)     # HELPCMD: performs upgrades of package software distributions
407 408
        epm_cmd=upgrade
        ;;
409
    Upgrade)                  # HELPCMD: force update package base, then run upgrade
410
        epm_cmd=Upgrade
411
        direct_args=1
412
        ;;
413 414 415
    Downgrade)                # HELPCMD: force update package base, then run downgrade [all] packages to the repo state
        epm_cmd=Downgrade
        ;;
416
    downgrade|distro-sync)    # HELPCMD: downgrade [all] packages to the repo state
417 418
        epm_cmd=downgrade
        ;;
419
    download|fetch|fc)        # HELPCMD: download package(s) file to the current dir
420 421
        epm_cmd=download
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
422
# TODO: replace with install --simulate
423
    simulate)                 # HELPCMD: simulate install with check requires
424 425
        epm_cmd=simulate
        ;;
426 427
    audit)                    # HELPCMD: audits installed packages against known vulnerabilities
        epm_cmd=audit
428
        direct_args=1
429
        ;;
430 431
    #checksystem)              # HELPCMD: check system for known errors (package management related)
    #    epm_cmd=checksystem
432
    #    direct_args=1
433
    #    ;;
434 435 436
    site|url)                 # HELPCMD: open package's site in a browser (use -p for open packages.altlinux.org site)
        epm_cmd=site
        ;;
437
    ei|ik|epminstall|epm-install|selfinstall) # HELPCMD: install package(s) from Korinf (eepm by default)
438 439
        epm_cmd=epm_install
        ;;
440 441
    print)                    # HELPCMD: print various info, run epm print help for details
        epm_cmd=print
442
        direct_args=1
443
        ;;
444
    tool)                     # HELPCMD: run embedded tool (see epm tool --help)
445 446 447
        epm_cmd=tool
        direct_args=1
        ;;
448 449 450
    repack)                   # HELPCMD: repack rpm to local compatibility
        epm_cmd=repack
        ;;
451
    pack)                     # HELPCMD: pack tarball or dir to a rpm package
Vitaly Lipatov's avatar
Vitaly Lipatov committed
452 453 454
        epm_cmd=pack
        direct_args=1
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
455 456 457 458
    moo)
        epm_cmd=moo
        direct_args=1
        ;;
459 460
    prescription|recipe)      # HELPCMD: run prescription (a script to achieving the goal), run without args to get list
        epm_cmd=prescription
461
        direct_args=1
462
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
463
    play)                     # HELPCMD: install the application from the official site (run without args to get list)
464
        epm_cmd=play
465
        direct_args=1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
466
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
467
    -V|checkpkg|integrity)    # HELPCMD: check package file integrity (checksum)
468 469
        epm_cmd=checkpkg
        ;;
470 471 472 473 474
    -h|--help|help)           # HELPOPT: print this help
        help=1
        phelp
        exit 0
        ;;
475 476
    *)
        return 1
477
        ;;
478 479 480 481
    esac
    return 0
}

482 483
check_option()
{
484 485 486 487 488 489 490 491 492 493
    # optimization
    case $1 in
    -*)
        # pass
        ;;
    *)
        return 1
        ;;
    esac

494
    case $1 in
495
    -v|--version)         # HELPOPT: print version
496
        [ -n "$epm_cmd" ] && return 1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
497
        [ -n "$short" ] && echo "$EPMVERSION" | sed -e 's|-.*||' && exit 0
Vitaly Lipatov's avatar
Vitaly Lipatov committed
498 499
        print_version
        exit 0
500 501
        ;;
    --verbose)            # HELPOPT: verbose mode
502
        verbose="--verbose"
503
        ;;
504 505 506
    --debug)              # HELPOPT: more debug output mode
        debug="--debug"
        ;;
507
    --skip-installed)     # HELPOPT: skip already installed packages during install
508 509
        skip_installed=1
        ;;
510 511 512
    --skip-missed)        # HELPOPT: skip not installed packages during remove
        skip_missed=1
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
513
    --show-command-only)  # HELPOPT: show command only, do not any action (supports install and remove ONLY)
514 515
        show_command_only=1
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
516
    --quiet|--silent)     # HELPOPT: quiet mode (do not print commands before exec)
517
        quiet="--quiet"
518
        ;;
519 520 521
    --nodeps)             # HELPOPT: skip dependency check (during install/simulate and so on)
        nodeps="--nodeps"
        ;;
522 523 524
    --force)              # HELPOPT: force install/remove package (f.i., override)
        force="--force"
        ;;
525 526 527
    --noremove|--no-remove)  # HELPOPT: exit if any packages are to be removed during upgrade
        noremove="--no-remove"
        ;;
528
    --no-stdin|--inscript)  # HELPOPT: don't read from stdin for epm args
529 530
        inscript=1
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
531
    --dry-run|--simulate|--just-print|--no-act) # HELPOPT: print only (autoremove/autoorphans/remove only)
532 533
        dryrun="--dry-run"
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
534
    --short)              # HELPOPT: short output (just 'package' instead 'package-version-release')
535 536
        short="--short"
        ;;
537 538 539
    --direct)              # HELPOPT: direct install package file from ftp (not via hilevel repository manager)
        direct="--direct"
        ;;
540 541 542
    --repack)              # HELPOPT: repack rpm package(s) before install
        repack="--repack"
        ;;
543
    --norepack)              # HELPOPT: don't repack rpm package(s) if it is by default before install
544
        norepack="--norepack"
545
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
546 547 548
    --install)             # HELPOPT: install packed rpm package(s)
        install="--install"
        ;;
549
    --scripts)             # HELPOPT: include scripts in repacked rpm package(s) (see --repack or repacking when foreign package is installed)
550
        scripts="--scripts"
551 552 553
        ;;
    --noscripts)           # HELPOPT: disable scripts in install packages
        noscripts="--noscripts"
554
        ;;
555
    --save-only)            # HELPOPT: save the package/tarball after all transformations (instead of install it)
556 557
        save_only="--save-only"
        ;;
558 559 560
    --put-to-repo=*)          # HELPOPT: put the package after all transformations to the repo (--put-to-repo=/path/to/repo)
        put_to_repo="$(echo "$1" | sed -e 's|--put-to-repo=||')"
        ;;
561
    --download-only)       # HELPOPT: download only the package/tarball (before any transformation)
562 563
        download_only="--download-only"
        ;;
564 565 566
    --url)                 # HELPOPT: print only URL instead of download package
        print_url="--url"
        ;;
567
    --sort)               # HELPOPT: sort output, f.i. --sort=size (supported only for packages command)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
568
        # TODO: how to read arg?
569 570
        sort="$1"
        ;;
571
    -y|--auto|--assumeyes|--non-interactive|--disable-interactivity)  # HELPOPT: non interactive mode
Vitaly Lipatov's avatar
Vitaly Lipatov committed
572
        non_interactive="--auto"
573 574 575 576 577
        interactive=""
        ;;
    --interactive)  # HELPOPT: interactive mode (ask before any operation)
        interactive="--interactive"
        non_interactive=""
578
        ;;
579 580 581
    --force-yes)           # HELPOPT: force yes in a danger cases (f.i., during release upgrade)
        force_yes="--force-yes"
        ;;
582 583 584
    --no-check-certificate)
        fatal "--no-check-certificate is a wget option. It is recommended never use it at all. Check the date or upgrade your system."
        ;;
585 586 587 588
    -*)
        [ -n "$direct_args" ] && return 1
        [ -n "$pkg_options" ] && pkg_options="$pkg_options $1" || pkg_options="$1"
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
589
    *)
590
        return 1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
591 592
        ;;
    esac
593 594 595
    return 0
}

596 597
check_filenames()
{
598
    local opt
599
    for opt in "$@" ; do
600
        # files can be with full path or have extension via .
601
        if [ -f "$opt" ] && rhas "$opt" "[/\.]" ; then
602
            has_space "$opt" && warning "There are space(s) in filename '$opt', it is not supported. Skipped" && continue
603
            [ -n "$pkg_files" ] && pkg_files="$pkg_files $opt" || pkg_files="$opt"
604
        elif [ -d "$opt" ] ; then
605
            has_space "$opt" && warning "There are space(s) in directory path '$opt', it is not supported. Skipped" && continue
606
            [ -n "$pkg_dirs" ] && pkg_dirs="$pkg_dirs $opt" || pkg_dirs="$opt"
607
        elif is_url "$opt" ; then
608
            has_space "$opt" && warning "There are space(s) in URL '$opt', it is not supported. Skipped" && continue
609
            [ -n "$pkg_urls" ] && pkg_urls="$pkg_urls $opt" || pkg_urls="$opt"
610 611
        # hack, TODO: reasons
        elif rhas "$opt" "[/]" && ! rhas "$opt" "[()]" ; then
612 613
            has_space "$opt" && warning "There are space(s) in filename '$opt', it is not supported. Skipped" && continue
            [ -n "$pkg_files" ] && pkg_files="$pkg_files $opt" || pkg_files="$opt"
614
        else
Vitaly Lipatov's avatar
Vitaly Lipatov committed
615
            has_space "$opt" && warning "There are space(s) in package name '$opt', it is not supported. Skipped." && continue
616
            rhas "$opt" "[*]" && warning "There are forbidden symbols in package name '$opt'. Skipped." && continue
617
            [ -n "$pkg_names" ] && pkg_names="$pkg_names $opt" || pkg_names="$opt"
618
        fi
619
        [ -n "$quoted_args" ] && quoted_args="$quoted_args \"$opt\"" || quoted_args="\"$opt\""
620
    done
621 622
}

623 624 625 626 627
# handle external EPM_OPTIONS
for opt in $EPM_OPTIONS ; do
        check_option "$opt"
done

628
FLAGENDOPTS=
629
# NOTE: can't use while read here: set vars inside
630
for opt in "$@" ; do
631

632
    [ "$opt" = "--" ] && FLAGENDOPTS=1 && continue
633

634
    if [ -z "$FLAGENDOPTS" ] ; then
635 636
        check_command "$opt" && continue
        check_option "$opt" && continue
637
    fi
638

639
    if [ -n "$direct_args" ] ; then
640
        [ -n "$quoted_args" ] && quoted_args="$quoted_args \"$opt\"" || quoted_args="\"$opt\""
641 642 643
    else
        # Note: will parse all params separately (no package names with spaces!)
        check_filenames "$opt"
644
    fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
645 646
done

647 648 649 650 651
if [ -n "$quiet" ] ; then
    verbose=''
    EPM_VERBOSE=''
fi

652
# fill
Vitaly Lipatov's avatar
Vitaly Lipatov committed
653
export EPM_OPTIONS="$nodeps $force $verbose $debug $quiet $interactive $non_interactive $save_only $download_only"
654

655
# if input is not console and run script from file, get pkgs from stdin too
656
if [ ! -n "$inscript" ] && [ -p /dev/stdin ] && [ "$EPMMODE" != "pipe" ] ; then
657
    for opt in $(withtimeout 10 cat) ; do
658 659 660 661
        # FIXME: do not work
        # workaround against # yes | epme
        [ "$opt" = "y" ] && break;
        [ "$opt" = "yes" ] && break;
662 663 664 665
        check_filenames $opt
    done
fi

666 667
# in common case dirs equals to names only suddenly
pkg_names=$(strip_spaces "$pkg_names $pkg_dirs")
Vitaly Lipatov's avatar
Vitaly Lipatov committed
668

669
pkg_filenames=$(strip_spaces "$pkg_files $pkg_names")
670

671 672 673 674
# Just debug
#echover "command: $epm_cmd"
#echover "pkg_files=$pkg_files"
#echover "pkg_names=$pkg_names"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
675

676 677 678 679 680 681 682 683 684 685 686 687
print_short_help()
{
cat <<EOF

Popular commands:
 epm search <name>          - search package by name
 epm install <package>      - install package
 epm full-upgrade           - do full upgrade (packages, kernel) of the system
 epm Upgrade                - upgrade all installed packages (Upgrade = update + upgrade)
 epm play [application]     - install the application (run without params to get list of available apps)
 epm qf (<command>|<path>)  - print what package contains this command (file)
 epm sf <name>              - search for the name in all files of all packages
Vitaly Lipatov's avatar
Vitaly Lipatov committed
688
 epm cl <package name>      - print changelog for the package
689 690 691
EOF
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
692
# Just printout help if run without args
Vitaly Lipatov's avatar
Vitaly Lipatov committed
693
if [ -z "$epm_cmd" ] ; then
694 695 696
    print_version >&2
    echo >&2
    fatstr="Unrecognized command in '$*' arg(s)"
697 698 699 700
    if [ -z "$*" ] ; then
        fatstr="That program needs be running with some command"
        print_short_help >&2
    fi
701 702
    echo "Run $(echocmd "$PROGNAME --help") to get help." >&2
    echo "Run $(echocmd "epm print info") to get some system and distro info." >&2
703
    fatal "$fatstr."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
704 705
fi

706 707
# Use eatmydata for write specific operations
case $epm_cmd in
708
    update|upgrade|Upgrade|install|reinstall|Install|remove|autoremove|kernel_update|release_upgrade|release_downgrade|check)
709
        set_eatmydata
710
        ;;
711 712
esac

713 714
[ -n "$verbose$EPM_VERBOSE" ] && showcmd "$0 $*"

715
# Run helper for command with natural args
716
load_helper epm-$epm_cmd
717
eval epm_$epm_cmd $quoted_args
Vitaly Lipatov's avatar
Vitaly Lipatov committed
718
# return last error code (from subroutine)