3
# Copyright (C) 2013-2015, 2017, 2020, 2023 Etersoft
4
# Copyright (C) 2013-2015, 2017, 2020, 2023 Vitaly Lipatov <lav@etersoft.ru>
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU Affero General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU Affero General Public License for more details.
16
# You should have received a copy of the GNU Affero General Public License
17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
21
[ "$PROGDIR" = "." ] && PROGDIR=$(pwd)
23
# will replaced to /usr/share/erc during install
28
local CMD="$SHAREDIR/$1"
29
[ -r "$CMD" ] || fatal "Have no $CMD helper file"
33
load_helper erc-sh-functions
34
load_helper erc-sh-archive
41
is_target_format $2 && echo $(get_archive_name "$1").${2/:/} && return
47
# TODO: list of $HAVE_7Z supported (see list_formats)
49
# target file1 [file2...]
55
docmd patool $verbose create "$arc" "$@"
59
# FIXME: get type by ext only
60
local type="$(get_archive_type "$arc")"
63
#docmd $HAVE_7Z a -l $arc "$@"
64
docmd tar cvf "$arc" "$@"
67
# TODO: fix symlinks support
68
docmd $HAVE_7Z a -l "$arc" "$@"
69
#fatal "Not yet supported creating of $type archives"
80
docmd patool $verbose extract "$arc" "$@"
84
local type="$(get_archive_type "$arc")"
86
arc="$(realpath -s "$arc")"
87
tdir=$(mktemp -d $(pwd)/UXXXXXXXX) && cd "$tdir" || fatal
89
local TSUBDIR="$(basename "$arc" .$type)"
93
# TODO: check if there is only one file?
94
# use subdir if there is no subdir in archive
95
TSUBDIR="$(basename "$arc" .$(echo $type | sed -e 's|^tar\.||') )"
96
docmd $HAVE_7Z x -so "$arc" | docmd $HAVE_7Z x -y -si -ttar
99
docmd $HAVE_7Z x -y "$arc" "$@"
100
#fatal "Not yet supported extracting of $type archives"
105
# if only one dir in the subdir
106
if [ -e "$(echo $tdir/*)" ] ; then
119
# TODO: move to patool
120
if [ "$(get_archive_type "$arc" 2>/dev/null)" = "exe" ] ; then
121
docmd $HAVE_7Z l "$arc" || fatal
125
if have_patool ; then
126
docmd patool $verbose list "$arc" "$@"
130
local type="$(get_archive_type "$arc")"
133
docmd $HAVE_7Z l "$arc" "$@"
134
#fatal "Not yet supported listing of $type archives"
145
# TODO: move to patool
146
if [ "$(get_archive_type "$arc" 2>/dev/null)" = "exe" ] ; then
147
docmd $HAVE_7Z t "$arc" || fatal
151
if have_patool ; then
152
docmd patool $verbose test "$arc" "$@"
156
local type="$(get_archive_type "$arc")"
159
docmd $HAVE_7Z t "$arc" "$@"
160
#fatal "Not yet supported test of $type archives"
168
if have_patool ; then
169
docmd patool $verbose repack "$1" "$2"
173
# TODO: if both have tar, try unpack | pack
175
local ftype="$(get_archive_type "$1")"
176
local ttype="$(get_archive_type "$2")"
177
case "$ftype-$ttype" in
179
docmd $HAVE_7Z x -so "$1" > "$2"
182
docmd $HAVE_7Z a -si "$2" < "$1"
185
docmd $HAVE_7Z x -so "$1" | $HAVE_7Z a -si "$2"
188
fatal "Not yet supported repack of $ftype-$ttype archives in 7z mode (try install patool)"
206
# erc dir - pack dir to dirname.zip
207
# erc a archive.zip file(s)... - pack files to archive.zip
208
# erc [x] archive.zip - unpack
209
# unerc archive.zip - unpack
210
# erc [repack] archive1.zip... archive2.rar $HAVE_7Z: - repack all to $HAVE_7Z
211
# erc -f [repack] archive.zip archive.$HAVE_7Z - force repack zip to $HAVE_7Z (override target in anyway)
212
# erc file/dir zip: - pack file to zip
218
echo "Etersoft archive manager version @VERSION@"
219
echo "Copyright (c) Etersoft 2013-2023"
220
echo "This program may be freely redistributed under the terms of the GNU AGPLv3."
225
Usage="Usage: $progname [options] [<command>] [params]..."
226
Descr="erc - universal archive manager"
238
echo "Etersoft archive manager version @VERSION@" >&2
239
echo "Run $0 --help to get help" >&2
243
while [ -n "$1" ] ; do
245
-h|--help|help) # HELPOPT: this help
249
-V|--version) # HELPOPT: print version
253
-q|--quiet) # HELPOPT: be silent
256
-f|--force) # HELPOPT: override target
259
--use-patool) # HELPOPT: force use patool as backend
262
--use-7z) # HELPOPT: force use 7z as backend
266
fatal "Unknown option '$1'"
281
# Just printout help if run without args
282
if [ -z "$cmd" ] ; then
285
fatal "Run $ $progname --help for get help"
290
# if the first arg is some archive, suggest extract
291
if get_archive_type "$cmd" 2>/dev/null >/dev/null ; then
292
if is_target_format $lastarg ; then
297
# erc dir (pack to zip by default)
298
elif [ -d "$cmd" ] && [ -z "$2" ] ; then
300
target=$(basename "$1").zip
302
elif test -r "$1" && is_target_format "$2" ; then
304
elif [ "$progname" = "unerc" ] ; then
311
# TODO: Если программа-архиватор не установлена, предлагать установку с помощью epm
314
a|-a|create|pack|add) # HELPCMD: create archive / add file(s) to archive
315
# TODO: realize archive addition if already exist (and separate adding?)
316
if [ -z "$target" ] && is_target_format $lastarg ; then
317
[ $# = 2 ] || fatal "Need two args"
318
target="$(build_target_name "$1" "$2")"
320
set -- "${@:1:$(($#-1))}"
322
[ -z "$target" ] && target="$1" && shift
324
[ -e "$target" ] && [ -n "$force" ] && docmd rm -f "$target"
325
create_archive "$target" "$@"
327
e|x|-e|-x|u|-u|extract|unpack) # HELPCMD: extract files from archive
328
# TODO: move to patool
329
if [ "$(get_archive_type "$1" 2>/dev/null)" = "exe" ] ; then
330
docmd $HAVE_7Z x "$1"
335
# TODO: implement deletion
336
# d|delete) # HELPCMD: delete file(s) from archive
337
# docmd patool delete "$@"
339
l|-l|list) # HELPCMD: list archive contents
342
t|-t|test|check) # HELPCMD: test for archive integrity
345
type) # HELPCMD: print type of archive
346
get_archive_type "$1" || fatal "Can't recognize $1 as archive"
348
diff) # HELPCMD: compare two archive
350
docmd patool $verbose diff "$@"
352
b|-b|bench|benchmark) # HELPCMD: do CPU benchmark
354
# TODO: can be $HAVE_7Za?
357
search|grep) # HELPCMD: search in files from archive
358
docmd patool $verbose search "$@"
360
repack|conv) # HELPCMD: convert source archive to target
361
# TODO: need repack remove source file?
362
# TODO: check for 2 arg
363
if ! is_target_format $lastarg ; then
364
[ $# = 2 ] || fatal "Need two args"
365
[ "$(realpath "$1")" = "$(realpath "$2")" ] && warning "Output file is the same as input" && exit
366
[ -e "$2" ] && [ -n "$force" ] && docmd rm -f "$2"
367
repack_archive "$1" "$2"
371
# add support for target zip:
373
[ "$i" = "$lastarg" ] && continue
374
target="$(build_target_name "$i" "$lastarg")"
375
[ "$(realpath "$1")" = "$(realpath "$target")" ] && warning "Output file is the same as input" && exit
376
[ -e "$target" ] && [ -n "$force" ] && docmd rm -f "$target"
377
repack_archive "$i" "$target" || exit
381
formats) # HELPCMD: lists supported archive formats
382
# TODO: print allowed with current programs separately
383
if [ -n "$verbose" ] && have_patool ; then
384
docmd patool formats "$@"
385
echo "Also we supports:"
386
( list_subformats ; list_extraformats ) | sed -e "s|^| |"
392
# TODO: If we have archive in parameter, just unpack it
393
fatal "Unknown command $1"