tools_ercat 3.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#!/bin/bash
#
# Copyright (C) 2013, 2020  Etersoft
# Copyright (C) 2013, 2020  Vitaly Lipatov <lav@etersoft.ru>
#
# 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)
[ "$PROGDIR" = "." ] && PROGDIR=$(pwd)

# will replaced to /usr/share/erc during install
SHAREDIR=$(dirname $0)

load_helper()
{
    local CMD="$SHAREDIR/$1"
    [ -r "$CMD" ] || fatal "Have no $CMD helper file"
    . $CMD
}

load_helper erc-sh-functions
load_helper erc-sh-archive

check_tty

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

 Options:
$(get_help HELPOPT)
"
}

print_version()
{
        echo "Etersoft uncompressor version @VERSION@"
53
        echo "Copyright (c) Etersoft 2013, 2020, 2023"
54 55 56 57 58 59 60 61 62 63 64
        echo "This program may be freely redistributed under the terms of the GNU AGPLv3."
}

regular_unpack()
{
    local file="$1"
    local prg="$2"
    local pkg="$3"
    local opt="$4"

    # instead of epm assure
65
    if ! is_command "$prg" ; then
66 67 68 69 70 71 72 73 74 75
        epm assure $prg $pkg || fatal "Try install $pkg package for $prg unpack command."
    fi

    docmd $prg $opt $file || fatal
}


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

Usage="Usage: $progname [options] file(s)..."
76
Descr="ercat - universal file uncompressor"
77

78
quiet=
79 80 81 82 83 84 85 86 87 88 89 90 91 92
cmd=$1

# Just printout help if run without args
if [ -z "$cmd" ] ; then
    print_version
    echo
    fatal "Run $ $progname --help for get help"
fi

case $cmd in
    -h|--help|help)       # HELPOPT: this help
        phelp
        exit
        ;;
93 94 95 96 97
    -q|--quiet)           # HELPOPT: be silent
        quiet=--quiet
        shift
        cmd=$1
        ;;
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    -v|--version)         # HELPOPT: print version
        print_version
        exit
        ;;
esac

# TODO: check ext
# TODO: check file existence
# TODO: check by content
for f in $@ ; do
    TYPE=$(get_archive_ext $f) || TYPE=$(is_plain_text $f) || { warning "Skipping unrecognized $f" ; continue ; }
    case $TYPE in
        gz)
            regular_unpack "$f" gunzip gzip -c
            ;;
        bz2)
            regular_unpack "$f" bzcat bzip2
            ;;
        xz)
            regular_unpack "$f" xzcat xz
            ;;
        Z|compress)
            regular_unpack "$f" zcat gzip
            ;;
        lzma)
            regular_unpack "$f" lzcat xz
            ;;
        zst|zstd)
            regular_unpack "$f" zstdcat zstd
            ;;
        lz4)
            regular_unpack "$f" lz4cat lz4
            ;;
        plain)
            docmd cat "$f" || fatal
            ;;
        *)
            fatal "Unsupported compression format $TYPE"
            ;;
    esac
done