xref: /386bsd/usr/local/bin/znew (revision a2142627)
1#!/bin/sh
2
3PATH="/usr/bin:$PATH"; export PATH
4check=0
5pipe=0
6opt=
7files=
8keep=0
9res=0
10old=0
11new=0
12block=1024
13# block is the disk block size (best guess, need not be exact)
14
15warn="(does not preserve modes and timestamp)"
16tmp=/tmp/zfoo.$$
17echo hi > $tmp.1
18echo hi > $tmp.2
19if test -z "`(${CPMOD-cpmod} $tmp.1 $tmp.2) 2>&1`"; then
20  cpmod=${CPMOD-cpmod}
21  warn=""
22fi
23
24if 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)"
28fi
29
30# check if GZIP env. variable uses -S or --suffix
31gzip -q $tmp.1
32ext=`echo $tmp.1* | sed "s|$tmp.1||"`
33rm -f $tmp.[12]*
34if test -z "$ext"; then
35  echo znew: error determining gzip extension
36  exit 1
37fi
38if test "$ext" = ".Z"; then
39  echo znew: cannot use .Z as gzip extension.
40  exit 1
41fi
42
43for arg
44do
45  case "$arg" in
46  -*)     opt="$opt $arg"; shift;;
47   *)     break;;
48  esac
49done
50
51if test $# -eq 0; then
52  echo "recompress .Z files into $ext (gzip) files"
53  echo usage: `echo $0 | sed 's,^.*/,,'` "[-tv9KP]" file.Z...
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)"
57  echo "  -K keep a .Z file when it is smaller than the $ext file"
58  echo "  -P use pipes for the conversion $warn"
59  exit 1
60fi
61
62opt=`echo "$opt" | sed -e 's/ //g' -e 's/-//g'`
63case "$opt" in
64  *t*) check=1; opt=`echo "$opt" | sed 's/t//g'`
65esac
66case "$opt" in
67  *K*) keep=1; opt=`echo "$opt" | sed 's/K//g'`
68esac
69case "$opt" in
70  *P*) pipe=1; opt=`echo "$opt" | sed 's/P//g'`
71esac
72if test -n "$opt"; then
73  opt="-$opt"
74fi
75
76for 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
84    if gzip -d < "$n.Z" | gzip $opt > "$n$ext"; then
85      # Copy file attributes from old file to new one, if possible.
86      test -n "$cpmod" && $cpmod $cpmodarg "$n.Z" "$n$ext" 2> /dev/null
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
120  test $keep -eq 1 && new=`wc -c < "$n$ext"`
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
124      rm -f "$n$ext"
125    elif test $check -eq 1; then
126      mv "$n.$$" "$n.Z" && rm -f "$n$ext"
127    else
128      gzip -d "$n$ext" && compress "$n" && rm -f "$n$ext"
129    fi
130    echo "$n.Z smaller than $n$ext -- unchanged"
131
132  elif test $check -eq 1; then
133    if gzip -t "$n$ext" ; then
134      rm -f "$n.$$" "$n.Z"
135    else
136      test $pipe -eq 0 && mv "$n.$$" "$n.Z"
137      rm -f "$n$ext"
138      echo error while testing $n$ext, $n.Z unchanged
139      res=1; continue
140    fi
141  elif test $pipe -eq 1; then
142    rm -f "$n.Z"
143  fi
144done
145exit $res
146