Add opendocument(s)
[llpp.git] / llppac
blobebb04d26b0f94a4bbc884b464bae0f3f5dbff887
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 -z $casp && rm -f $casp' 0
18 test -d $CACHEDIR || die "cache directory $CACHEDIR does not exist"
19 while getopts m: opt; do
20 case "$opt" in
21 m) mime="$OPTARG";;
23 die "usage: $0 [-m mime/type] path";
24 exit 1;;
25 esac
26 done
27 shift $(($OPTIND - 1))
28 test -z "$1" && die "usage $0: path"
30 ft=$(file -L --mime-type -b "$1") || die "$ft"
32 case $ft in
33 application/x-gzip) dc=zcat;;
34 application/x-xz) dc=xzcat;;
35 application/x-bzip2) dc=bzcat;;
36 *) unset dc || true;;
37 esac
39 test -z $dc || ft=$(file -L --mime-type -bz "$1") || die "$ft"
41 filt='${dc-cat} "$1" |'
43 mime=${mime:-$ft}
44 case $mime in
45 application/postscript) conv='ps2pdf - $casp';;
46 application/pdf) test -z $dc && exec llpp "$@" || conv='cat >$casp';;
47 image/vnd.djvu) conv='djvups - | ps2pdf - $casp';;
48 text/html) {
49 ! test -z $dc && die "can not convert compressed $mime"
50 unset filt
51 conv='prince "$1" -o $casp'
52 };;
53 application/msword) {
54 if executable_p unoconv && test -z $dc; then
55 unset filt
56 conv='unoconv -o $casp "$1"'
57 else
58 conv='antiword -m 8859-1.txt -a a4 - >$casp'
60 };;
61 application/vnd.openxmlformats-officedocument.* \
62 | application/vnd.oasis.opendocument.*) {
63 ! test -z $dc && die "can not convert compressed $mime"
64 unset filt
65 conv='unoconv -o $casp "$1"'
66 };;
67 image/svg+xml) {
68 executable_p rsvg-convert \
69 && conv='rsvg-convert -f pdf -o $casp' \
70 || conv='inkscape -z -A $casp -'
71 };;
72 image/*) conv='convert - pdf:$casp';;
73 application/x-dvi) {
74 ! test -z $dc && die "can not convert compressed $mime"
75 unset filt
76 conv='dvipdf $1 $casp'
77 };;
78 *) die "unhandled file type: $mime";;
79 esac
81 hash=$(md5sum "$1") || die "$hash"
82 casp="$CACHEDIR/${hash%% *}"
83 test -e $casp || eval "$filt" "$conv"
85 exec llpp -origin "$@" $casp