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