Imported Upstream version 4.0.43
[platform/upstream/mtools.git] / scripts / tgz
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
37 set -e
38 set -u
39
40 Error ()
41 {       echo "Error: $0: ${@-}." >&2
42         exit 1
43 }
44
45 if [ $# = 0 ]; then
46         dest=
47         src=.
48         tar cvf - . | gzip -9v
49         exit 0
50 elif [ $# = 1 ]; then
51         dest=$1
52         src=.
53 else
54         dest=$1
55         shift
56         src="${@-}"
57 fi
58
59 case $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
68 esac
69
70 if [ -h "$dest" ]; then
71         Error "Destination file \"$dest\" already exists as a symbolic link"
72 elif [ -f "$dest" ]; then
73         Error "Destination \"$dest\" already exists as a file"
74 elif [ -d "$dest" ]; then
75         Error "Destination \"$dest\" already exists as a directory"
76 fi
77 if [ -z "$dest" -o "X$dest" = 'X-' ]; then
78         echo "Writing gzipp'd tar archive to standard output." >&2
79         tar cvfS - -- $src | gzip -9v
80 else
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
84 fi
85
86 exit 0