Add debugging
[llpp.git] / llppac
blob59226f97829f7594a2c775cf2a2ac9a871d0cb82
1 #!/bin/sh
3 set -e
5 CACHEDIR=$HOME/.cache/llpp
6 caspsuf=
7 type=
9 die() {
10 echo "$1" >&2
11 exit 1
14 executable_p() {
15 command -v "$1" >/dev/null 2>&1
18 maketext() {
19 cat >"$CACHEDIR/fonts/text" <<EOF
20 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
21 eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
22 minim veniam, quis nostrud exercitation ullamco laboris nisi ut
23 aliquip ex ea commodo consequat. Duis aute irure dolor in
24 reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
25 pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
26 culpa qui officia deserunt mollit anim id est laborum.
27 abcdefghizklmnopqrstwxyz
28 ABCDEFGHIZKLMNOPQRSTWXYZ
29 \`1234567890-=\\
30 ~!@#\$%^&*()_+|
31 [];',./
32 {}:"<>?
34 EOF
37 md5of="$1"
38 trap 'test -n "$casp" && rm -f "$casp"' 0
40 test -d "$CACHEDIR" || die "cache directory '$CACHEDIR' does not exist"
41 while getopts m:t:f opt; do
42 case $opt in
43 m) mime=$OPTARG;;
44 t) type=$OPTARG;;
45 f) force=1;;
46 ?) die "usage: $0 [-m mime/type] [-t filter] [-f] path";;
47 esac
48 done
49 shift $(($OPTIND - 1))
50 test -z "$1" && die "usage $0: path"
52 test -z "$type" && {
53 ft=$(file -L --mime-type -b "$1") || die "$ft"
56 case $ft in
57 application/x-gzip | application/x-compress) dc=zcat;;
58 application/x-xz) dc=xzcat;;
59 application/x-bzip2) dc=bzcat;;
60 *) unset dc || true;;
61 esac
63 filt='"${dc-cat}" "$1" |'
65 if test -z "$type"; then
66 test -z "$dc" || ft=$(file -L --mime-type -bz "$1") || die "$ft"
68 mime=${mime:-$ft}
69 case $mime in
70 application/postscript) type=ps;;
71 application/pdf) type=pdf;;
72 image/vnd.djvu) type=djvu;;
73 text/html) type=html;;
74 application/msword) type=word;;
75 application/vnd.openxmlformats-officedocument.* \
76 | application/vnd.ms-powerpoint \
77 | application/vnd.ms-excel \
78 | application/vnd.oasis.opendocument.*) type=uno;;
79 image/svg+xml) type=svg;;
80 image/png | image/jpeg) test -n "$dc" && type="image" || type="image2";;
81 image/*) type=image;;
82 application/x-dvi) type=dvi;;
83 application/x-font-ttf | application/vnd.ms-opentype) type=font;;
84 *) die "unhandled file type: '$mime'";;
85 esac
88 case $type in
89 ps) conv='ps2pdf - "$casp"';;
90 image2|pdf) test -z "$dc" && exec llpp "$@" || conv='cat >"$casp"';;
91 djvu) conv='djvups - | ps2pdf - "$casp"';;
92 html) {
93 test -n "$dc" && die "can not convert compressed '$mime'"
94 unset filt
95 conv='prince "$1" -o "$casp"'
96 };;
97 word) {
98 if executable_p unoconv && test -z "$dc"; then
99 unset filt
100 conv='unoconv -o "$casp" "$1"'
101 else
102 conv='antiword -m 8859-1.txt -a a4 - >"$casp"'
105 uno) {
106 test -n "$dc" && die "can not convert compressed '$mime'"
107 unset filt
108 conv='unoconv -o "$casp" "$1"'
110 svg) {
111 executable_p rsvg-convert \
112 && conv='rsvg-convert -f pdf -o "$casp"' \
113 || conv='inkscape -z -A "$casp" -'
115 font) {
116 if test -e "$2"; then
117 arg="$2"
118 else
119 arg="--waterfall $CACHEDIR/fonts/text"
120 maketext
122 # https://mail.gnome.org/archives/gtk-i18n-list/2010-June/msg00000.html
123 eval $filt cat >$CACHEDIR/fonts/font
124 cat >$CACHEDIR/fonts/fonts.conf <<EOF
125 <?xml version="1.0"?>
126 <!DOCTYPE fontconfig SYSTEM "fonts.dtd">
127 <fontconfig>
128 <dir>$CACHEDIR/fonts/</dir>
129 <cachedir>$CACHEDIR/fonts/</cachedir>
130 <config></config>
131 </fontconfig>
133 unset filt
134 conv='FONTCONFIG_PATH=$CACHEDIR/fonts/ pango-view -q $arg -o $casp'
135 caspsuf=.pdf
137 fcfont) {
138 if test -e "$2"; then
139 arg="$2"
140 else
141 arg="--waterfall $CACHEDIR/fonts/text"
142 maketext
144 conv='pango-view --font="$1" -q $arg -o $casp'
145 caspsuf=.pdf
146 md5of=/dev/null
147 filt=
148 force=1
150 image) conv='convert - pdf:"$casp"';;
151 dvi) {
152 test -n "$dc" && die "can not convert compressed '$mime'"
153 unset filt
154 conv='dvipdf "$1" "$casp"'
156 *) die "unhandled filter type: '$type'";;
157 esac
159 hash=$(md5sum "$md5of") || die "$hash"
160 casp=$CACHEDIR/${hash%% *}$caspsuf
161 (test -n "$force" -o ! -e "$casp") && eval "$filt" "$conv"
163 exec llpp -origin "$@" "$casp"