Kill stray line
[llpp.git] / misc / llppac
blob3d805ac00018ebf9dac8d2e7a6cd624235b2e7c9
1 #!/bin/sh
2 set -e
4 die() {
5 echo "$1" >&2
6 exit 1
9 cachedir="${XDG_CACHE_HOME:-$HOME/.cache}/llpp"
10 test -d "$cachedir" || die "cache directory '$cachedir' does not exist"
12 caspsuf=
13 type=
15 executable_p() {
16 command -v "$1" >/dev/null 2>&1
19 missing() {
20 executable_p $1 || \
21 eval "$1() { die \"$2 is needed for \$type conversion\"; }"
24 maketext() {
25 test -d "$cachedir/fonts" || mkdir "$cachedir/fonts"
26 cat >"$cachedir/fonts/text" <<EOF
27 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
28 eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
29 minim veniam, quis nostrud exercitation ullamco laboris nisi ut
30 aliquip ex ea commodo consequat. Duis aute irure dolor in
31 reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
32 pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
33 culpa qui officia deserunt mollit anim id est laborum.
34 Posting Soccer Goals On Vine Is Illegal, Say England's Premier League
35 abcdefghizklmnopqrstwxyz
36 ABCDEFGHIZKLMNOPQRSTWXYZ
37 абвгдеёжзийклмнопрстуфхцчшщэюя
38 АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЭЮЯ
39 \`1234567890-=\\
40 ~!@#\$%^&*()_+|
41 [];',./
42 {}:"<>?"
43 S in S9 59 is too much like 5
44 3ЗЭэзээз3ЗqgO0o ijklI1| bgqpykvwlliliiijil1
46 EOF
49 trap 'test -n "$casp" && rm -f "$casp"' 0
51 while getopts c:m:t:f opt; do
52 case $opt in
53 m) mime=$OPTARG;;
54 t) type=$OPTARG;;
55 f) force=true;;
56 c) css="-s $OPTARG";;
57 ?) die "usage: $0 [-c css] [-m mime/type] [-t filter] [-f] [path|url]";;
58 esac
59 done
60 shift $(($OPTIND - 1))
61 test -z "$1" && die "usage $0: path"
63 origin="$1"
64 if ${force-test ! -e "$1"} && expr >/dev/null "$1" : "\(ftp\|https\?\)://"; then
65 if executable_p wget; then
66 dl() {
67 wget -q $1 -O $2
69 elif executable_p curl; then
70 dl() {
71 curl $1 -o $2
73 else
74 die "no program to fetch remote urls found"
77 md5of="$cachedir/$(basename "$1")"
78 dl "$1" "$md5of" || test -e "$md5of"
79 shift
80 set -- "$md5of" "$@"
81 else
82 md5of="$1"
85 test -z "$type" && {
86 ft=$(file -L --mime-type -b "$1") || die "$ft"
89 case $ft in
90 application/x-gzip | application/x-compress) dc=zcat;;
91 application/x-xz) dc=xzcat;;
92 application/x-bzip2) dc=bzcat;;
93 *) unset dc || true;;
94 esac
96 filt='"${dc-cat}" "$1" |'
98 if test -z "$type"; then
99 test -z "$mime" && mime=$(file -L --mime-type -bz "$1" || die "$mime")
100 case $mime in
101 application/postscript) type=ps;;
102 application/pdf) type=pdf;;
103 image/vnd.djvu) type=djvu;;
104 text/html) type=html;;
105 text/plain) type=text;;
106 application/msword) type=word;;
107 application/vnd.openxmlformats-officedocument.* \
108 | application/vnd.ms-powerpoint \
109 | application/vnd.ms-excel \
110 | application/vnd.oasis.opendocument.*) type=uno;;
111 image/svg+xml) type=svg;;
112 image/png | image/jpeg) test -n "$dc" && type="image" || type="image2";;
113 image/*) type=image;;
114 application/x-dvi) type=dvi;;
115 application/x-font-ttf \
116 | application/vnd.ms-opentype \
117 | application/font-sfnt) type=font;;
118 *) die "unhandled file type: '$mime'";;
119 esac
122 caspsuf=".pdf"
123 case $type in
124 ps) conv='ps2pdf - "$casp"';;
125 image2|pdf) test -z "$dc" && exec llpp "$@" || conv='cat >"$casp"';;
126 texi) {
127 missing texi2html "texi2html(http://www.nongnu.org/texi2html/)"
128 missing prince "PrinceXML(http://www.princexml.com/)"
129 conv='texi2html - -o - | prince $css - -o "$casp"'
131 djvu) {
132 missing ddjvu "ddjvu(http://djvu.sourceforge.net/doc/man/ddjvu.html)"
133 conv='ddjvu -format=pdf - "$casp"'
135 html) {
136 missing prince "PrinceXML(http://www.princexml.com/)"
137 conv='prince $css - -o "$casp"'
139 html2epub) {
140 missing pandoc "pandoc(http://pandoc.org)"
141 caspsuf=".epub"
142 conv='pandoc -r html - -w epub -o "$casp"'
144 word) {
145 if executable_p unoconv && test -z "$dc"; then
146 unset filt
147 conv='unoconv -o "$casp" "$1"'
148 else
149 missing antiword "antiword or unoconv"
150 conv='antiword -m 8859-1.txt -a a4 - >"$casp"'
153 uno) {
154 test -n "$dc" && die "cannot convert compressed '$mime'"
155 unset filt
156 missing unoconv "unoconv(http://dag.wiee.rs/home-made/unoconv/)"
157 conv='unoconv -o "$casp" "$1"'
159 svg) {
160 if executable_p inkscape && test -z "$dc"; then
161 unset filt
162 conv='inkscape -z -A "$casp" "$1"'
163 else
164 missing rsvg-convert "rsvg-convert"
165 conv='rsvg-convert -f pdf -o "$casp"'
168 font) {
169 if test -e "$2"; then
170 arg="$2"
171 else
172 arg="--waterfall $cachedir/fonts/text"
173 maketext
175 # https://mail.gnome.org/archives/gtk-i18n-list/2010-June/msg00000.html
176 eval $filt cat >$cachedir/fonts/font
177 cat >$cachedir/fonts/fonts.conf <<EOF
178 <?xml version="1.0"?>
179 <!DOCTYPE fontconfig SYSTEM "fonts.dtd">
180 <fontconfig>
181 <dir>$cachedir/fonts/</dir>
182 <cachedir>$cachedir/fonts/</cachedir>
183 <config></config>
184 </fontconfig>
186 unset filt
187 missing pango-view "pango-view(http://www.pango.org/)"
188 conv='FONTCONFIG_PATH=$cachedir/fonts/ pango-view -q $arg -o $casp'
190 fcfont) {
191 if test -e "$2"; then
192 arg="$2"
193 else
194 arg="--waterfall $cachedir/fonts/text"
195 maketext
197 unset filt
198 missing pango-view "pango-view(http://www.pango.org/)"
199 conv='pango-view --font="$1" -q $arg -o $casp'
200 md5of=/dev/null
201 force=1
203 image) {
204 missing convert "convert(http://www.imagemagick.org/script/convert.php)"
205 conv='convert - pdf:"$casp"'
207 dvi) {
208 test -n "$dc" && die "cannot convert compressed '$mime'"
209 unset filt
210 missing dvipdf "dvipdf(http://ghostscript.com/)"
211 conv='dvipdf "$1" "$casp"'
213 text) {
214 missing pandoc "pandoc(http://pandoc.org/)"
215 conv='pandoc -t epub - -o "$casp"'
216 caspsuf=.epub
218 *) die "unhandled filter type: '$type'";;
219 esac
221 hash=$(md5sum "$md5of") || die "$hash"
222 casp=$cachedir/${hash%% *}$caspsuf
224 { test -n "$force" || test ! -e "$casp"; } && eval "$filt" "$conv"
225 shift
227 exec llpp -origin $origin "$@" "$casp"