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