1#!/bin/sh
2# Copyright 1994 David C. Niemi.
3# Copyright 1997,2001,2002 Alain Knaff.
4# This file is part of mtools.
5#
6# Mtools is free software: you can redistribute it and/or modify
7# it under the terms of the GNU 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.
10#
11# Mtools 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 General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with Mtools.  If not, see <http://www.gnu.org/licenses/>.
18#
19# tgz [destination [source...] ]
20#
21# Make a gzip'd tar archive $1 (or stdout) out of specified files
22# (or, if not specified, from everything in the current directory)
23#
24# Requires gzip in the user's path.
25#
26# Requires gnu tar (or something close) in the user's path
27# due to use of --exclude, --totals and -S.
28#
29# 1994/02/19	DCN	Created
30# 1994/12/01	DCN	Cleanup and major improvements
31#
32# Copyright (C) 1994 David C. Niemi (niemi@tuxers.net)
33# The author requires that any copies or derived works include this
34# copyright notice; no other restrictions are placed on its use.
35#
36
37set -e
38set -u
39
40Error ()
41{	echo "Error: $0: ${@-}." >&2
42	exit 1
43}
44
45if [ $# = 0 ]; then
46	dest=
47	src=.
48	tar cvf - . | gzip -9v
49	exit 0
50elif [ $# = 1 ]; then
51	dest=$1
52	src=.
53else
54	dest=$1
55	shift
56	src="${@-}"
57fi
58
59case $dest in
60"" | . | .. | */ | */. | */.. )
61	echo "Usage: $0: [destination [source...] ]" >&2
62	exit 1
63	;;
64*.t?z | *.?z | *.z | *.Z | *.tz | *.tz? )
65	;;
66*)
67	dest=${dest}.tgz	## Add on .tgz as default suffix
68esac
69
70if [ -h "$dest" ]; then
71	Error "Destination file \"$dest\" already exists as a symbolic link"
72elif [ -f "$dest" ]; then
73	Error "Destination \"$dest\" already exists as a file"
74elif [ -d "$dest" ]; then
75	Error "Destination \"$dest\" already exists as a directory"
76fi
77if [ -z "$dest" -o "X$dest" = 'X-' ]; then
78	echo "Writing gzipp'd tar archive to standard output." >&2
79	tar cvfS - -- $src | gzip -9v
80else
81	echo "Writing gzip'd tar archive to \"$dest\"." >&2
82	tar -cvS --totals --exclude "$dest" -f - -- $src | gzip -9v > "$dest" 
83	ls -l "$dest" >&2
84fi
85
86exit 0
87