Initial import from FreeBSD RELENG_4:
[dragonfly.git] / gnu / usr.bin / gzip / znew
1 #!/bin/sh
2 #
3 # $FreeBSD: src/gnu/usr.bin/gzip/znew,v 1.5.2.1 2002/01/28 01:21:12 nectar Exp $
4
5 check=0
6 pipe=0
7 opt=
8 files=
9 keep=0
10 res=0
11 old=0
12 new=0
13 block=1024
14 # block is the disk block size (best guess, need not be exact)
15
16 warn="(does not preserve modes and timestamp)"
17 tmp=`mktemp -d -t znew`
18 if test -z "$tmp"; then
19   echo znew: could not create temporary directory
20   exit 1
21 fi
22 echo hi > $tmp/1
23 echo hi > $tmp/2
24 if test -z "`(${CPMOD-cpmod} $tmp/1 $tmp/2) 2>&1`"; then
25   cpmod=${CPMOD-cpmod}
26   warn=""
27 fi
28
29 if test -z "$cpmod" && ${TOUCH-touch} -r $tmp/1 $tmp/2 2>/dev/null; then
30   cpmod="${TOUCH-touch}"
31   cpmodarg="-r"
32   warn="(does not preserve file modes)"
33 fi
34
35 # check if GZIP env. variable uses -S or --suffix
36 gzip -q $tmp/1
37 ext=`echo $tmp/1* | sed "s|$tmp/1||"`
38 rm -rf $tmp
39 if test -z "$ext"; then
40   echo znew: error determining gzip extension
41   exit 1
42 fi
43 if test "$ext" = ".Z"; then
44   echo znew: cannot use .Z as gzip extension.
45   exit 1
46 fi
47
48 for arg
49 do
50   case "$arg" in
51   -*)     opt="$opt $arg"; shift;;
52    *)     break;;
53   esac
54 done
55
56 if test $# -eq 0; then
57   echo "recompress .Z files into $ext (gzip) files"
58   echo usage: `echo $0 | sed 's,^.*/,,'` "[-tv9KP]" file.Z...
59   echo "  -t tests the new files before deleting originals"
60   echo "  -v be verbose"
61   echo "  -9 use the slowest compression method (optimal compression)"
62   echo "  -K keep a .Z file when it is smaller than the $ext file"
63   echo "  -P use pipes for the conversion $warn"
64   exit 1
65 fi
66
67 opt=`echo "$opt" | sed -e 's/ //g' -e 's/-//g'`
68 case "$opt" in
69   *t*) check=1; opt=`echo "$opt" | sed 's/t//g'`
70 esac
71 case "$opt" in
72   *K*) keep=1; opt=`echo "$opt" | sed 's/K//g'`
73 esac
74 case "$opt" in
75   *P*) pipe=1; opt=`echo "$opt" | sed 's/P//g'`
76 esac
77 if test -n "$opt"; then
78   opt="-$opt"
79 fi
80
81 for i do
82   n=`echo $i | sed 's/.Z$//'`
83   if test ! -f "$n.Z" ; then
84     echo $n.Z not found
85     res=1; continue
86   fi
87   test $keep -eq 1 && old=`wc -c < "$n.Z"`
88   if test $pipe -eq 1; then
89     if gzip -d < "$n.Z" | gzip $opt > "$n$ext"; then
90       # Copy file attributes from old file to new one, if possible.
91       test -n "$cpmod" && $cpmod $cpmodarg "$n.Z" "$n$ext" 2> /dev/null
92     else
93       echo error while recompressing $n.Z
94       res=1; continue
95     fi
96   else
97     if test $check -eq 1; then
98       if cp -p "$n.Z" "$n.$$" 2> /dev/null || cp "$n.Z" "$n.$$"; then
99         :
100       else
101         echo cannot backup "$n.Z"
102         res=1; continue
103       fi
104     fi
105     if gzip -d "$n.Z"; then
106       :
107     else
108       test $check -eq 1 && mv "$n.$$" "$n.Z"
109       echo error while uncompressing $n.Z
110       res=1; continue
111     fi
112     if gzip $opt "$n"; then
113       :
114     else
115       if test $check -eq 1; then
116         mv "$n.$$" "$n.Z" && rm -f "$n"
117         echo error while recompressing $n
118       else
119         # compress $n  (might be dangerous if disk full)
120         echo error while recompressing $n, left uncompressed
121       fi
122       res=1; continue
123     fi
124   fi
125   test $keep -eq 1 && new=`wc -c < "$n$ext"`
126   if test $keep -eq 1 -a `expr \( $old + $block - 1 \) / $block` -lt \
127                          `expr \( $new + $block - 1 \) / $block`; then
128     if test $pipe -eq 1; then
129       rm -f "$n$ext"
130     elif test $check -eq 1; then
131       mv "$n.$$" "$n.Z" && rm -f "$n$ext"
132     else
133       gzip -d "$n$ext" && compress "$n" && rm -f "$n$ext"
134     fi
135     echo "$n.Z smaller than $n$ext -- unchanged"
136
137   elif test $check -eq 1; then
138     if gzip -t "$n$ext" ; then
139       rm -f "$n.$$" "$n.Z"
140     else
141       test $pipe -eq 0 && mv "$n.$$" "$n.Z"
142       rm -f "$n$ext"
143       echo error while testing $n$ext, $n.Z unchanged
144       res=1; continue
145     fi
146   elif test $pipe -eq 1; then
147     rm -f "$n.Z"
148   fi
149 done
150 exit $res