f8c4732ead9fcb2e4e400af9f562aacb064a1545
[platform/upstream/aspell.git] / scripts / prezip
1 #!/bin/sh
2
3 # Copyright (c) 2004
4 # Kevin Atkinson
5 #
6 # Permission to use, copy, modify, distribute and sell this software
7 # and its documentation for any purpose is hereby granted without
8 # fee, provided that the above copyright notice appear in all copies  
9 # and that both that copyright notice and this permission notice 
10 # appear in supporting documentation.  Kevin Atkinson makes no
11 # representations about the suitability of this software for any
12 # purpose.  It is provided "as is" without express or implied
13 # warranty.
14
15 cmd=`basename "$0"`
16
17 warn () 
18 {
19   if test -z "$quiet"; then echo "$cmd: $1" >&2 ; fi
20 }
21
22 error () 
23 {
24   echo "$cmd: $1" >&2
25   errors=t
26 }
27
28 zip2 ()
29 {
30   case $1 in
31   d) 
32     prezip-bin -d "$cmd: $2"
33     ;;
34   z)
35     if test "$sort"
36     then
37       LC_COLLATE=C sort -u | prezip-bin -z "$cmd: $2"
38     else
39       prezip-bin -z "$cmd: $2"
40     fi
41     ;;
42   esac
43   if test $? -eq 0
44   then
45     return 0
46   else
47     errors=t
48     return 1
49   fi
50 }
51
52 zip ()
53 {
54   if test -e "$3" -a ! "$force"
55   then
56     error "Output file $3 already exists."
57     return 1
58   fi
59   zip2 $1 "$2: " < "$2" > "$3"
60   if test $? -eq 0
61   then
62     if test -z "$keep"; then rm "$2"; fi
63     return 0
64   else
65     rm "$3"
66     return 1
67   fi
68 }
69
70 case $cmd in
71 prezip)   mode=z ;;
72 preunzip) mode=d ;; 
73 precat)   mode=d; stdout=t ;;
74 *)        mode=h ;;
75 esac
76
77 num=0
78 for p
79 do
80   case $p in
81   --*)
82     parm=`expr "x$p" : '...\(.*\)'`
83     case $parm in
84     decompress ) mode=d ;;
85     compress   ) mode=z ;;
86     keep       ) keep=t ;;
87     force      ) force=t ;;
88     stdout     ) stdout=t ;;
89     sort       ) sort=t ;;
90     nocwl      ) nocwl=t ;;
91     license    ) mode=L ;;
92     version    ) mode=V ;;
93     help       ) mode=h ;;
94     quiet      ) quiet=t ;;
95     *          ) error "invalid option -- $parm";;
96     esac
97     ;;
98   -* ) 
99     p=`expr "x$p" : '..\(.*\)'`
100     while test "$p"
101     do
102       parm=`expr "$p" : '\(.\)'`
103       p=`expr "$p" : '.\(.*\)'`
104       case $parm in
105       d ) mode=d ;;
106       z ) mode=z ;;
107       k ) keep=t ;;
108       f ) force=t ;;
109       c ) stdout=t ;;
110       s ) sort=t ;;
111       S ) nocwl=t ;;
112       L ) mode=L ;;
113       V ) mode=V ;;
114       h ) mode=h ;;
115       q ) quiet=t ;;
116       * ) error "invalid option -- $parm";;
117       esac
118     done
119     ;;
120   * )
121     num=`expr $num + 1`
122     ;;
123   esac
124 done
125
126 if test "$errors" 
127 then
128   mode=h
129 fi
130
131 case $mode in
132 h ) 
133   prezip-bin -V
134   cat <<EOF
135
136   usage $0 [-dzhLV] [-cfksSq] [file ...]
137
138    -h --help        display help
139    -d --decompress  force decompression
140    -z --compress    dorce compression
141    -L --license     display software license
142    -V --version     display version
143
144    -c --stdout      decompress to standard output
145    -f --force       force
146    -k --keep        keep input files
147    -s --sort        sort and remove duplicates before compressing
148    -S --nocwl       do not rename .wl suffix to .cwl (use .wl.pz instead)
149    -q --quiet       suppress all warnings
150
151   If invoked as "prezip" the default action is to compress.
152              as "preunzip" the default action is to decompress.
153              as "precat" the default action is to decompress to stdout.
154
155   If no file names are given then prezip will compress or decompress
156   from the standard input to the standard output.  Short flags can be
157   combined so that "-c -s" is the same as "-cs".
158
159   Prezip is _not_ a general purpose compressor.  It should only be
160   used on sorted word lists or other similar text files.  It will
161   likely _increase_ the size of binary data.
162
163 EOF
164   ;;
165 L )
166   prezip-bin -V
167   cat <<EOF
168
169   Copyright (c) 2004
170   Kevin Atkinson
171
172   Permission to use, copy, modify, distribute and sell this software
173   and its documentation for any purpose is hereby granted without
174   fee, provided that the above copyright notice appear in all copies  
175   and that both that copyright notice and this permission notice 
176   appear in supporting documentation.  Kevin Atkinson makes no
177   representations about the suitability of this software for any
178   purpose.  It is provided "as is" without express or implied
179   warranty.
180
181 EOF
182   ;;
183 V ) 
184   prezip-bin -V
185   ;;
186 d | z )
187 if test $num -gt 0
188 then
189   for f
190   do
191     case $f in
192     -* ) ;;
193     * )
194       if test \( -f "$f" -a ! -L "$f" \) \
195               -o \( \( "$stdout" -o "$force" \) -a -L "$f" \)
196       then
197         if test "$stdout"
198         then
199           zip2 $mode "$f: " < "$f"
200         else
201           case $mode in
202           d )
203             dir=`dirname "$f"`
204             file=`basename "$f"`
205             base=`basename "$f" .pz`
206             base2=`basename "$f" .cwl`
207             if test "$file" != "$base"
208             then
209               out="$dir/$base"
210               zip d "$f" "$out"
211             elif test "$file" != "$base2"
212             then
213               out="$dir/$base2.wl"
214               zip d "$f" "$out"
215             elif test "$force"
216             then 
217               out="$f.out"
218               warn "can't guess original name - using \"$out\""
219               zip d "$f" "$out"
220             else
221               warn "$f does not end in .pz or .cwl - ignored"
222             fi
223             ;;
224           z )
225             dir=`dirname "$f"`
226             file=`basename "$f"`
227             base=`basename "$f" .wl`
228             if test "$nocwl" -o "$file" = "$base"
229             then
230               zip z "$f" "$f.pz"
231             else
232               zip z "$f" "$dir/$base.cwl"
233             fi
234             ;;
235           esac
236         fi
237       elif test -e "$f"
238       then
239         warn "$f is not a regular file - ignored"
240       else
241         error "$f: No such file"
242       fi
243       ;;
244     esac
245   done
246 else
247   case $mode in
248   d ) zip2 d ;;
249   z ) zip2 z ;;
250   esac
251 fi
252 ;;
253 esac
254
255 if test "$errors" 
256 then
257   exit 1
258 else
259   exit 0
260 fi