maint: update copyright year, bootstrap, init.sh
[debian/gzip] / znew.in
1 #!/bin/sh
2
3 # Copyright (C) 1998, 2002, 2004, 2007, 2010-2016 Free Software Foundation,
4 # Inc.
5 # Copyright (C) 1993 Jean-loup Gailly
6
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License along
18 # with this program; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 bindir=@bindir@
22 case $1 in
23 --__bindir) bindir=${2?}; shift; shift;;
24 esac
25 PATH=$bindir:$PATH; export PATH
26
27 version="znew (gzip) @VERSION@
28 Copyright (C) 2010-2015 Free Software Foundation, Inc.
29 This is free software.  You may redistribute copies of it under the terms of
30 the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
31 There is NO WARRANTY, to the extent permitted by law.
32
33 Written by Jean-loup Gailly."
34
35 usage="Usage: $0 [OPTION]... [FILE]...
36 Recompress files from .Z (compress) format to .gz (gzip) format.
37
38 Options:
39
40   -f     Force recompression even if a .gz file already exists.
41   -t     Test the new files before deleting originals.
42   -v     Verbose; display name and statistics for each file compressed.
43   -9     Use the slowest compression method (optimal compression).
44   -P     Use pipes for the conversion to reduce disk space usage.
45   -K     Keep a .Z file when it is smaller than the .gz file; implies -t.
46       --help     display this help and exit
47       --version  output version information and exit
48
49 Report bugs to <bug-gzip@gnu.org>."
50
51 check=0
52 pipe=0
53 opt=
54 files=
55 keep=0
56 res=0
57 old=0
58 new=0
59 block=1024
60 # block is the disk block size (best guess, need not be exact)
61
62 # Beware -s or --suffix in $GZIP.
63 unset GZIP
64 ext=.gz
65
66 for arg
67 do
68   case "$arg" in
69   --help)      exec echo "$usage";;
70   --version)   exec echo "$version";;
71   -*)     opt="$opt $arg"; shift;;
72    *)     break;;
73   esac
74 done
75
76 if test $# -eq 0; then
77   echo >&2 "$0: invalid number of operands; try \`$0 --help' for help"
78   exit 1
79 fi
80
81 opt=`echo "$opt" | sed -e 's/ //g' -e 's/-//g'`
82 case "$opt" in
83   *t*) check=1; opt=`echo "$opt" | sed 's/t//g'`
84 esac
85 case "$opt" in
86   *K*) keep=1; check=1; opt=`echo "$opt" | sed 's/K//g'`
87 esac
88 case "$opt" in
89   *P*) pipe=1; opt=`echo "$opt" | sed 's/P//g'`
90 esac
91 if test -n "$opt"; then
92   opt="-$opt"
93 fi
94
95 for i do
96   n=`echo "$i" | sed 's/.Z$//'`
97   if test ! -f "$n.Z" ; then
98     echo "$n.Z not found"
99     res=1; continue
100   fi
101   test $keep -eq 1 && old=`wc -c < "$n.Z"`
102   if test $pipe -eq 1; then
103     if gzip -d < "$n.Z" | gzip $opt > "$n$ext"; then
104       # Copy file attributes from old file to new one, if possible.
105       touch -r"$n.Z" -- "$n$ext" 2>/dev/null
106       chmod --reference="$n.Z" -- "$n$ext" 2>/dev/null
107     else
108       echo "error while recompressing $n.Z"
109       res=1; continue
110     fi
111   else
112     if test $check -eq 1; then
113       if cp -p "$n.Z" "$n.$$"; then
114         :
115       else
116         echo "cannot backup $n.Z"
117         res=1; continue
118       fi
119     fi
120     if gzip -d "$n.Z"; then
121       :
122     else
123       test $check -eq 1 && mv "$n.$$" "$n.Z"
124       echo "error while uncompressing $n.Z"
125       res=1; continue
126     fi
127     if gzip $opt "$n"; then
128       :
129     else
130       if test $check -eq 1; then
131         mv "$n.$$" "$n.Z" && rm -f "$n"
132         echo "error while recompressing $n"
133       else
134         # compress $n  (might be dangerous if disk full)
135         echo "error while recompressing $n, left uncompressed"
136       fi
137       res=1; continue
138     fi
139   fi
140   test $keep -eq 1 && new=`wc -c < "$n$ext"`
141   if test $keep -eq 1 && test `expr \( $old + $block - 1 \) / $block` -lt \
142                               `expr \( $new + $block - 1 \) / $block`; then
143     if test $pipe -eq 1; then
144       rm -f "$n$ext"
145     else
146       mv "$n.$$" "$n.Z" && rm -f "$n$ext"
147     fi
148     echo "$n.Z smaller than $n$ext -- unchanged"
149
150   elif test $check -eq 1; then
151     if gzip -t "$n$ext" ; then
152       rm -f "$n.$$" "$n.Z"
153     else
154       test $pipe -eq 0 && mv "$n.$$" "$n.Z"
155       rm -f "$n$ext"
156       echo "error while testing $n$ext, $n.Z unchanged"
157       res=1; continue
158     fi
159   elif test $pipe -eq 1; then
160     rm -f "$n.Z"
161   fi
162 done
163 exit $res