PageRenderTime 44ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/djvulibre-3.5.24/tools/any2djvu

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