-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctan
executable file
·411 lines (350 loc) · 9.06 KB
/
ctan
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#!bash
# $Id: ctan 52 2008-05-24 06:10:06Z kyanh $
export PATH=$PATH:$HOME/bin
source vnmik.configuration
CTAN_ARCHIVE=http://ctan.org/tex-archive
CTAN_CONTRIB=$CTAN_ARCHIVE/macros/latex/contrib/
KYANHNET_REPOS=http://kyanh.net/ctan.tools/
DESTDIR=$PKGDIR/ctan
TOOLS="lynx wget tar bzip2 sed sort gawk"
HAVE_LZMA=0
# functions
tar()
{
z $*
}
msg()
{
echo -e ":: $*"
}
# check for executable tool
ctan_check()
{
type $1 2>/dev/null 1>&2
if [[ $? -gt 0 ]]; then
ERROR=1
msg "not found '$1'"
return 1
fi
}
# check for all tools
ctan_check_all()
{
# msg "checking dependencies..."
ERROR=0
for tool in $TOOLS; do
ctan_check $tool
done
if [[ $ERROR == 1 ]]; then
msg "error occured. exit 1"
exit 1
fi
# msg "found $TOOLS"
# optional packages
ctan_check lzma
if [[ $? == 0 ]]; then
HAVE_LZMA=1
# msg "found lzma"
fi
return 0
}
# show information after update
ctan_info()
{
msg
msg "* `wc -l $DESTDIR/packages.txt |gawk '{print $1}'` package(s) found"
msg "* `wc -l $DESTDIR/files.txt |gawk '{print $1}'` file(s) found"
}
# update database
ctan_update()
{
if [[ "x$1" == "x--first-time" ]]; then
shift
msg "This maybe the first time you use the tool."
msg "The directory ~/.ctan will be created"
fi
mkdir -pv $DESTDIR
# using the cache file http://kyanh.net/ctan.tools/cache.tbz
if [[ ( "x$1" == "x" ) || ( "x$1" == "x--use-cache" ) ]]; then
shift
msg "update by using cache file..."
if [[ $HAVE_LZMA == 1 ]]; then
msg "getting $KYANHNET_REPOS/ctan.tools/cache.tlzma..."
wget $KYANHNET_REPOS/cache.tlzma -O $DESTDIR/cache.tlzma
msg "uncompressing $DESTDIR/cache.tlzma..."
tar xvf $DESTDIR/cache.tlzma --use-compress-program lzma -C $DESTDIR
else
msg "getting $KYANHNET_REPOS/ctan.tools/cache.tbz..."
wget $KYANHNET_REPOS/cache.tbz -O $DESTDIR/cache.tbz
msg "uncompressing $DESTDIR/cache.tbz..."
tar xfjv $DESTDIR/cache.tbz -C $DESTDIR
fi
msg "removing {dirs,files,dump_files}.txt as it is too big..."
msg rm -fv $DESTDIR/{dump_files.txt,cache.tbz,cache.tlzma}
ctan_info
return 0
fi
# donot use cache. download directly from CTAN (15MB)
msg "update will get about 15MB from CTAN and may take very long time."
read -n1 -p":: Are you sure you wanna do this (y/N)?" reply
msg
if [[ ! "x$reply" == "xy" ]]; then
msg "Update was cancelled."
return 0
fi
# create list of packages ####################################################
msg "dumping $CTAN_CONTRIB..."
lynx -dump $CTAN_CONTRIB > $DESTDIR/contrib.txt
grep -E '^[ ]*\[[0-9]+\][^ ]+/' $DESTDIR/contrib.txt \
|gawk '{printf("%s, %s\n", $1,$2);}' \
|sed \
-e 's/\/, /\t/g' \
-e 's#\[[0-9]\+\]##g' \
> $DESTDIR/packages.txt
# create list of files #######################################################
msg "searching all files..."
lynx -dump 'http://ctan.org/cgi-bin/filenameSearch.py?filename=.' \
> $DESTDIR/dump_files.txt
if [[ $? -gt 0 ]]; then
msg "failed to download from CTAN. exit 1"
rm -fv $DESTDIR/dump_files.txt
exit 1
fi
msg "creating list of directories..."
grep tex-archive/ $DESTDIR/dump_files.txt \
|sed \
-e 's/#/ /g' \
-e 's#http://ctan.org/tex-archive/# #g' \
|gawk '{print $2}' \
|sort -u \
> $DESTDIR/dirs.txt
msg "creating list of files..."
grep -E '^\[[0-9]+\][^ ]+' $DESTDIR/dump_files.txt \
|gawk -F']' '{print $2}'\
|sort -u \
> $DESTDIR/files.txt
msg "compressing the results..."
cd $DESTDIR
msg "creating tbz archive..."
tar -cvj -f .cache.tbz {dirs,files,packages}.txt
mv .cache.tbz cache.tbz
if [[ $HAVE_LZMA == 1 ]]; then
msg "creating lzma archive..."
tar -cv --use-compress-program lzma -f .cache.tlzma {dirs,files,packages}.txt
mv .cache.tlzma cache.tlzma
fi
msg "removing dump_files.txt as it's too big..."
rm -fv $DESTDIR/dump_files.txt
ctan_info
if [[ "x$1" == "x--keep-only-cache" ]]; then
msg "removing all temporary files. Only cache is kept"
rm -fv $DESTDIR/*.txt
fi
}
check_first_time()
{
[[ -d $DESTDIR/ ]] || ctan_update --first-time --use-cache
}
# search something
ctan_search()
{
check_fist_time
if [[ "x$1" == "x-file" ]]; then
shift
grep $* $DESTDIR/files.txt | grep -v obsolete
else # search package name
grep $* $DESTDIR/packages.txt | grep -v obsolete
fi
if [[ $? -gt 0 ]]; then
msg "no package/file found"
return 1
fi
}
# download files
ctan_get()
{
check_first_time
ctan_search $* > $DESTDIR/tmp.txt
if [[ $? -gt 0 ]]; then
msg "no package matches '$*'"
return
fi
if [[ "x$1" == "x-file" ]]; then
shift
ctan_grep_type='file'
else
ctan_grep_type='pkg'
fi
if [[ "$ctan_grep_type" == "file" ]]; then
for pkg in `cat $DESTDIR/tmp.txt`; do
msg "$CTAN_ARCHIVE/$pkg";
done
msg
read -n 1 -p ":: Are you sure you want to download (y/N)?" download
if [[ "x$download" == "xy" ]]; then
msg
for pkg in `cat $DESTDIR/tmp.txt`; do
msg "downloading $pkg..."
wget --directory-prefix=$PKGDIR/ctan -c "$CTAN_ARCHIVE/$pkg";
done
fi
else
gawk '{print $1}' $DESTDIR/tmp.txt > $DESTDIR/tmp2.txt
for pkg in `cat $DESTDIR/tmp2.txt`; do
msg "$pkg\t $CTAN_CONTRIB/$pkg.zip";
done
msg
read -n 1 -p "Are you sure you want to download (y/N)?" download
if [[ "x$download" == "xy" ]]; then
msg
for f in `cat $DESTDIR/tmp2.txt`; do
msg "downloading $pkg..."
wget --directory-prefix=$PKGDIR/ctan -c "$CTAN_CONTRIB/$pkg.zip";
done
fi
fi
}
ctan_about()
{
msg "ABOUT"
msg
msg "\tThis small tool is used to seach/download some LaTeX packages that were listed in CTAN:"
msg "\t\t$CTAN_CONTRIB"
msg "\tor search/download any files in"
msg "\t\t$CTAN_ARCHIVE/."
msg ""
msg "\tSince version 1.0.1 the tool uses the cache file"
msg "\t\t$KYANHNET_REPOS/cache.tbz"
msg "\tThis file contains the list of all files in CTAN. It's updated daily by a cron job hosted"
msg "\tby http://kyanh.net/. The size of file is about 400k."
msg
msg "\tThe tool requires wget, lynx, gawk, wc, tar/bzip and sed program to run correctly."
msg "\tSome linux distributions donot contain lynx by default and you must install lynx manually"
msg "\tbefore running this script. So this tool requires a *nix-liked environment to work."
msg "\ttexer <VietTUG> tested and reported that the tool worked fine in Cygwin environment."
msg
msg "\tThis tool was written by kyanh <[email protected]>"
msg "\tkyanh is a member of Vietnamese TeX Users Group <http://viettug.org>."
msg
msg "VERSION"
msg
msg "\t1.0.0 2008/05/11: first version"
msg "\t1.1.0 2008/05/15: search files (use cache file)"
msg "\t1.1.1 2008/05/15: fix bug (ctan_update)"
msg "\t1.2.0 2008/05/16: smaller cache. Thanks to Karl Berry <[email protected]>"
msg "\t1.3.0 2008/05/23: everything is cached. Faster. New options. Thanks to texer at VietTUG"
msg "\t1.3.1 2008/05/24: fix typo. lzma support"
msg
ctan_usage
msg
msg "TODO"
msg
msg "\t* package information supported"
msg "\t* ability to select FTP mirror"
msg "\t* search by package description,..."
msg "\t* package build script"
msg
msg "THANKS"
msg
msg "\t* Karl Berry <[email protected]>"
msg "\t* Nguyen Van Hanh <[email protected]>"
msg
msg "LICENSE"
msg
msg "\tThis tool is published under LPPL."
msg
msg "BUGS"
msg "\tPlease report to kyanh <[email protected]>."
msg
}
ctan_usage()
{
msg "USAGE"
msg
msg "\tctan about :\tshow all information about this tool"
msg "\tctan usage :\tshow usage"
msg "\tctan version :\tshow script version"
msg
msg "\tctan update :\tupdate using $KYANHNET_REPOS/cache.tbz (450k)"
msg "\tctan update --direct :\tupdate directly from CTAN. You are going to download 15MB"
msg "\tctan grep <string> :\tsearch packages match <string>. grep ability is supported"
msg "\tctan get <string> :\tdownload packages match <string> to working directory"
msg
msg "\tIf you want to search files:"
msg
msg "\tctan fgrep <string> :\tsearch files."
msg "\tctan fget <string> :\tdownload files match <string> to working directory"
msg "\tctan grep -file <string>"
msg "\tctan get -file <string>"
msg
msg "EXAMPLES"
msg
msg "\tctan grep theorem\t# search packages match 'theorem'"
msg "\tctan grep ^n\t# search packages srated by 'n'"
msg "\tctan get ^n\t# download packages started by 'n'"
msg "\tctan get -file contrib/ntheorem.zip"
msg "\tctan fget contrib/ntheorem.zip"
msg
msg "\tThe result by '-file' option should be filtered"
msg
msg "\tctan fgrep ntheorem | grep zip"
msg
}
ctan_arg()
{
if [[ "x$1" == "x-file" ]]; then
shift
fi
if [[ "x$1" == "x" ]]; then
msg "missing parameter"
exit 1
fi
}
# check for requirements or not
case "x$1" in
"xusage");;
"xhelp");;
"x");;
"xabout");;
"xdoc");;
"xversion");;
"xcheck");;
*) ctan_check_all;;
esac
# main program
case "x$1" in
"xupdate")
shift
ctan_update $*
;;
"xfgrep")
shift
ctan_arg $*
ctan_search -file $*
;;
"xgrep")
shift
ctan_arg $*
ctan_search $*
;;
"xget")
shift
ctan_arg $*
ctan_get $*
;;
"xfget")
shift
ctan_arg $*
ctan_get -file $*
;;
"x") ctan_usage;;
"xusage") ctan_usage;;
"xhelp") ctan_usage;;
"xdoc") ctan_about;;
"xabout") ctan_about;;
"xversion") grep '^# $Id' $0;;
"xcheck") ctan_check_all;;
*)
msg "wrong parameter. please try 'ctan usage'"
;;
esac