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