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