Whoops (as expected/predicted)
[llpp.git] / llppac
blob9c587d409c8b89d0c5b61746e7ddd2d72c2a8eab
1 #!/bin/sh
3 set -e
5 CACHEDIR=$HOME/.cache/llpp
7 die() {
8 echo "$1" >&2
9 exit 1
12 executable_p() {
13 command -v "$1" >/dev/null 2>&1
16 trap 'test -n "$casp" && rm -f "$casp"' 0
18 test -d "$CACHEDIR" || die "cache directory '$CACHEDIR' does not exist"
19 while getopts m:t:f opt; do
20 case $opt in
21 m) mime=$OPTARG;;
22 t) type=$OPTARG;;
23 f) force=1;;
25 die "usage: $0 [-m mime/type] [-t filter] [-f] path";
26 exit 1;;
27 esac
28 done
29 shift $(($OPTIND - 1))
30 test -z "$1" && die "usage $0: path"
32 ft=$(file -L --mime-type -b "$1") || die "$ft"
34 case $ft in
35 application/x-gzip | application/x-compress) dc=zcat;;
36 application/x-xz) dc=xzcat;;
37 application/x-bzip2) dc=bzcat;;
38 *) unset dc || true;;
39 esac
41 filt='"${dc-cat}" "$1" |'
43 if test -z "$type"; then
44 test -z "$dc" || ft=$(file -L --mime-type -bz "$1") || die "$ft"
46 mime=${mime:-$ft}
47 case $mime in
48 application/postscript) type=ps;;
49 application/pdf) type=pdf;;
50 image/vnd.djvu) type=djvu;;
51 text/html) type=html;;
52 application/msword) type=word;;
53 application/vnd.openxmlformats-officedocument.* \
54 | application/vnd.ms-powerpoint \
55 | application/vnd.ms-excel \
56 | application/vnd.oasis.opendocument.*) type=uno;;
57 image/svg+xml) type=svg;;
58 image/*) type=image;;
59 application/x-dvi) type=dvi;;
60 *) die "unhandled file type: '$mime'";;
61 esac
64 case $type in
65 ps) conv='ps2pdf - "$casp"';;
66 pdf) test -z "$dc" && exec llpp "$@" || conv='cat >"$casp"';;
67 djvu) conv='djvups - | ps2pdf - "$casp"';;
68 html) {
69 test -n "$dc" && die "can not convert compressed '$mime'"
70 unset filt
71 conv='prince "$1" -o "$casp"'
72 };;
73 word) {
74 if executable_p unoconv && test -z "$dc"; then
75 unset filt
76 conv='unoconv -o "$casp" "$1"'
77 else
78 conv='antiword -m 8859-1.txt -a a4 - >"$casp"'
80 };;
81 uno) {
82 test -n "$dc" && die "can not convert compressed '$mime'"
83 unset filt
84 conv='unoconv -o "$casp" "$1"'
85 };;
86 svg) {
87 executable_p rsvg-convert \
88 && conv='rsvg-convert -f pdf -o "$casp"' \
89 || conv='inkscape -z -A "$casp" -'
90 };;
91 image) conv='convert - pdf:"$casp"';;
92 dvi) {
93 test -n "$dc" && die "can not convert compressed '$mime'"
94 unset filt
95 conv='dvipdf "$1" "$casp"'
96 };;
97 *) die "unhandled filter type: '$type'";;
98 esac
100 hash=$(md5sum "$1") || die "$hash"
101 casp=$CACHEDIR/${hash%% *}
102 (test -n "$force" -o ! -e "$casp") && eval "$filt" "$conv"
104 exec llpp -origin "$@" "$casp"