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

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

23
# will replaced to /usr/share/eepm during install
24
SHAREDIR=$PROGDIR
25 26 27

load_helper()
{
28
    local CMD="$SHAREDIR/$1"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
29 30
    # do not use fatal() here, it can be initial state
    [ -r "$CMD" ] || { echo "FATAL: Have no $CMD helper file" ; exit 1; }
31
    . $CMD
32 33
}

34

35 36
load_helper epm-sh-functions

Vitaly Lipatov's avatar
Vitaly Lipatov committed
37 38
#PATH=$PATH:/sbin:/usr/sbin

39
set_sudo
40

41
check_tty
Vitaly Lipatov's avatar
Vitaly Lipatov committed
42 43 44 45 46 47 48

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

phelp()
{
	echo "$Descr
$Usage
49
 Commands:
50
$(get_help HELPCMD)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
51

52
 Options:
53
$(get_help HELPOPT)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
54 55 56
"
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
57 58 59
print_version()
{
        echo "EPM package manager version @VERSION@"
60
        echo "Running on $($DISTRVENDOR) ('$PMTYPE' package manager uses '$PKGFORMAT' package format)"
61
        echo "Copyright (c) Etersoft 2012-2014"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
62
        echo "This program may be freely redistributed under the terms of the GNU AGPLv3."
Vitaly Lipatov's avatar
Vitaly Lipatov committed
63 64
}

65

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

69 70
set_pm_type

71
verbose=
72
quiet=
73
nodeps=
74
force=
75
short=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
76
sort=
77
non_interactive=
78
skip_installed=
79
show_command_only=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
80 81
epm_cmd=
pkg_files=
82
pkg_dirs=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
83
pkg_names=
84
pkg_urls=
85
quoted_args=
86 87

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

89 90 91 92
case $progname in
    epmi)
        epm_cmd=install
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
93 94 95
    epmI)
        epm_cmd=Install
        ;;
Danil Mikhailov's avatar
Danil Mikhailov committed
96 97 98
    epme)
        epm_cmd=remove
        ;;
99 100 101
    epmcl)
        epm_cmd=changelog
        ;;
Danil Mikhailov's avatar
Danil Mikhailov committed
102 103 104
    epms)
        epm_cmd=search
        ;;
105 106 107
    epmsf)
        epm_cmd=search_file
        ;;
108 109 110
    epmq)
        epm_cmd=query
        ;;
111 112 113
    epmqi)
        epm_cmd=info
        ;;
Danil Mikhailov's avatar
Danil Mikhailov committed
114 115 116
    epmqf)
        epm_cmd=query_file
        ;;
117 118 119 120 121 122
    epmqa)
        epm_cmd=packages
        ;;
    epmqp)
        epm_cmd=query_package
        ;;
123 124 125
    epmql)
        epm_cmd=filelist
        ;;
126 127 128
    epmu)
        epm_cmd=update
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
129
    epm|upm|eepm)
Danil Mikhailov's avatar
Danil Mikhailov committed
130
        ;;
131
    *)
132 133
        # epm by default
        # fatal "Unknown command: $progname"
134 135 136
        ;;
esac

137 138 139 140 141
check_command()
{
    # do not override command
    [ -z "$epm_cmd" ] || return

Vitaly Lipatov's avatar
Vitaly Lipatov committed
142
# Base commands
143
    case $1 in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
144
    -i|install|add|i)         # HELPCMD: install package(s) from remote repositories or from local file
Vitaly Lipatov's avatar
Vitaly Lipatov committed
145 146
        epm_cmd=install
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
147
    -e|-P|remove|delete|uninstall|erase|e)  # HELPCMD: remove (delete) package(s) from the database and the system
Danil Mikhailov's avatar
Danil Mikhailov committed
148 149
        epm_cmd=remove
        ;;
150
    -s|search|s)                # HELPCMD: search in remote package repositories
151
        epm_cmd=search
152
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
153
    -qp|qp|query_package)     # HELPCMD: search in the list of installed packages
154 155
        epm_cmd=query_package
        ;;
156
    -qf|qf|-S|which|belongs)     # HELPCMD: query package(s) owning file
157
        epm_cmd=query_file
158
        ;;
159

Vitaly Lipatov's avatar
Vitaly Lipatov committed
160
# Useful commands
Vitaly Lipatov's avatar
Vitaly Lipatov committed
161
    reinstall)                # HELPCMD: reinstall package(s) from remote repositories or from local file
162
        epm_cmd=reinstall
Danil Mikhailov's avatar
Danil Mikhailov committed
163
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
164
    Install)                  # HELPCMD: perform update package repo info and install package(s) via install command
165 166
        epm_cmd=Install
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
167
    -q|q|installed|query)     # HELPCMD: check presence of package(s) and print this name (also --short is supported)
Danil Mikhailov's avatar
Danil Mikhailov committed
168
        epm_cmd=query
Danil Mikhailov's avatar
Danil Mikhailov committed
169
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
170
    -sf|sf|filesearch)        # HELPCMD: search in which package a file is included
171 172
        epm_cmd=search_file
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
173
    -ql|ql|filelist)          # HELPCMD: print package file list
174
        epm_cmd=filelist
Danil Mikhailov's avatar
Danil Mikhailov committed
175
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
176
    check|fix|verify)         # HELPCMD: check local package base integrity and fix it
177 178
        epm_cmd=check
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
179
    changelog|cl|-cl)         # HELPCMD: show changelog for package
180 181
        epm_cmd=changelog
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
182
    -qi|qi|info|show)         # HELPCMD: print package detail info
183 184
        epm_cmd=info
        ;;
185
    requires|deplist|req)     # HELPCMD: print package requires
186 187
        epm_cmd=requires
        ;;
188
    provides|prov)            # HELPCMD: print package provides
189 190
        epm_cmd=provides
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
191
    whatdepends)              # HELPCMD: print packages dependences on that
192 193
        epm_cmd=whatdepends
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
194
    whatprovides)             # HELPCMD: print packages provides that target
195 196
        epm_cmd=whatprovides
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
197
    conflicts)                # HELPCMD: print package conflicts
198 199
        epm_cmd=conflicts
        ;;
200
    -qa|list|packages|-l|qa)  # HELPCMD: print list of installed package(s)
201 202
        epm_cmd=packages
        ;;
203
    programs)                 # HELPCMD: print list of installed GUI program(s)
204 205
        epm_cmd=programs
        ;;
206
    assure)                   # HELPCMD: <command> [package]: install package if command does not exists
207 208
        epm_cmd=assure
        ;;
209

Vitaly Lipatov's avatar
Vitaly Lipatov committed
210
# Repository control
Vitaly Lipatov's avatar
Vitaly Lipatov committed
211
    update)                   # HELPCMD: update remote package repository databases
212
        epm_cmd=update
213
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
214
    addrepo|ar)               # HELPCMD: add package repo
215 216
        epm_cmd=addrepo
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
217
    repolist|sl|rl|listrepo)  # HELPCMD: print repo list
Vitaly Lipatov's avatar
Vitaly Lipatov committed
218 219
        epm_cmd=repolist
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
220
    removerepo|rr)            # HELPCMD: remove package repo
Vitaly Lipatov's avatar
Vitaly Lipatov committed
221 222
        epm_cmd=removerepo
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
223
    release-upgrade)          # HELPCMD: update whole system to the next release
224 225
        epm_cmd=release_upgrade
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
226
    kernel-update|kernel-upgrade|update-kernel|upgrade-kernel)      # HELPCMD: update system kernel to the last repo version
227 228
        epm_cmd=kernel_update
        ;;
229

Vitaly Lipatov's avatar
Vitaly Lipatov committed
230
# Other commands
Vitaly Lipatov's avatar
Vitaly Lipatov committed
231
    clean)                    # HELPCMD: clean local package cache
232 233
        epm_cmd=clean
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
234
    autoremove)               # HELPCMD: auto remove unneeded package(s)
235 236
        epm_cmd=autoremove
        ;;
237 238 239
    autoorphans|--orphans)    # HELPCMD: remove all packages not from the repository
        epm_cmd=autoorphans
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
240
    upgrade|dist-upgrade)     # HELPCMD: performs upgrades of package software distributions
241 242
        epm_cmd=upgrade
        ;;
243
    Upgrade)                  # HELPCMD: force update package base, then run upgrade
244 245
        epm_cmd=Upgrade
        ;;
246 247 248
    downgrade)                # HELPCMD: downgrade [all] packages to the repo state
        epm_cmd=downgrade
        ;;
249
    simulate)                 # HELPCMD: simulate install with check requires
250 251
        epm_cmd=simulate
        ;;
252 253 254
    audit)                    # HELPCMD: audits installed packages against known vulnerabilities
        epm_cmd=audit
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
255
    -V|checkpkg|integrity)    # HELPCMD: check package file integrity (checksum)
256 257 258
        epm_cmd=checkpkg
        ;;

259 260
    *)
        return 1
261
        ;;
262 263 264 265
    esac
    return 0
}

266 267 268
check_option()
{
    case $1 in
269
    -h|--help|help)       # HELPOPT: this help
Vitaly Lipatov's avatar
Vitaly Lipatov committed
270 271
        phelp
        exit 0
272 273
        ;;
    -v|--version)         # HELPOPT: print version
Vitaly Lipatov's avatar
Vitaly Lipatov committed
274 275
        print_version
        exit 0
276 277 278 279
        ;;
    --verbose)            # HELPOPT: verbose mode
        verbose=1
        ;;
280
    --skip-installed)     # HELPOPT: skip already installed packages during install
281 282
        skip_installed=1
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
283
    --show-command-only)  # HELPOPT: show command only, do not any action (supports install and remove ONLY)
284 285
        show_command_only=1
        ;;
286 287 288
    --quiet)              # HELPOPT: quiet mode (do not print commands before exec)
        quiet=1
        ;;
289 290 291
    --nodeps)             # HELPOPT: skip dependency check (during install/simulate and so on)
        nodeps="--nodeps"
        ;;
292 293 294
    --force)              # HELPOPT: force install/remove package (f.i., override)
        force="--force"
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
295
    --short)              # HELPOPT: short output (just 'package' instead 'package-version-release')
296 297
        short="--short"
        ;;
298
    --sort)               # HELPOPT: sort output, f.i. --sort=size (supported only for packages command)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
299
        # TODO: how to read arg?
300 301
        sort="$1"
        ;;
302 303 304
    --auto)               # HELPOPT: non interactive mode
        non_interactive=1
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
305
    *)
306
        return 1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
307 308
        ;;
    esac
309 310 311
    return 0
}

312 313
check_filenames()
{
314 315 316 317 318
    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"
319 320
        elif [ -d "$opt" ] ; then
            pkg_dirs="$pkg_dirs $opt"
321
        elif echo "$opt" | grep -q "://" ; then
322
            pkg_urls="$pkg_urls $opt"
323 324 325 326 327
        else
            pkg_names="$pkg_names $opt"
        fi
        quoted_args="$quoted_args \"$opt\""
    done
328 329
}

330
FLAGENDOPTS=
331
for opt in "$@" ; do
332 333 334 335 336
    [ "$opt" = "--" ] && FLAGENDOPTS=1 && continue
    if [ -z "$FLAGENDOPTS" ] ; then
        check_command $opt && continue
        check_option $opt && continue
    fi
337
    # Note: will parse all params separately (no package names with spaces!)
338
    check_filenames $opt
Vitaly Lipatov's avatar
Vitaly Lipatov committed
339 340
done

341
# if input is not console, get pkg from it too
342
if ! inputisatty ; then
343
    for opt in $(withtimeout 1 cat) ; do
344 345 346 347
        check_filenames $opt
    done
fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
348
pkg_files=$(strip_spaces "$pkg_files")
349
pkg_dirs=$(strip_spaces "$pkg_dirs")
Vitaly Lipatov's avatar
Vitaly Lipatov committed
350
pkg_names=$(strip_spaces "$pkg_names")
351
pkg_urls=$(strip_spaces "$pkg_urls")
Vitaly Lipatov's avatar
Vitaly Lipatov committed
352

353
pkg_filenames=$(strip_spaces "$pkg_files $pkg_dirs $pkg_names")
354

355 356 357 358
# Just debug
#echover "command: $epm_cmd"
#echover "pkg_files=$pkg_files"
#echover "pkg_names=$pkg_names"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
359

Vitaly Lipatov's avatar
Vitaly Lipatov committed
360
# Just printout help if run without args
Vitaly Lipatov's avatar
Vitaly Lipatov committed
361 362 363
if [ -z "$epm_cmd" ] ; then
    print_version
    echo
364
    fatal "Run $ $progname --help for get help"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
365 366
fi

367 368
# Use eatmydata for write specific operations
case $epm_cmd in
369
    update|upgrade|Upgrade|install|reinstall|Install|remove|autoremove|kernel_update|release_upgrade|check)
370
        set_eatmydata
371
        ;;
372 373
esac

Vitaly Lipatov's avatar
Vitaly Lipatov committed
374
# Run helper for command
375
load_helper epm-$epm_cmd
376
epm_$epm_cmd
Vitaly Lipatov's avatar
Vitaly Lipatov committed
377
# return last error code (from subroutine)