xref: /aosp_15_r20/external/mtools/scripts/uz (revision d5c9a868b113e0ec0db2f27bc2ce8a253e77c4b0)
1*d5c9a868SElliott Hughes#!/bin/sh
2*d5c9a868SElliott Hughes# Copyright 1994,2002 David C. Niemi.
3*d5c9a868SElliott Hughes# Copyright 1996,1997,2001-2003,2010 Alain Knaff.
4*d5c9a868SElliott Hughes# This file is part of mtools.
5*d5c9a868SElliott Hughes#
6*d5c9a868SElliott Hughes# Mtools is free software: you can redistribute it and/or modify
7*d5c9a868SElliott Hughes# it under the terms of the GNU General Public License as published by
8*d5c9a868SElliott Hughes# the Free Software Foundation, either version 3 of the License, or
9*d5c9a868SElliott Hughes# (at your option) any later version.
10*d5c9a868SElliott Hughes#
11*d5c9a868SElliott Hughes# Mtools is distributed in the hope that it will be useful,
12*d5c9a868SElliott Hughes# but WITHOUT ANY WARRANTY; without even the implied warranty of
13*d5c9a868SElliott Hughes# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*d5c9a868SElliott Hughes# GNU General Public License for more details.
15*d5c9a868SElliott Hughes#
16*d5c9a868SElliott Hughes# You should have received a copy of the GNU General Public License
17*d5c9a868SElliott Hughes# along with Mtools.  If not, see <http://www.gnu.org/licenses/>.
18*d5c9a868SElliott Hughes# uz [file...]
19*d5c9a868SElliott Hughes# lz [file...]
20*d5c9a868SElliott Hughes#
21*d5c9a868SElliott Hughes# If called "uz", gunzips and extracts a gzip'd tar'd archive.
22*d5c9a868SElliott Hughes# If called "lz", gunzips and shows a listing of a gzip'd tar'd archive.
23*d5c9a868SElliott Hughes#
24*d5c9a868SElliott Hughes# Requires: gzip and tar in the user's path.  Should work with most tars.
25*d5c9a868SElliott Hughes# "-" is now used for backwards compatibility with antique tars, e.g. SCO.
26*d5c9a868SElliott Hughes#
27*d5c9a868SElliott Hughes# 1994/02/19	DCN	Created (as a trivial, but useful script)
28*d5c9a868SElliott Hughes# 1994/12/01	DCN	Combined uz and lz, added suffix handling
29*d5c9a868SElliott Hughes# 2002/09/11	DCN	Added bzip2 support
30*d5c9a868SElliott Hughes# 2010/10/16	AKN	Added lzip support
31*d5c9a868SElliott Hughes#
32*d5c9a868SElliott Hughes# Copyright (C) 1994, 2002 David C. Niemi (niemi at tuxers dot net)
33*d5c9a868SElliott Hughes# The author requires that any copies or derived works include this
34*d5c9a868SElliott Hughes# copyright notice; no other restrictions are placed on its use.
35*d5c9a868SElliott Hughes#
36*d5c9a868SElliott Hughes
37*d5c9a868SElliott Hughesset -e
38*d5c9a868SElliott Hughesset -u
39*d5c9a868SElliott Hughes
40*d5c9a868SElliott Hughes## Default unzipping command
41*d5c9a868SElliott Hughesuzcmd='gzip -cd'
42*d5c9a868SElliott Hughes
43*d5c9a868SElliott Hughescase $0 in
44*d5c9a868SElliott Hughes*uz)
45*d5c9a868SElliott Hughes	tarparam="-pxvf"
46*d5c9a868SElliott Hughes	zipparam=""
47*d5c9a868SElliott Hughes	action="Extracting from "
48*d5c9a868SElliott Hughes	;;
49*d5c9a868SElliott Hughes*lz)
50*d5c9a868SElliott Hughes	tarparam="-tvf"
51*d5c9a868SElliott Hughes	zipparam="-l"
52*d5c9a868SElliott Hughes	action="Reading directory of "
53*d5c9a868SElliott Hughes	;;
54*d5c9a868SElliott Hughes*)
55*d5c9a868SElliott Hughes	echo "$0: expect to be named either \"uz\" or \"lz\"." >&2
56*d5c9a868SElliott Hughes	exit 1
57*d5c9a868SElliott Hughes	;;
58*d5c9a868SElliott Hughesesac
59*d5c9a868SElliott Hughes
60*d5c9a868SElliott Hughesif [ $# = 0 ]; then
61*d5c9a868SElliott Hughes	echo "$action standard input." >&2
62*d5c9a868SElliott Hughes	$uzcmd - | tar "$tarparam" -
63*d5c9a868SElliott Hughes	exit 0
64*d5c9a868SElliott Hughesfi
65*d5c9a868SElliott Hughes
66*d5c9a868SElliott Hugheswhile [ $# -ge 1 ]; do
67*d5c9a868SElliott Hughes	echo >&2
68*d5c9a868SElliott Hughes	found=
69*d5c9a868SElliott Hughes
70*d5c9a868SElliott Hughes	for suffix in "" .gz .tgz .tar.gz .z .tar.z .taz .tpz .Z .tar.Z .tar.bz2 tar.lz .zip .jar .war .ear .aar .tar.xz; do
71*d5c9a868SElliott Hughes		if [ -r "${1}$suffix" ]; then
72*d5c9a868SElliott Hughes			found=$1$suffix
73*d5c9a868SElliott Hughes			break
74*d5c9a868SElliott Hughes		fi
75*d5c9a868SElliott Hughes	done
76*d5c9a868SElliott Hughes
77*d5c9a868SElliott Hughes	unzip=0
78*d5c9a868SElliott Hughes	case $found in
79*d5c9a868SElliott Hughes		*.tar.bz2 | *.tb2)
80*d5c9a868SElliott Hughes			uzcmd='bzip2 -cd'
81*d5c9a868SElliott Hughes			;;
82*d5c9a868SElliott Hughes		*.zip | *.jar | *.war | *.ear | *.aar)
83*d5c9a868SElliott Hughes			unzip=1
84*d5c9a868SElliott Hughes			;;
85*d5c9a868SElliott Hughes		*.tar.lz)
86*d5c9a868SElliott Hughes			uzcmd='lzip -cd'
87*d5c9a868SElliott Hughes			;;
88*d5c9a868SElliott Hughes		*.tar.xz)
89*d5c9a868SElliott Hughes			uzcmd='xz -cd'
90*d5c9a868SElliott Hughes			;;
91*d5c9a868SElliott Hughes	esac
92*d5c9a868SElliott Hughes	if [ -z "$found" ]; then
93*d5c9a868SElliott Hughes		echo "$0: could not read \"$1\"." >&2
94*d5c9a868SElliott Hughes	else
95*d5c9a868SElliott Hughes		echo "$action \"$found\"." >&2
96*d5c9a868SElliott Hughes		if [ $unzip = 1 ]; then
97*d5c9a868SElliott Hughes			unzip $zipparam -- "$found"
98*d5c9a868SElliott Hughes		else
99*d5c9a868SElliott Hughes			$uzcmd -- "$found" | tar "$tarparam" -
100*d5c9a868SElliott Hughes		fi
101*d5c9a868SElliott Hughes	fi
102*d5c9a868SElliott Hughes	shift
103*d5c9a868SElliott Hughesdone
104*d5c9a868SElliott Hughes
105*d5c9a868SElliott Hughesexit 0
106