Uhm.. dynamic typing is fun
[llpp.git] / misc / llppac
blob5200b98b18e64995d23a791afefea407bda0487d
1 #!/bin/sh
2 set -e
4 CACHEDIR=$HOME/.cache/llpp
5 caspsuf=
6 type=
8 die() {
9 echo "$1" >&2
10 exit 1
13 executable_p() {
14 command -v "$1" >/dev/null 2>&1
17 maketext() {
18 cat >"$CACHEDIR/fonts/text" <<EOF
19 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
20 eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
21 minim veniam, quis nostrud exercitation ullamco laboris nisi ut
22 aliquip ex ea commodo consequat. Duis aute irure dolor in
23 reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
24 pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
25 culpa qui officia deserunt mollit anim id est laborum.
26 Posting Soccer Goals On Vine Is Illegal, Say England's Premier League
27 abcdefghizklmnopqrstwxyz
28 ABCDEFGHIZKLMNOPQRSTWXYZ
29 \`1234567890-=\\
30 ~!@#\$%^&*()_+|
31 [];',./
32 {}:"<>?
34 EOF
37 trap 'test -n "$casp" && rm -f "$casp"' 0
39 test -d "$CACHEDIR" || die "cache directory '$CACHEDIR' does not exist"
40 while getopts m:t:f opt; do
41 case $opt in
42 m) mime=$OPTARG;;
43 t) type=$OPTARG;;
44 f) force=1;;
45 ?) die "usage: $0 [-m mime/type] [-t filter] [-f] [path|url]";;
46 esac
47 done
48 shift $(($OPTIND - 1))
49 test -z "$1" && die "usage $0: path"
51 test ! -e "$1" && expr >/dev/null "$1" : "\(ftp\|https\?\)://" && {
52 if executable_p wget; then
53 dl() {
54 wget -q -c -nc $1 -O $2
56 else
57 if executable_p curl; then
58 dl() {
59 curl $1 -o $2
61 else
62 echo "no program to fetch remote urls found"
63 exit 1
67 md5of="$CACHEDIR/$(basename "$1")"
68 dl "$1" "$md5of" || test -e "$md5of"
69 set -- "$md5of" ${2+"@"}
70 } || {
71 md5of="$1"
74 test -z "$type" && {
75 ft=$(file -L --mime-type -b "$1") || die "$ft"
78 case $ft in
79 application/x-gzip | application/x-compress) dc=zcat;;
80 application/x-xz) dc=xzcat;;
81 application/x-bzip2) dc=bzcat;;
82 *) unset dc || true;;
83 esac
85 filt='"${dc-cat}" "$1" |'
87 if test -z "$type"; then
88 test -z "$dc" || ft=$(file -L --mime-type -bz "$1") || die "$ft"
90 mime=${mime:-$ft}
91 case $mime in
92 application/postscript) type=ps;;
93 application/pdf) type=pdf;;
94 image/vnd.djvu) type=djvu;;
95 text/html) type=html;;
96 application/msword) type=word;;
97 application/vnd.openxmlformats-officedocument.* \
98 | application/vnd.ms-powerpoint \
99 | application/vnd.ms-excel \
100 | application/vnd.oasis.opendocument.*) type=uno;;
101 image/svg+xml) type=svg;;
102 image/png | image/jpeg) test -n "$dc" && type="image" || type="image2";;
103 image/*) type=image;;
104 application/x-dvi) type=dvi;;
105 application/x-font-ttf | application/vnd.ms-opentype) type=font;;
106 *) die "unhandled file type: '$mime'";;
107 esac
110 case $type in
111 ps) conv='ps2pdf - "$casp"';;
112 image2|pdf) test -z "$dc" && exec llpp "$@" || conv='cat >"$casp"';;
113 djvu) conv='djvups - | ps2pdf - "$casp"';;
114 html) {
115 test -n "$dc" && die "can not convert compressed '$mime'"
116 unset filt
117 conv='prince "$1" -o "$casp"'
119 word) {
120 if executable_p unoconv && test -z "$dc"; then
121 unset filt
122 conv='unoconv -o "$casp" "$1"'
123 else
124 conv='antiword -m 8859-1.txt -a a4 - >"$casp"'
127 uno) {
128 test -n "$dc" && die "can not convert compressed '$mime'"
129 unset filt
130 conv='unoconv -o "$casp" "$1"'
132 svg) {
133 executable_p rsvg-convert \
134 && conv='rsvg-convert -f pdf -o "$casp"' \
135 || conv='inkscape -z -A "$casp" -'
137 font) {
138 if test -e "$2"; then
139 arg="$2"
140 else
141 arg="--waterfall $CACHEDIR/fonts/text"
142 maketext
144 # https://mail.gnome.org/archives/gtk-i18n-list/2010-June/msg00000.html
145 eval $filt cat >$CACHEDIR/fonts/font
146 cat >$CACHEDIR/fonts/fonts.conf <<EOF
147 <?xml version="1.0"?>
148 <!DOCTYPE fontconfig SYSTEM "fonts.dtd">
149 <fontconfig>
150 <dir>$CACHEDIR/fonts/</dir>
151 <cachedir>$CACHEDIR/fonts/</cachedir>
152 <config></config>
153 </fontconfig>
155 unset filt
156 conv='FONTCONFIG_PATH=$CACHEDIR/fonts/ pango-view -q $arg -o $casp'
157 caspsuf=.pdf
159 fcfont) {
160 if test -e "$2"; then
161 arg="$2"
162 else
163 arg="--waterfall $CACHEDIR/fonts/text"
164 maketext
166 conv='pango-view --font="$1" -q $arg -o $casp'
167 caspsuf=.pdf
168 md5of=/dev/null
169 filt=
170 force=1
172 image) conv='convert - pdf:"$casp"';;
173 dvi) {
174 test -n "$dc" && die "can not convert compressed '$mime'"
175 unset filt
176 conv='dvipdf "$1" "$casp"'
178 *) die "unhandled filter type: '$type'";;
179 esac
181 hash=$(md5sum "$md5of") || die "$hash"
182 casp=$CACHEDIR/${hash%% *}$caspsuf
183 (test -n "$force" -o ! -e "$casp") && eval "$filt" "$conv"
185 exec llpp -origin "$@" "$casp"