Fix settrim
[llpp.git] / llppac
blob5b0101897842c0a44bf0b2c3b5842a921dccc148
1 #!/bin/sh
3 set -e
5 CACHEDIR=$HOME/.config/llpp
7 die()
9 echo "$1" >&2
10 exit 1
13 executable_p() {
14 command -v $1 >/dev/null 2>&1
17 trap '! test -z $casp && rm -f $casp' 0
19 test -d $CACHEDIR || die "cache directory $CACHEDIR does not exist"
20 while getopts m: opt; do
21 case "$opt" in
22 m) mime="$OPTARG";;
24 die "usage: $0 [-t mime/type] path";
25 exit 1;;
26 esac
27 done
28 shift $(($OPTIND - 1))
29 test -z "$1" && die "usage $0: path"
31 ft=$(file -L --mime-type -b "$1") || die "$ft"
33 case $ft in
34 application/x-gzip) dc=zcat;;
35 application/x-xz) dc=xzcat;;
36 application/x-bzip2) dc=bzcat;;
37 *) unset dc || true;;
38 esac
40 test -z $dc || ft=$(file -L --mime-type -bz "$1") || die "$ft"
42 filt='${dc-cat} "$1" |'
44 mime=${mime:-$ft}
45 case $mime in
46 application/postscript) conv='ps2pdf - $casp';;
47 application/pdf) test -z $dc && exec llpp "$1" || conv='cat >$casp';;
48 image/vnd.djvu) conv='djvups - | ps2pdf - $casp';;
49 text/html) {
50 ! test -z $dc && die "can not convert compressed $mime"
51 unset filt
52 conv='prince "$1" -o $casp'
53 };;
54 application/msword) {
55 if executable_p unoconv && test -z $dc; then
56 unset filt
57 conv='unoconv -o $casp "$1"'
58 else
59 conv='antiword -m 8859-1.txt -a a4 - >$casp'
61 };;
62 application/vnd.openxmlformats-officedocument.*) {
63 if executable_p unoconv; then
64 ! test -z $dc && die "can not convert compressed $mime"
65 unset filt
66 conv='unoconv -o $casp "$1"'
67 else
68 die 'unoconv not found'
70 };;
71 image/svg+xml) {
72 executable_p rsvg-convert \
73 && conv='rsvg-convert -f pdf -o $casp' \
74 || conv='inkscape -z -A $casp -'
75 };;
76 image/*) conv='convert - pdf:$casp';;
77 application/x-dvi) {
78 ! test -z $dc && die "can not convert compressed $mime"
79 unset filt
80 conv='dvipdf $1 $casp'
81 };;
82 *) die "unhandled file type: $mime";;
83 esac
85 hash=$(set - $(md5sum "$1"); printf $1)
86 casp="$CACHEDIR/$hash"
87 test -e $casp || eval "$filt" "$conv"
89 origin="$1"
90 shift
91 exec llpp -origin "$origin" $casp "$@"