epm 9.79 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 82
epm_cmd=
pkg_files=
pkg_names=
83
pkg_urls=
84
quoted_args=
85 86

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

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

133 134 135 136 137
check_command()
{
    # do not override command
    [ -z "$epm_cmd" ] || return

Vitaly Lipatov's avatar
Vitaly Lipatov committed
138
# Base commands
139
    case $1 in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
140
    -i|install|add|i)         # HELPCMD: install package(s) from remote repositories or from local file
Vitaly Lipatov's avatar
Vitaly Lipatov committed
141 142
        epm_cmd=install
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
143
    -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
144 145
        epm_cmd=remove
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
146
    -s|search)                # HELPCMD: search in remote package repositories
147
        epm_cmd=search
148
        ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
149
    -qp|qp|query_package)     # HELPCMD: search in the list of installed packages
150 151
        epm_cmd=query_package
        ;;
152
    -qf|qf|-S|which|belongs)     # HELPCMD: query package(s) owning file
153
        epm_cmd=query_file
154
        ;;
155

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

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

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

252 253
    *)
        return 1
254
        ;;
255 256 257 258
    esac
    return 0
}

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

305 306
check_filenames()
{
307 308 309 310 311
    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"
312 313
        elif echo "$opt" | grep -q "://" ; then
            pkg_urls="$pkg_names $opt"
314 315 316 317 318
        else
            pkg_names="$pkg_names $opt"
        fi
        quoted_args="$quoted_args \"$opt\""
    done
319 320
}

321
FLAGENDOPTS=
322
for opt in "$@" ; do
323 324 325 326 327
    [ "$opt" = "--" ] && FLAGENDOPTS=1 && continue
    if [ -z "$FLAGENDOPTS" ] ; then
        check_command $opt && continue
        check_option $opt && continue
    fi
328
    # Note: will parse all params separately (no package names with spaces!)
329
    check_filenames $opt
Vitaly Lipatov's avatar
Vitaly Lipatov committed
330 331
done

332
# if input is not console, get pkg from it too
333
if ! inputisatty ; then
334
    for opt in $(withtimeout 1 cat) ; do
335 336 337 338
        check_filenames $opt
    done
fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
339 340
pkg_files=$(strip_spaces "$pkg_files")
pkg_names=$(strip_spaces "$pkg_names")
341
pkg_urls=$(strip_spaces "$pkg_urls")
Vitaly Lipatov's avatar
Vitaly Lipatov committed
342

343 344
pkg_filenames=$(strip_spaces "$pkg_files $pkg_names")

345 346 347 348
# Just debug
#echover "command: $epm_cmd"
#echover "pkg_files=$pkg_files"
#echover "pkg_names=$pkg_names"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
349

Vitaly Lipatov's avatar
Vitaly Lipatov committed
350
# Just printout help if run without args
Vitaly Lipatov's avatar
Vitaly Lipatov committed
351 352 353
if [ -z "$epm_cmd" ] ; then
    print_version
    echo
354
    fatal "Run $ $progname --help for get help"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
355 356
fi

357 358
# Use eatmydata for write specific operations
case $epm_cmd in
359
    update|upgrade|Upgrade|install|reinstall|Install|remove|autoremove|kernel_update|release_upgrade|check)
360
        set_eatmydata
361
        ;;
362 363
esac

Vitaly Lipatov's avatar
Vitaly Lipatov committed
364
# Run helper for command
365
load_helper epm-$epm_cmd
366
epm_$epm_cmd
Vitaly Lipatov's avatar
Vitaly Lipatov committed
367
# return last error code (from subroutine)