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