Debian Codegraph @

tools/any2djvu

raw
1 #! /bin/bash -f
2
3 function copyright()
4 {
5 echo "Copyright (C) 2002 David Kreil <[email protected]>"
6 echo "Modified by Barak A. Pearlmutter <[email protected]>"
7 echo " and Yaroslav Halchenko <[email protected]>"
8 echo "Released under the GNU GPL v2, 21-Oct-2002."
9 }
10
11 function warranty()
12 {
13 echo "This program is distributed in the hope that it will be useful,"
14 echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
15 echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"
16 echo "GNU General Public License for more details."
17 }
18
19 function disclaimer()
20 {
21 echo "By using this tool you accept the following disclaimer:"
22 echo " Because the any2djvu service is free of charge, there is no"
23 echo " warranty of any kind. In particular, no effort is made to"
24 echo " prevent anyone from downloading the files submitted to or"
25 echo " produced by the any2djvu server."
26 }
27
28 # TO DO:
29 # - error handling
30
31 rurl="http://any2djvu.djvu.org"
32 rcgi="any2djvu.php"
33 res=400
34 ocr=1
35 docformat=2
36
37 function warn()
38 {
39 echo "Notes:"
40 echo " - Internet connection is required."
41 echo " - Filenames are assumed to require no URL-encoding."
42 echo " - Documents must be PostScript (.ps, .ps.gz) or PDF (.pdf)."
43 echo " - Defaults are: conversion is at 400dpi, with English OCR enabled."
44 echo " - This script should not be used for large scale conversions of"
45 echo " documents, as it may badly affect and hence endanger the free web"
46 echo " service to the community."
47 echo " - This software comes with NO WARRANTY."
48 }
49
50 function format_help()
51 {
52 echo "Codes for the formats of the input documents to use with -f"
53 echo " 1 - DjVu Document (for verification or OCR)"
54 echo " 2 - PS/PS.GZ/PDF Document (default)"
55 echo " 3 - Photo/Picture/Icon"
56 echo " 4 - Scanned Document - B&W - <200 dpi"
57 echo " 5 - Scanned Document - B&W - 200-400 dpi"
58 echo " 6 - Scanned Document - B&W - >400 dpi"
59 echo " 7 - Scanned Document - Color/Mixed - <200 dpi"
60 echo " 8 - Scanned Document - Color/Mixed - 200-400 dpi"
61 echo " 9 - Scanned Document - Color/Mixed - >400 dpi"
62 }
63
64 function usage()
65 {
66 echo "Convert files from .ps/.ps.gz/.pdf to .djvu"
67 echo "Usage: $0 [options] [url] {filename(s)}"
68 echo
69 echo "Options:"
70 echo " -q quiet mode: reduce chatter on the screen"
71 echo " -a acknowledge internet transmission"
72 echo " -c clean after work: remove log file"
73 echo " -r dpi resolution in dpi (200/300/400, default: $res)"
74 echo " -o ocr perform OCR (0 - no, 1 - yes, default: $ocr)"
75 echo " -f format format of input document; invoke -f help for list"
76 echo " -u url base URL of server (default: $rurl)"
77 echo " -p s CGI script name (default: $rcgi)"
78 echo
79 echo "If no local file is named by the 1st argument the 1st"
80 echo "argument is taken to be an external web-accessible directory and,"
81 echo "the specified files are uploaded from that location."
82 echo
83 echo "Examples:"
84 echo " cd ~bap/public_html/foo"
85 echo " # uploads from web-accessible directory"
86 echo " any2djvu http://barak.pearlmutter.net/papers mesh-preprint.ps.gz"
87 echo " any2djvu http://www.inference.phy.cam.ac.uk/mackay *.ps.gz bar.pdf"
88 echo " # uploads from current directory"
89 echo " any2djvu b*.pdf"
90 echo " any2djvu -s -c -r 300 -o 0 bp.ps"
91 }
92
93
94 while getopts "hacqsr:o:f:u:p:" opt
95 do
96 case "$opt" in
97 h) usage; exit 0 ;;
98 a) DJVU_ONLINE_ACK=1 ;;
99 c) doclean=1 ;;
100 s) echo "option -s is deprecated, please use -q"; silent=1 ;;
101 q) silent=1 ;;
102 r) res="$OPTARG" ;;
103 o) ocr="$OPTARG" ;;
104 f) docformat="$OPTARG" ;;
105 u) rurl="$OPTARG" ;;
106 p) rcgi="$OPTARG" ;;
107 *) usage; exit 2 ;;
108 esac
109 done
110
111 shift $((OPTIND - 1))
112
113 # check resolution
114 if [ ${res#[234]} != '00' ]; then
115 echo 'error: -r resolution must be one of: 200, 300, 400'
116 exit 2
117 fi
118
119 # check OCR option
120 if [ ! "x$ocr" == x0 ] && [ ! "x$ocr" == x1 ]; then
121 echo 'error: -o OCR must be 0 or 1'
122 exit 2
123 fi
124
125 # if help is requested or docformat is not specified right - show help
126 if [ "x$docformat" == x'help' ]; then
127 format_help
128 exit 0
129 fi
130
131 if [ -z "$docformat" ] || [ ! -z "${docformat#[0-9]}" ]; then
132 echo "error: requested document format unknown: $docformat".
133 format_help
134 exit 2
135 fi
136
137 lurl=''
138 if [ ! -e "$1" ]; then # if 1st argument is not an existing file
139 lurl="$1" # assume that it is a URL
140 shift
141 fi
142
143 #log=`date -u`": "`whoami`'@'`hostname`", pid $$: $0 (cwd "`pwd`")"
144 log="`date -u`: `whoami`@`hostname`, pid $$: $0 (cwd `pwd`)"
145 [ -z $silent ] && echo '/-- Started' "$log" >&2
146
147 in="$1"
148 shift
149 if [ -z "$in" ]; then
150 echo "error: no files to convert"
151 usage
152 exit 1
153 fi
154
155 if [ ! -z $s ]; then
156 copyright
157 disclaimer
158 warn
159 fi
160
161 if [ -z "$DJVU_ONLINE_ACK" ]; then
162 cat <<EOF
163 WARNING!
164
165 any2djvu uses an external server which is willing to perform the
166 conversion and requires the document transfer over to that server.
167 There is a security issue in operating on documents not intended for
168 widespread distribution, which could be partially although not
169 completely ameliorated by using a secure web connection.
170
171 Do you acknowledge and allow the transmission of the document?
172 (Type 'yes' to acknowledge. You can define non-empty environment
173 variable DJVU_ONLINE_ACK to avoid seeing this dialog, or use -a
174 command line parameter to any2djvu).
175 EOF
176 echo -n "[yes/no]:"
177 read ack
178 if [[ ! "$ack" =~ ^[yY][eE][sS]$ ]]; then
179 warn
180 exit 1
181 fi
182 fi
183
184 wgetopts=''
185 curlopts=''
186 shellopts=''
187 if [ ! -z $silent ]; then
188 curlopts=' -s '
189 wgetopts=' -q '
190 shellopts=' 1>/dev/null ' # 2>&1
191 fi
192
193 cgiopts="&docformat=$docformat&resolution=$res&ocr=$ocr&legal=1"
194 while [ -n "$in" ]; do
195 b="$in"
196 b="${b%.ps.gz}"
197 b="${b%.ps}"
198 b="${b%.pdf}"
199 b="${b%.PDF}"
200 echo `date -u` Processing $b ...
201 log="$b-any2djvu.log"
202 if [ -z "$lurl" ]; then
203 if [ ! -e "$in" ]; then
204 echo "File $in wasn't found. Conversion stopped"
205 exit 2
206 fi
207 curl $curlopts -F "fupload=@$in" -F submit=Submit \
208 -F docformat=$docformat -F resolution=$res \
209 -F ocr=$ocr -F legal=1 "$rurl/$rcgi" \
210 | eval tee "'$log'" $shellopts
211 else
212 wget $wgetopts -O - "$rurl/$rcgi?urlupload=$lurl/$in$cgiopts" \
213 | eval tee "'$log'" $shellopts
214 fi
215 l=`egrep 'href=djvu/.*\.djvu' "$log"`
216 l="${l##*href=}"
217 l="${l%%>*}"
218 if [ -z "$l" ]; then
219 echo "error: something got wrong. check log file"
220 exit 1
221 fi
222 wget $wgetopts -O "$b.djvu" "$rurl/$l"
223
224 [ -z "$doclean" ] || rm "$log"
225 [ -z $silent ] && ls -l "$b.djvu"
226
227 in="$1"
228 shift
229 done
230
231 log=`date -u`": "`whoami`'@'`hostname`", pid $$: $0 (cwd "`pwd`")"
232
233 [ -z $silent ] && echo '\-- Done' $log >&2